pthread_join.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (C) 2002, 2003 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. *(void **) arg = NULL;
  24. }
  25. int
  26. pthread_join (threadid, thread_return)
  27. pthread_t threadid;
  28. void **thread_return;
  29. {
  30. struct pthread *self;
  31. struct pthread *pd = (struct pthread *) threadid;
  32. /* Make sure the descriptor is valid. */
  33. if (INVALID_NOT_TERMINATED_TD_P (pd))
  34. /* Not a valid thread handle. */
  35. return ESRCH;
  36. /* Is the thread joinable?. */
  37. if (IS_DETACHED (pd))
  38. /* We cannot wait for the thread. */
  39. return EINVAL;
  40. self = THREAD_SELF;
  41. if (pd == self
  42. || (self->joinid == pd
  43. && (pd->cancelhandling
  44. & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
  45. | TERMINATED_BITMASK)) == 0))
  46. /* This is a deadlock situation. The threads are waiting for each
  47. other to finish. Note that this is a "may" error. To be 100%
  48. sure we catch this error we would have to lock the data
  49. structures but it is not necessary. In the unlikely case that
  50. two threads are really caught in this situation they will
  51. deadlock. It is the programmer's problem to figure this
  52. out. */
  53. return EDEADLK;
  54. /* Wait for the thread to finish. If it is already locked something
  55. is wrong. There can only be one waiter. */
  56. if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
  57. self,
  58. NULL), 0))
  59. /* There is already somebody waiting for the thread. */
  60. return EINVAL;
  61. /* During the wait we change to asynchronous cancellation. If we
  62. are cancelled the thread we are waiting for must be marked as
  63. un-wait-ed for again. */
  64. pthread_cleanup_push (cleanup, &pd->joinid);
  65. /* Switch to asynchronous cancellation. */
  66. int oldtype = CANCEL_ASYNC ();
  67. /* Wait for the child. */
  68. lll_wait_tid (pd->tid);
  69. /* Restore cancellation mode. */
  70. CANCEL_RESET (oldtype);
  71. /* Remove the handler. */
  72. pthread_cleanup_pop (0);
  73. /* We mark the thread as terminated and as joined. */
  74. pd->tid = -1;
  75. /* Store the return value if the caller is interested. */
  76. if (thread_return != NULL)
  77. *thread_return = pd->result;
  78. /* Free the TCB. */
  79. __free_tcb (pd);
  80. return 0;
  81. }