pthread_join.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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;
  85. pte_osMutexLock (pte_thread_reuse_lock);
  86. if (NULL == tp)
  87. {
  88. result = ESRCH;
  89. }
  90. else if (PTHREAD_CREATE_DETACHED == tp->detachState)
  91. {
  92. result = EINVAL;
  93. }
  94. else
  95. {
  96. result = 0;
  97. }
  98. pte_osMutexUnlock(pte_thread_reuse_lock);
  99. if (result == 0)
  100. {
  101. /*
  102. * The target thread is joinable and can't be reused before we join it.
  103. */
  104. self = pthread_self();
  105. if (NULL == self)
  106. {
  107. result = ENOENT;
  108. }
  109. else if (pthread_equal (self, thread))
  110. {
  111. result = EDEADLK;
  112. }
  113. else
  114. {
  115. /*
  116. * Pthread_join is a cancelation point.
  117. * If we are canceled then our target thread must not be
  118. * detached (destroyed). This is guarranteed because
  119. * pthreadCancelableWait will not return if we
  120. * are canceled.
  121. */
  122. result = pte_osThreadWaitForEnd(tp->threadId);
  123. if (PTE_OS_OK == result)
  124. {
  125. if (value_ptr != NULL)
  126. {
  127. *value_ptr = tp->exitStatus;
  128. }
  129. /*
  130. * The result of making multiple simultaneous calls to
  131. * pthread_join() or pthread_detach() specifying the same
  132. * target is undefined.
  133. */
  134. result = pthread_detach (thread);
  135. }
  136. else if (result == PTE_OS_INTERRUPTED)
  137. {
  138. /* Call was cancelled, but still return success (per spec) */
  139. result = 0;
  140. }
  141. else
  142. {
  143. result = ESRCH;
  144. }
  145. }
  146. }
  147. return (result);
  148. } /* pthread_join */