join.c 7.2 KB

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