pthread_join.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * pthread_join.c
  3. *
  4. * Description:
  5. * This translation unit implements functions related to thread
  6. * synchronisation.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. /*
  40. * Not needed yet, but defining it should indicate clashes with build target
  41. * environment that should be fixed.
  42. */
  43. #if !defined(WINCE)
  44. # include <signal.h>
  45. #endif
  46. int
  47. pthread_join (pthread_t thread, void **value_ptr)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function waits for 'thread' to terminate and
  52. * returns the thread's exit value if 'value_ptr' is not
  53. * NULL. This also detaches the thread on successful
  54. * completion.
  55. *
  56. * PARAMETERS
  57. * thread
  58. * an instance of pthread_t
  59. *
  60. * value_ptr
  61. * pointer to an instance of pointer to void
  62. *
  63. *
  64. * DESCRIPTION
  65. * This function waits for 'thread' to terminate and
  66. * returns the thread's exit value if 'value_ptr' is not
  67. * NULL. This also detaches the thread on successful
  68. * completion.
  69. * NOTE: detached threads cannot be joined or canceled
  70. *
  71. * RESULTS
  72. * 0 'thread' has completed
  73. * EINVAL thread is not a joinable thread,
  74. * ESRCH no thread could be found with ID 'thread',
  75. * ENOENT thread couldn't find it's own valid handle,
  76. * EDEADLK attempt to join thread with self
  77. *
  78. * ------------------------------------------------------
  79. */
  80. {
  81. int result;
  82. pthread_t self;
  83. ptw32_thread_t * tp = (ptw32_thread_t *) thread.p;
  84. ptw32_mcs_local_node_t node;
  85. ptw32_mcs_lock_acquire(&ptw32_thread_reuse_lock, &node);
  86. if (NULL == tp
  87. || thread.x != tp->ptHandle.x)
  88. {
  89. result = ESRCH;
  90. }
  91. else if (PTHREAD_CREATE_DETACHED == tp->detachState)
  92. {
  93. result = EINVAL;
  94. }
  95. else
  96. {
  97. result = 0;
  98. }
  99. ptw32_mcs_lock_release(&node);
  100. if (result == 0)
  101. {
  102. /*
  103. * The target thread is joinable and can't be reused before we join it.
  104. */
  105. self = pthread_self();
  106. if (NULL == self.p)
  107. {
  108. result = ENOENT;
  109. }
  110. else if (pthread_equal (self, thread))
  111. {
  112. result = EDEADLK;
  113. }
  114. else
  115. {
  116. /*
  117. * Pthread_join is a cancelation point.
  118. * If we are canceled then our target thread must not be
  119. * detached (destroyed). This is guarranteed because
  120. * pthreadCancelableWait will not return if we
  121. * are canceled.
  122. */
  123. result = pthreadCancelableWait (tp->threadH);
  124. if (0 == result)
  125. {
  126. if (value_ptr != NULL)
  127. {
  128. *value_ptr = tp->exitStatus;
  129. }
  130. /*
  131. * The result of making multiple simultaneous calls to
  132. * pthread_join() or pthread_detach() specifying the same
  133. * target is undefined.
  134. */
  135. result = pthread_detach (thread);
  136. }
  137. else
  138. {
  139. result = ESRCH;
  140. }
  141. }
  142. }
  143. return (result);
  144. } /* pthread_join */