pt-exit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Thread termination.
  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 <errno.h>
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <pt-internal.h>
  20. #include <pthreadP.h>
  21. #include <atomic.h>
  22. #include <shlib-compat.h>
  23. #include <libc-internal.h>
  24. /* Terminate the current thread and make STATUS available to any
  25. thread that might join it. */
  26. void
  27. __pthread_exit (void *status)
  28. {
  29. struct __pthread *self = _pthread_self ();
  30. struct __pthread_cancelation_handler **handlers;
  31. int oldstate;
  32. /* Run any cancelation handlers. According to POSIX, the
  33. cancellation cleanup handlers should be called with cancellation
  34. disabled. */
  35. __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
  36. for (handlers = __pthread_get_cleanup_stack ();
  37. *handlers != NULL;
  38. *handlers = (*handlers)->__next)
  39. (*handlers)->__handler ((*handlers)->__arg);
  40. /* Call destructors for the thread_local TLS variables. */
  41. call_function_static_weak (__call_tls_dtors);
  42. /* Clean up any state libc stored in thread-local variables. */
  43. __libc_thread_freeres ();
  44. __pthread_setcancelstate (oldstate, &oldstate);
  45. /* Decrease the number of threads. We use an atomic operation to
  46. make sure that only the last thread calls `exit'. */
  47. if (atomic_fetch_add_relaxed (&__pthread_total, -1) == 1)
  48. /* We are the last thread. */
  49. exit (0);
  50. /* Destroy any thread specific data. */
  51. __pthread_destroy_specific (self);
  52. /* Note that after this point the process can be terminated at any
  53. point if another thread calls `pthread_exit' and happens to be
  54. the last thread. */
  55. __pthread_mutex_lock (&self->state_lock);
  56. if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
  57. status = PTHREAD_CANCELED;
  58. switch (self->state)
  59. {
  60. default:
  61. assert (!"Consistency error: unexpected self->state");
  62. abort ();
  63. break;
  64. case PTHREAD_DETACHED:
  65. __pthread_mutex_unlock (&self->state_lock);
  66. break;
  67. case PTHREAD_JOINABLE:
  68. /* We need to stay around for a while since another thread
  69. might want to join us. */
  70. self->state = PTHREAD_EXITED;
  71. /* We need to remember the exit status. A thread joining us
  72. might ask for it. */
  73. self->status = status;
  74. /* Broadcast the condition. This will wake up threads that are
  75. waiting to join us. */
  76. __pthread_cond_broadcast (&self->state_cond);
  77. __pthread_mutex_unlock (&self->state_lock);
  78. break;
  79. }
  80. /* Destroy any signal state. */
  81. __pthread_sigstate_destroy (self);
  82. /* Self terminating requires TLS, so defer the release of the TCB until
  83. the thread structure is reused. */
  84. /* Release kernel resources, including the kernel thread and the stack,
  85. and drop the self reference. */
  86. __pthread_thread_terminate (self);
  87. /* NOTREACHED */
  88. abort ();
  89. }
  90. libc_hidden_def (__pthread_exit)
  91. versioned_symbol (libc, __pthread_exit, pthread_exit, GLIBC_2_21);
  92. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_12, GLIBC_2_21)
  93. compat_symbol (libpthread, __pthread_exit, pthread_exit, GLIBC_2_12);
  94. #endif