Modify elf/tst-audit9.c to use test-skeleton.c
[glibc.git] / sysdeps / nacl / exit-thread.h
blob915f93dd0da90e387d26647f504d94e316dddd1f
1 /* Call to terminate the current thread. NaCl version.
2 Copyright (C) 2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <atomic.h>
21 #include <futex-internal.h>
22 #include <nacl-interfaces.h>
23 #include <nptl/pthreadP.h>
25 /* This causes the current thread to exit, without affecting other
26 threads in the process if there are any. If there are no other
27 threads left, then this has the effect of _exit (0). */
29 static inline void __attribute__ ((noreturn, always_inline, unused))
30 __exit_thread (void)
32 struct pthread *pd = THREAD_SELF;
34 /* The generic logic for pthread_join and stack/descriptor reuse is
35 based on the Linux kernel feature that will clear and futex-wake
36 a designated address as a final part of thread teardown. Correct
37 synchronization relies on the fact that these happen only after
38 there is no possibility of user code touching or examining the
39 late thread's stack.
41 The NaCl system interface implements half of this: it clears a
42 word after the thread's user stack is safely dead, but it does
43 not futex-wake the location. So, some shenanigans are required.
44 We change and futex-wake the location here, so as to wake up any
45 blocked pthread_join (i.e. lll_wait_tid) or pthread_timedjoin_np
46 (i.e. lll_timedwait_tid). However, that's before we have safely
47 vacated the stack. So instead of clearing the location, we set
48 it to a special magic value, NACL_EXITING_TID. This counts as a
49 "live thread" value for all the generic logic, but is recognized
50 specially in lll_wait_tid and lll_timedwait_tid (lowlevellock.h).
51 Once it has this value, lll_wait_tid will busy-wait for the
52 location to be cleared to zero by the NaCl system code. Only then
53 is the stack actually safe to reuse. */
55 if (!IS_DETACHED (pd))
57 /* The magic value must not be one that could ever be a valid
58 TID value. See pthread-pids.h about the low bit. */
59 assert (NACL_EXITING_TID & 1);
61 /* The magic value must not be one that has the "free" flag
62 (i.e. sign bit) set. If that bit is set, then the
63 descriptor could be reused for a new thread. */
64 assert (NACL_EXITING_TID > 0);
66 atomic_store_relaxed (&pd->tid, NACL_EXITING_TID);
67 futex_wake ((unsigned int *) &pd->tid, 1, FUTEX_PRIVATE);
70 /* This clears PD->tid some time after the thread stack can never
71 be touched again. Unfortunately, it does not also do a
72 futex-wake at that time (as Linux does via CLONE_CHILD_CLEARTID
73 and set_tid_address). So lll_wait_tid does some busy-waiting. */
74 __nacl_irt_thread.thread_exit (&pd->tid);
76 /* That never returns unless something is severely and unrecoverably wrong.
77 If it ever does, try to make sure we crash. */
78 while (1)
79 __builtin_trap ();