join.c 7.4 KB

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