pthread_timedjoin.c 3.0 KB

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