pthread_join.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * pthread_join.c
  3. *
  4. * Description:
  5. * This translation unit implements functions related to thread
  6. * synchronisation.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. *
  16. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The original list of contributors to the Pthreads-win32 project
  23. * is contained in the file CONTRIBUTORS.ptw32 included with the
  24. * source code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include <pte_osal.h>
  44. #include "pthread.h"
  45. #include "implement.h"
  46. #include <stdio.h>
  47. int
  48. pthread_join (pthread_t thread, void **value_ptr)
  49. /*
  50. * ------------------------------------------------------
  51. * DOCPUBLIC
  52. * This function waits for 'thread' to terminate and
  53. * returns the thread's exit value if 'value_ptr' is not
  54. * NULL. This also detaches the thread on successful
  55. * completion.
  56. *
  57. * PARAMETERS
  58. * thread
  59. * an instance of pthread_t
  60. *
  61. * value_ptr
  62. * pointer to an instance of pointer to void
  63. *
  64. *
  65. * DESCRIPTION
  66. * This function waits for 'thread' to terminate and
  67. * returns the thread's exit value if 'value_ptr' is not
  68. * NULL. This also detaches the thread on successful
  69. * completion.
  70. * NOTE: detached threads cannot be joined or canceled
  71. *
  72. * RESULTS
  73. * 0 'thread' has completed
  74. * EINVAL thread is not a joinable thread,
  75. * ESRCH no thread could be found with ID 'thread',
  76. * ENOENT thread couldn't find it's own valid handle,
  77. * EDEADLK attempt to join thread with self
  78. *
  79. * ------------------------------------------------------
  80. */
  81. {
  82. int result;
  83. pthread_t self;
  84. pte_thread_t * tp = (pte_thread_t *) thread.p;
  85. pte_osMutexLock (pte_thread_reuse_lock);
  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. pte_osMutexUnlock(pte_thread_reuse_lock);
  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 = pte_osThreadWaitForEnd(tp->threadId);
  124. if (PTE_OS_OK == 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 if (result == PTE_OS_INTERRUPTED)
  138. {
  139. /* Call was cancelled, but still return success (per spec) */
  140. result = 0;
  141. }
  142. else
  143. {
  144. result = ESRCH;
  145. }
  146. }
  147. }
  148. return (result);
  149. } /* pthread_join */