]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-dealloc.c
x86: In ld.so, diagnose missing APX support in APX-only builds
[thirdparty/glibc.git] / htl / pt-dealloc.c
1 /* Deallocate a thread structure.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #include <assert.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22
23 #include <pt-internal.h>
24
25 #include <atomic.h>
26
27 /* List of thread structures corresponding to free thread IDs. */
28 extern struct __pthread *__pthread_free_threads;
29 extern pthread_mutex_t __pthread_free_threads_lock;
30
31
32 /* Deallocate the thread structure for PTHREAD. */
33 void
34 __pthread_dealloc (struct __pthread *pthread)
35 {
36 assert (pthread->state != PTHREAD_TERMINATED);
37
38 if (!atomic_decrement_and_test (&pthread->nr_refs))
39 return;
40
41 /* Withdraw this thread from the thread ID lookup table. */
42 __pthread_setid (pthread->thread, NULL);
43
44 /* Mark the thread as terminated. We broadcast the condition
45 here to prevent pthread_join from waiting for this thread to
46 exit where it was never really started. Such a call to
47 pthread_join is completely bogus, but unfortunately allowed
48 by the standards. */
49 __pthread_mutex_lock (&pthread->state_lock);
50 if (pthread->state != PTHREAD_EXITED)
51 __pthread_cond_broadcast (&pthread->state_cond);
52 __pthread_mutex_unlock (&pthread->state_lock);
53
54 /* We do not actually deallocate the thread structure, but add it to
55 a list of re-usable thread structures. */
56 __pthread_mutex_lock (&__pthread_free_threads_lock);
57 __pthread_enqueue (&__pthread_free_threads, pthread);
58 __pthread_mutex_unlock (&__pthread_free_threads_lock);
59
60 /* Setting PTHREAD->STATE to PTHREAD_TERMINATED makes this TCB
61 available for reuse. After that point, we can no longer assume
62 that PTHREAD is valid.
63
64 Note that it is safe to not lock this update to PTHREAD->STATE:
65 the only way that it can now be accessed is in __pthread_alloc,
66 which reads this variable. */
67 pthread->state = PTHREAD_TERMINATED;
68 }