join.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Thread termination and joining */
  15. #include <errno.h>
  16. #include <sched.h>
  17. #include <unistd.h>
  18. #include "pthread.h"
  19. #include "internals.h"
  20. #include "spinlock.h"
  21. #include "restart.h"
  22. #include "debug.h" /* PDEBUG, added by StS */
  23. void pthread_exit(void * retval)
  24. {
  25. pthread_descr self = thread_self();
  26. pthread_descr joining;
  27. struct pthread_request request;
  28. PDEBUG("self=%p, pid=%d\n", self, self->p_pid);
  29. /* Reset the cancellation flag to avoid looping if the cleanup handlers
  30. contain cancellation points */
  31. THREAD_SETMEM(self, p_canceled, 0);
  32. /* Call cleanup functions and destroy the thread-specific data */
  33. __pthread_perform_cleanup();
  34. __pthread_destroy_specifics();
  35. /* Store return value */
  36. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  37. THREAD_SETMEM(self, p_retval, retval);
  38. /* Say that we've terminated */
  39. THREAD_SETMEM(self, p_terminated, 1);
  40. /* See whether we have to signal the death. */
  41. if (THREAD_GETMEM(self, p_report_events))
  42. {
  43. /* See whether TD_DEATH is in any of the mask. */
  44. int idx = __td_eventword (TD_DEATH);
  45. uint32_t mask = __td_eventmask (TD_DEATH);
  46. if ((mask & (__pthread_threads_events.event_bits[idx]
  47. | THREAD_GETMEM(self,
  48. p_eventbuf.eventmask).event_bits[idx]))
  49. != 0)
  50. {
  51. /* Yep, we have to signal the death. */
  52. THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
  53. THREAD_SETMEM(self, p_eventbuf.eventdata, self);
  54. __pthread_last_event = self;
  55. /* Now call the function to signal the event. */
  56. __linuxthreads_death_event();
  57. }
  58. }
  59. /* See if someone is joining on us */
  60. joining = THREAD_GETMEM(self, p_joining);
  61. PDEBUG("joining = %p, pid=%d\n", joining, joining->p_pid);
  62. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  63. /* Restart joining thread if any */
  64. if (joining != NULL) restart(joining);
  65. /* If this is the initial thread, block until all threads have terminated.
  66. If another thread calls exit, we'll be terminated from our signal
  67. handler. */
  68. if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
  69. request.req_thread = self;
  70. request.req_kind = REQ_MAIN_THREAD_EXIT;
  71. __libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
  72. suspend(self);
  73. }
  74. /* Exit the process (but don't flush stdio streams, and don't run
  75. atexit functions). */
  76. _exit(0);
  77. }
  78. /* Function called by pthread_cancel to remove the thread from
  79. waiting on a condition variable queue. */
  80. static int join_extricate_func(void *obj, pthread_descr th)
  81. {
  82. volatile pthread_descr self = thread_self();
  83. pthread_handle handle = obj;
  84. pthread_descr jo;
  85. int did_remove = 0;
  86. __pthread_lock(&handle->h_lock, self);
  87. jo = handle->h_descr;
  88. did_remove = jo->p_joining != NULL;
  89. jo->p_joining = NULL;
  90. __pthread_unlock(&handle->h_lock);
  91. return did_remove;
  92. }
  93. int pthread_join(pthread_t thread_id, void ** thread_return)
  94. {
  95. volatile pthread_descr self = thread_self();
  96. struct pthread_request request;
  97. pthread_handle handle = thread_handle(thread_id);
  98. pthread_descr th;
  99. pthread_extricate_if extr;
  100. int already_canceled = 0;
  101. PDEBUG("\n");
  102. /* Set up extrication interface */
  103. extr.pu_object = handle;
  104. extr.pu_extricate_func = join_extricate_func;
  105. __pthread_lock(&handle->h_lock, self);
  106. if (invalid_handle(handle, thread_id)) {
  107. __pthread_unlock(&handle->h_lock);
  108. return ESRCH;
  109. }
  110. th = handle->h_descr;
  111. if (th == self) {
  112. __pthread_unlock(&handle->h_lock);
  113. return EDEADLK;
  114. }
  115. /* If detached or already joined, error */
  116. if (th->p_detached || th->p_joining != NULL) {
  117. __pthread_unlock(&handle->h_lock);
  118. return EINVAL;
  119. }
  120. /* If not terminated yet, suspend ourselves. */
  121. if (! th->p_terminated) {
  122. /* Register extrication interface */
  123. __pthread_set_own_extricate_if(self, &extr);
  124. if (!(THREAD_GETMEM(self, p_canceled)
  125. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  126. th->p_joining = self;
  127. else
  128. already_canceled = 1;
  129. __pthread_unlock(&handle->h_lock);
  130. if (already_canceled) {
  131. __pthread_set_own_extricate_if(self, 0);
  132. pthread_exit(PTHREAD_CANCELED);
  133. }
  134. PDEBUG("before suspend\n");
  135. suspend(self);
  136. PDEBUG("after suspend\n");
  137. /* Deregister extrication interface */
  138. __pthread_set_own_extricate_if(self, 0);
  139. /* This is a cancellation point */
  140. if (THREAD_GETMEM(self, p_woken_by_cancel)
  141. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  142. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  143. pthread_exit(PTHREAD_CANCELED);
  144. }
  145. __pthread_lock(&handle->h_lock, self);
  146. }
  147. /* Get return value */
  148. if (thread_return != NULL) *thread_return = th->p_retval;
  149. __pthread_unlock(&handle->h_lock);
  150. /* Send notification to thread manager */
  151. if (__pthread_manager_request >= 0) {
  152. request.req_thread = self;
  153. request.req_kind = REQ_FREE;
  154. request.req_args.free.thread_id = thread_id;
  155. __libc_write(__pthread_manager_request,
  156. (char *) &request, sizeof(request));
  157. }
  158. return 0;
  159. }
  160. int pthread_detach(pthread_t thread_id)
  161. {
  162. int terminated;
  163. struct pthread_request request;
  164. pthread_handle handle = thread_handle(thread_id);
  165. pthread_descr th;
  166. __pthread_lock(&handle->h_lock, NULL);
  167. if (invalid_handle(handle, thread_id)) {
  168. __pthread_unlock(&handle->h_lock);
  169. return ESRCH;
  170. }
  171. th = handle->h_descr;
  172. /* If already detached, error */
  173. if (th->p_detached) {
  174. __pthread_unlock(&handle->h_lock);
  175. return EINVAL;
  176. }
  177. /* If already joining, don't do anything. */
  178. if (th->p_joining != NULL) {
  179. __pthread_unlock(&handle->h_lock);
  180. return 0;
  181. }
  182. /* Mark as detached */
  183. th->p_detached = 1;
  184. terminated = th->p_terminated;
  185. __pthread_unlock(&handle->h_lock);
  186. /* If already terminated, notify thread manager to reclaim resources */
  187. if (terminated && __pthread_manager_request >= 0) {
  188. request.req_thread = thread_self();
  189. request.req_kind = REQ_FREE;
  190. request.req_args.free.thread_id = thread_id;
  191. __libc_write(__pthread_manager_request,
  192. (char *) &request, sizeof(request));
  193. }
  194. return 0;
  195. }