pt-dealloc.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Deallocate a thread structure.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <pt-internal.h>
  19. #include <atomic.h>
  20. #include <ldsodefs.h>
  21. /* List of thread structures corresponding to free thread IDs. */
  22. extern struct __pthread *__pthread_free_threads;
  23. extern pthread_mutex_t __pthread_free_threads_lock;
  24. /* Deallocate the content of the thread structure for PTHREAD. */
  25. void
  26. __pthread_dealloc (struct __pthread *pthread)
  27. {
  28. if (atomic_fetch_add_relaxed (&pthread->nr_refs, -1) != 1)
  29. return;
  30. /* Withdraw this thread from the thread ID lookup table. */
  31. __pthread_setid (pthread->thread, NULL);
  32. /* Mark the thread as terminated. We broadcast the condition
  33. here to prevent pthread_join from waiting for this thread to
  34. exit where it was never really started. Such a call to
  35. pthread_join is completely bogus, but unfortunately allowed
  36. by the standards. */
  37. __pthread_mutex_lock (&pthread->state_lock);
  38. if (pthread->state != PTHREAD_EXITED)
  39. __pthread_cond_broadcast (&pthread->state_cond);
  40. __pthread_mutex_unlock (&pthread->state_lock);
  41. /* We do not actually deallocate the thread structure, but add it to
  42. a list of re-usable thread structures. */
  43. __pthread_mutex_lock (&__pthread_free_threads_lock);
  44. __pthread_enqueue (&__pthread_free_threads, pthread);
  45. __pthread_mutex_unlock (&__pthread_free_threads_lock);
  46. }
  47. libc_hidden_def (__pthread_dealloc)
  48. /* Confirm deallocation of the thread structure for PTHREAD. */
  49. void
  50. __pthread_dealloc_finish (struct __pthread *pthread)
  51. {
  52. /* Setting PTHREAD->TERMINATED makes this TCB
  53. available for reuse. After that point, we can no longer assume
  54. that PTHREAD is valid.
  55. Note that it is safe to not lock this update to PTHREAD->STATE:
  56. the only way that it can now be accessed is in __pthread_alloc,
  57. which reads this variable. */
  58. pthread->terminated = TRUE;
  59. }
  60. libc_hidden_def (__pthread_dealloc_finish)