pthread_join.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <atomic.h>
  18. #include "pthreadP.h"
  19. static void
  20. cleanup (void *arg)
  21. {
  22. /* If we already changed the waiter ID, reset it. The call cannot
  23. fail for any reason but the thread not having done that yet so
  24. there is no reason for a loop. */
  25. (void) atomic_compare_and_exchange_bool_acq ((struct pthread **) arg, NULL,
  26. THREAD_SELF);
  27. }
  28. int
  29. pthread_join (
  30. pthread_t threadid,
  31. void **thread_return)
  32. {
  33. struct pthread *pd = (struct pthread *) threadid;
  34. /* Make sure the descriptor is valid. */
  35. if (INVALID_NOT_TERMINATED_TD_P (pd))
  36. /* Not a valid thread handle. */
  37. return ESRCH;
  38. /* Is the thread joinable?. */
  39. if (IS_DETACHED (pd))
  40. /* We cannot wait for the thread. */
  41. return EINVAL;
  42. struct pthread *self = THREAD_SELF;
  43. int result = 0;
  44. /* During the wait we change to asynchronous cancellation. If we
  45. are canceled the thread we are waiting for must be marked as
  46. un-wait-ed for again. */
  47. pthread_cleanup_push (cleanup, &pd->joinid);
  48. /* Switch to asynchronous cancellation. */
  49. int oldtype = CANCEL_ASYNC ();
  50. if ((pd == self
  51. || (self->joinid == pd
  52. && (pd->cancelhandling
  53. & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
  54. | TERMINATED_BITMASK)) == 0))
  55. && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
  56. /* This is a deadlock situation. The threads are waiting for each
  57. other to finish. Note that this is a "may" error. To be 100%
  58. sure we catch this error we would have to lock the data
  59. structures but it is not necessary. In the unlikely case that
  60. two threads are really caught in this situation they will
  61. deadlock. It is the programmer's problem to figure this
  62. out. */
  63. result = EDEADLK;
  64. /* Wait for the thread to finish. If it is already locked something
  65. is wrong. There can only be one waiter. */
  66. else if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
  67. self,
  68. NULL), 0))
  69. /* There is already somebody waiting for the thread. */
  70. result = EINVAL;
  71. else
  72. /* Wait for the child. */
  73. lll_wait_tid (pd->tid);
  74. /* Restore cancellation mode. */
  75. CANCEL_RESET (oldtype);
  76. /* Remove the handler. */
  77. pthread_cleanup_pop (0);
  78. if (__builtin_expect (result == 0, 1))
  79. {
  80. /* We mark the thread as terminated and as joined. */
  81. pd->tid = -1;
  82. /* Store the return value if the caller is interested. */
  83. if (thread_return != NULL)
  84. *thread_return = pd->result;
  85. /* Free the TCB. */
  86. __free_tcb (pd);
  87. }
  88. return result;
  89. }