join.c 7.2 KB

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