1 /* Deallocate a thread structure.
2 Copyright (C) 2000-2018 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/>. */
23 #include <pt-internal.h>
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
;
32 /* Deallocate the thread structure for PTHREAD. */
34 __pthread_dealloc (struct __pthread
*pthread
)
36 assert (pthread
->state
!= PTHREAD_TERMINATED
);
38 if (!atomic_decrement_and_test (&pthread
->nr_refs
))
41 /* Withdraw this thread from the thread ID lookup table. */
42 __pthread_setid (pthread
->thread
, NULL
);
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
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
);
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
);
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.
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
;