]> git.saurik.com Git - apple/libpthread.git/commitdiff
libpthread-330.230.1.tar.gz macos-10142 macos-10143 v330.230.1
authorApple <opensource@apple.com>
Tue, 26 Mar 2019 22:03:38 +0000 (22:03 +0000)
committerApple <opensource@apple.com>
Tue, 26 Mar 2019 22:03:38 +0000 (22:03 +0000)
src/internal.h
src/pthread.c
src/pthread_atfork.c
tests/pthread_threadid_np.c

index c9c16c7ae66d9e1733e1e5b1ea7e4095a2bc2c16..e8cc16158aec17e3882f1d5166027c56e891b286 100644 (file)
@@ -546,6 +546,10 @@ PTHREAD_NOEXPORT
 void
 _pthread_main_thread_init(pthread_t p);
 
+PTHREAD_NOEXPORT
+void
+_pthread_main_thread_postfork_init(pthread_t p);
+
 PTHREAD_NOEXPORT
 void
 _pthread_bsdthread_init(struct _pthread_registration_data *data);
index f9aa0dcb4e382c474bd27a07bf5902f51396a203..3c3ea6a02645a2a1d6ad3e3ab27d0fbc640f69fa 100644 (file)
@@ -1948,7 +1948,7 @@ _pthread_main_thread_init(pthread_t p)
        p->__cleanup_stack = NULL;
        p->tl_join_ctx = NULL;
        p->tl_exit_gate = MACH_PORT_NULL;
-       p->tsd[__TSD_SEMAPHORE_CACHE] = (void*)SEMAPHORE_NULL;
+       p->tsd[__TSD_SEMAPHORE_CACHE] = (void*)(uintptr_t)SEMAPHORE_NULL;
        p->tsd[__TSD_MACH_SPECIAL_REPLY] = 0;
        p->cancel_state |= _PTHREAD_CANCEL_INITIALIZED;
 
@@ -1959,6 +1959,14 @@ _pthread_main_thread_init(pthread_t p)
        _pthread_introspection_thread_start(p);
 }
 
+PTHREAD_NOEXPORT
+void
+_pthread_main_thread_postfork_init(pthread_t p)
+{
+       _pthread_main_thread_init(p);
+       _pthread_set_self_internal(p, false);
+}
+
 int
 sched_yield(void)
 {
index d489c24118becf9791a71566e7a884910f73b3a1..edeec16dbcf2e59f65c96f7fbb27074fb9bd77d7 100644 (file)
@@ -156,7 +156,7 @@ _pthread_atfork_child(void)
        pthread_globals_t globals = _pthread_globals();
        _PTHREAD_LOCK_INIT(globals->psaved_self_global_lock);
        __is_threaded = 0;
-       _pthread_main_thread_init(globals->psaved_self);
+       _pthread_main_thread_postfork_init(globals->psaved_self);
 
        struct _pthread_registration_data registration_data;
        _pthread_bsdthread_init(&registration_data);
index 19cfc254f5f78c8d0df6a4cb72c79f8ea5bab71a..46eb527136fb58bd305982be4d5ed5cff71a610c 100644 (file)
@@ -1,6 +1,8 @@
 #include <pthread.h>
 #include <pthread/private.h>
 #include <dispatch/dispatch.h>
+#include <sys/wait.h>
+#include <stdlib.h>
 
 #include "darwintest_defaults.h"
 
@@ -42,3 +44,41 @@ T_DECL(pthread_threadid_np, "pthread_threadid_np",
        dispatch_async(dq, ^{ do_test(NULL); });
        dispatch_sync(dq, ^{});
 }
+
+T_DECL(pthread_threadid_fork, "pthread_threadid_np post-fork test")
+{
+       uint64_t tid = __thread_selfid();
+       T_ASSERT_NE(tid, (uint64_t)0, "__thread_selfid()");
+
+       uint64_t ptid = 0;
+       T_ASSERT_POSIX_ZERO(pthread_threadid_np(NULL, &ptid), NULL);
+       T_ASSERT_EQ(ptid, tid, NULL);
+
+       pid_t pid = fork();
+       if (pid == 0) {
+               // child
+               uint64_t ntid = __thread_selfid();
+               if (ntid == tid) {
+                       T_LOG("FAIL: forked child tid is equal to parent tid");
+                       exit(1);
+               }
+
+               uint64_t nptid = 0;
+               if (pthread_threadid_np(NULL, &nptid) != 0) {
+                       T_LOG("FAIL: pthread_threadid_np: %d", errno);
+                       exit(1);
+               }
+
+               if (nptid != ntid) {
+                       T_LOG("FAIL: pthread_threadid_np == tid (expected: %lld == %lld)",
+                                       nptid, ntid);
+                       exit(1);
+               }
+               exit(0);
+       } else {
+               int status;
+               T_ASSERT_EQ(waitpid(pid, &status, 0), pid, NULL);
+               int exitstatus = WEXITSTATUS(status);
+               T_ASSERT_EQ(exitstatus, 0, NULL);
+       }
+}