pt-join.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Wait for 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 <errno.h>
  16. #include <pthread.h>
  17. #include <stddef.h>
  18. #include <pt-internal.h>
  19. #include <shlib-compat.h>
  20. #include <ldsodefs.h>
  21. /* Make calling thread wait for termination of thread THREAD. Return
  22. the exit status of the thread in *STATUS. */
  23. static int
  24. __pthread_join_common (pthread_t thread, void **status, int try,
  25. clockid_t clockid,
  26. const struct timespec *abstime)
  27. {
  28. struct __pthread *pthread;
  29. int err = 0;
  30. /* Lookup the thread structure for THREAD. */
  31. pthread = __pthread_getid (thread);
  32. if (pthread == NULL)
  33. return ESRCH;
  34. if (pthread == _pthread_self ())
  35. return EDEADLK;
  36. __pthread_mutex_lock (&pthread->state_lock);
  37. if (try == 0)
  38. {
  39. pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock,
  40. &pthread->state_lock);
  41. /* Rely on pthread_cond_wait being a cancellation point to make
  42. pthread_join one too. */
  43. while (pthread->state == PTHREAD_JOINABLE && err != ETIMEDOUT && err != EINVAL)
  44. err = __pthread_cond_clockwait (&pthread->state_cond,
  45. &pthread->state_lock,
  46. clockid, abstime);
  47. pthread_cleanup_pop (0);
  48. if (err == EINVAL)
  49. {
  50. __pthread_mutex_unlock (&pthread->state_lock);
  51. return err;
  52. }
  53. }
  54. switch (pthread->state)
  55. {
  56. case PTHREAD_JOINABLE:
  57. __pthread_mutex_unlock (&pthread->state_lock);
  58. if (err != ETIMEDOUT)
  59. err = EBUSY;
  60. break;
  61. case PTHREAD_EXITED:
  62. /* THREAD has already exited. Salvage its exit status. */
  63. if (status != NULL)
  64. *status = pthread->status;
  65. __pthread_mutex_unlock (&pthread->state_lock);
  66. __pthread_dealloc (pthread);
  67. break;
  68. default:
  69. /* Thou shalt not join non-joinable threads! */
  70. __pthread_mutex_unlock (&pthread->state_lock);
  71. err = EINVAL;
  72. break;
  73. }
  74. return err;
  75. }
  76. int
  77. __pthread_join (pthread_t thread, void **status)
  78. {
  79. return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, NULL);
  80. }
  81. libc_hidden_def (__pthread_join)
  82. versioned_symbol (libc, __pthread_join, pthread_join, GLIBC_2_43);
  83. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_12, GLIBC_2_43)
  84. compat_symbol (libc, __pthread_join, pthread_join, GLIBC_2_12);
  85. #endif
  86. int
  87. __pthread_tryjoin_np (pthread_t thread, void **status)
  88. {
  89. return __pthread_join_common (thread, status, 1, CLOCK_REALTIME, NULL);
  90. }
  91. libc_hidden_def (__pthread_tryjoin_np)
  92. versioned_symbol (libc, __pthread_tryjoin_np, pthread_tryjoin_np, GLIBC_2_43);
  93. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_32, GLIBC_2_43)
  94. compat_symbol (libc, __pthread_tryjoin_np, pthread_tryjoin_np, GLIBC_2_32);
  95. #endif
  96. int
  97. __pthread_timedjoin_np (pthread_t thread, void **status,
  98. const struct timespec *abstime)
  99. {
  100. return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, abstime);
  101. }
  102. libc_hidden_def (__pthread_timedjoin_np)
  103. versioned_symbol (libc, __pthread_timedjoin_np, pthread_timedjoin_np, GLIBC_2_43);
  104. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_32, GLIBC_2_43)
  105. compat_symbol (libc, __pthread_timedjoin_np, pthread_timedjoin_np, GLIBC_2_32);
  106. #endif
  107. int
  108. __pthread_clockjoin_np (pthread_t thread, void **status,
  109. clockid_t clockid,
  110. const struct timespec *abstime)
  111. {
  112. return __pthread_join_common (thread, status, 0, clockid, abstime);
  113. }
  114. libc_hidden_def (__pthread_clockjoin_np)
  115. versioned_symbol (libc, __pthread_clockjoin_np, pthread_clockjoin_np, GLIBC_2_43);
  116. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_32, GLIBC_2_43)
  117. compat_symbol (libc, __pthread_clockjoin_np, pthread_clockjoin_np, GLIBC_2_32);
  118. #endif