join.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <features.h>
  16. #define __USE_GNU
  17. #include <errno.h>
  18. #include <sched.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include "pthread.h"
  22. #include "internals.h"
  23. #include "spinlock.h"
  24. #include "restart.h"
  25. #include "debug.h" /* PDEBUG, added by StS */
  26. void pthread_exit(void * retval)
  27. {
  28. pthread_descr self = thread_self();
  29. pthread_descr joining;
  30. struct pthread_request request;
  31. PDEBUG("self=%p, pid=%d\n", self, self->p_pid);
  32. /* Reset the cancellation flag to avoid looping if the cleanup handlers
  33. contain cancellation points */
  34. THREAD_SETMEM(self, p_canceled, 0);
  35. /* Call cleanup functions and destroy the thread-specific data */
  36. __pthread_perform_cleanup();
  37. __pthread_destroy_specifics();
  38. /* Store return value */
  39. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  40. THREAD_SETMEM(self, p_retval, retval);
  41. /* Say that we've terminated */
  42. THREAD_SETMEM(self, p_terminated, 1);
  43. /* See whether we have to signal the death. */
  44. if (THREAD_GETMEM(self, p_report_events))
  45. {
  46. /* See whether TD_DEATH is in any of the mask. */
  47. int idx = __td_eventword (TD_DEATH);
  48. uint32_t mask = __td_eventmask (TD_DEATH);
  49. if ((mask & (__pthread_threads_events.event_bits[idx]
  50. | THREAD_GETMEM_NC(self,
  51. p_eventbuf.eventmask).event_bits[idx]))
  52. != 0)
  53. {
  54. #if 0 /* Appears like DEATH event reporting is broken */
  55. /* Yep, we have to signal the death. */
  56. THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
  57. THREAD_SETMEM(self, p_eventbuf.eventdata, self);
  58. __pthread_last_event = self;
  59. /* Now call the function to signal the event. */
  60. __linuxthreads_death_event();
  61. #endif
  62. }
  63. }
  64. /* See if someone is joining on us */
  65. joining = THREAD_GETMEM(self, p_joining);
  66. PDEBUG("joining = %p, pid=%d\n", joining, joining->p_pid);
  67. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  68. /* Restart joining thread if any */
  69. if (joining != NULL) restart(joining);
  70. /* If this is the initial thread, block until all threads have terminated.
  71. If another thread calls exit, we'll be terminated from our signal
  72. handler. */
  73. if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
  74. request.req_thread = self;
  75. request.req_kind = REQ_MAIN_THREAD_EXIT;
  76. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  77. (char *)&request, sizeof(request)));
  78. suspend(self);
  79. /* Main thread flushes stdio streams and runs atexit functions.
  80. * It also calls a handler within LinuxThreads which sends a process exit
  81. * request to the thread manager. */
  82. exit(0);
  83. }
  84. /* Exit the process (but don't flush stdio streams, and don't run
  85. atexit functions). */
  86. _exit(0);
  87. }
  88. /* Function called by pthread_cancel to remove the thread from
  89. waiting on a condition variable queue. */
  90. static int join_extricate_func(void *obj, pthread_descr th)
  91. {
  92. volatile pthread_descr self = thread_self();
  93. pthread_handle handle = obj;
  94. pthread_descr jo;
  95. int did_remove = 0;
  96. __pthread_lock(&handle->h_lock, self);
  97. jo = handle->h_descr;
  98. did_remove = jo->p_joining != NULL;
  99. jo->p_joining = NULL;
  100. __pthread_unlock(&handle->h_lock);
  101. return did_remove;
  102. }
  103. int pthread_join(pthread_t thread_id, void ** thread_return)
  104. {
  105. volatile pthread_descr self = thread_self();
  106. struct pthread_request request;
  107. pthread_handle handle = thread_handle(thread_id);
  108. pthread_descr th;
  109. pthread_extricate_if extr;
  110. int already_canceled = 0;
  111. PDEBUG("\n");
  112. /* Set up extrication interface */
  113. extr.pu_object = handle;
  114. extr.pu_extricate_func = join_extricate_func;
  115. __pthread_lock(&handle->h_lock, self);
  116. if (invalid_handle(handle, thread_id)) {
  117. __pthread_unlock(&handle->h_lock);
  118. return ESRCH;
  119. }
  120. th = handle->h_descr;
  121. if (th == self) {
  122. __pthread_unlock(&handle->h_lock);
  123. return EDEADLK;
  124. }
  125. /* If detached or already joined, error */
  126. if (th->p_detached || th->p_joining != NULL) {
  127. __pthread_unlock(&handle->h_lock);
  128. return EINVAL;
  129. }
  130. /* If not terminated yet, suspend ourselves. */
  131. if (! th->p_terminated) {
  132. /* Register extrication interface */
  133. __pthread_set_own_extricate_if(self, &extr);
  134. if (!(THREAD_GETMEM(self, p_canceled)
  135. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  136. th->p_joining = self;
  137. else
  138. already_canceled = 1;
  139. __pthread_unlock(&handle->h_lock);
  140. if (already_canceled) {
  141. __pthread_set_own_extricate_if(self, 0);
  142. pthread_exit(PTHREAD_CANCELED);
  143. }
  144. PDEBUG("before suspend\n");
  145. suspend(self);
  146. PDEBUG("after suspend\n");
  147. /* Deregister extrication interface */
  148. __pthread_set_own_extricate_if(self, 0);
  149. /* This is a cancellation point */
  150. if (THREAD_GETMEM(self, p_woken_by_cancel)
  151. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  152. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  153. pthread_exit(PTHREAD_CANCELED);
  154. }
  155. __pthread_lock(&handle->h_lock, self);
  156. }
  157. /* Get return value */
  158. if (thread_return != NULL) *thread_return = th->p_retval;
  159. __pthread_unlock(&handle->h_lock);
  160. /* Send notification to thread manager */
  161. if (__pthread_manager_request >= 0) {
  162. request.req_thread = self;
  163. request.req_kind = REQ_FREE;
  164. request.req_args.free.thread_id = thread_id;
  165. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  166. (char *) &request, sizeof(request)));
  167. }
  168. return 0;
  169. }
  170. int pthread_detach(pthread_t thread_id)
  171. {
  172. int terminated;
  173. struct pthread_request request;
  174. pthread_handle handle = thread_handle(thread_id);
  175. pthread_descr th;
  176. __pthread_lock(&handle->h_lock, NULL);
  177. if (invalid_handle(handle, thread_id)) {
  178. __pthread_unlock(&handle->h_lock);
  179. return ESRCH;
  180. }
  181. th = handle->h_descr;
  182. /* If already detached, error */
  183. if (th->p_detached) {
  184. __pthread_unlock(&handle->h_lock);
  185. return EINVAL;
  186. }
  187. /* If already joining, don't do anything. */
  188. if (th->p_joining != NULL) {
  189. __pthread_unlock(&handle->h_lock);
  190. return 0;
  191. }
  192. /* Mark as detached */
  193. th->p_detached = 1;
  194. terminated = th->p_terminated;
  195. __pthread_unlock(&handle->h_lock);
  196. /* If already terminated, notify thread manager to reclaim resources */
  197. if (terminated && __pthread_manager_request >= 0) {
  198. request.req_thread = thread_self();
  199. request.req_kind = REQ_FREE;
  200. request.req_args.free.thread_id = thread_id;
  201. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  202. (char *) &request, sizeof(request)));
  203. }
  204. return 0;
  205. }