pthread_join.c 3.5 KB

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