From c28b7a9d931133a677bcf4201285277e6fe1a2b2 Mon Sep 17 00:00:00 2001 From: Apple Date: Tue, 26 Mar 2019 22:03:38 +0000 Subject: [PATCH] libpthread-330.230.1.tar.gz --- src/internal.h | 4 ++++ src/pthread.c | 10 +++++++++- src/pthread_atfork.c | 2 +- tests/pthread_threadid_np.c | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/internal.h b/src/internal.h index c9c16c7..e8cc161 100644 --- a/src/internal.h +++ b/src/internal.h @@ -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); diff --git a/src/pthread.c b/src/pthread.c index f9aa0dc..3c3ea6a 100644 --- a/src/pthread.c +++ b/src/pthread.c @@ -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) { diff --git a/src/pthread_atfork.c b/src/pthread_atfork.c index d489c24..edeec16 100644 --- a/src/pthread_atfork.c +++ b/src/pthread_atfork.c @@ -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(®istration_data); diff --git a/tests/pthread_threadid_np.c b/tests/pthread_threadid_np.c index 19cfc25..46eb527 100644 --- a/tests/pthread_threadid_np.c +++ b/tests/pthread_threadid_np.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #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); + } +} -- 2.45.2