join.c 7.5 KB

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