From 3ed015122f159b4fade8e4feecb53fe1f84fa95f Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Thu, 28 May 2015 15:37:31 -0700 Subject: NaCl: Make thread exit wake pthread_join. --- sysdeps/nacl/exit-thread.h | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'sysdeps/nacl/exit-thread.h') diff --git a/sysdeps/nacl/exit-thread.h b/sysdeps/nacl/exit-thread.h index a08a5b1..c809405 100644 --- a/sysdeps/nacl/exit-thread.h +++ b/sysdeps/nacl/exit-thread.h @@ -16,8 +16,11 @@ License along with the GNU C Library; if not, see . */ -#include +#include +#include +#include #include +#include /* This causes the current thread to exit, without affecting other threads in the process if there are any. If there are no other @@ -26,7 +29,49 @@ static inline void __attribute__ ((noreturn, always_inline, unused)) __exit_thread (void) { - __nacl_irt_thread.thread_exit (NULL); + struct pthread *pd = THREAD_SELF; + + /* The generic logic for pthread_join and stack/descriptor reuse is + based on the Linux kernel feature that will clear and futex-wake + a designated address as a final part of thread teardown. Correct + synchronization relies on the fact that these happen only after + there is no possibility of user code touching or examining the + late thread's stack. + + The NaCl system interface implements half of this: it clears a + word after the thread's user stack is safely dead, but it does + not futex-wake the location. So, some shenanigans are required. + We change and futex-wake the location here, so as to wake up any + blocked pthread_join (i.e. lll_wait_tid) or pthread_timedjoin_np + (i.e. lll_timedwait_tid). However, that's before we have safely + vacated the stack. So instead of clearing the location, we set + it to a special magic value, NACL_EXITING_TID. This counts as a + "live thread" value for all the generic logic, but is recognized + specially in lll_wait_tid and lll_timedwait_tid (lowlevellock.h). + Once it has this value, lll_wait_tid will busy-wait for the + location to be cleared to zero by the NaCl system code. Only then + is the stack actually safe to reuse. */ + + if (!IS_DETACHED (pd)) + { + /* The magic value must not be one that could ever be a valid + TID value. See pthread-pids.h about the low bit. */ + assert (NACL_EXITING_TID & 1); + + /* The magic value must not be one that has the "free" flag + (i.e. sign bit) set. If that bit is set, then the + descriptor could be reused for a new thread. */ + assert (NACL_EXITING_TID > 0); + + atomic_store_relaxed (&pd->tid, NACL_EXITING_TID); + lll_futex_wake (&pd->tid, 1, LLL_PRIVATE); + } + + /* This clears PD->tid some time after the thread stack can never + be touched again. Unfortunately, it does not also do a + futex-wake at that time (as Linux does via CLONE_CHILD_CLEARTID + and set_tid_address). So lll_wait_tid does some busy-waiting. */ + __nacl_irt_thread.thread_exit (&pd->tid); /* That never returns unless something is severely and unrecoverably wrong. If it ever does, try to make sure we crash. */ -- cgit v1.1