join.c 7.6 KB

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