join.c 7.3 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 <errno.h>
  16. #include <sched.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include "pthread.h"
  20. #include "internals.h"
  21. #include "spinlock.h"
  22. #include "restart.h"
  23. #include <not-cancel.h>
  24. void __pthread_exit(void * retval)
  25. {
  26. __pthread_do_exit (retval, CURRENT_STACK_FRAME);
  27. }
  28. strong_alias (__pthread_exit, pthread_exit)
  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. /* Reset the cancellation flag to avoid looping if the cleanup handlers
  35. contain cancellation points */
  36. THREAD_SETMEM(self, p_canceled, 0);
  37. /* Call cleanup functions and destroy the thread-specific data */
  38. __pthread_perform_cleanup(currentframe);
  39. __pthread_destroy_specifics();
  40. /* Store return value */
  41. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  42. THREAD_SETMEM(self, p_retval, retval);
  43. /* See whether we have to signal the death. */
  44. if (THREAD_GETMEM(self, p_report_events))
  45. {
  46. /* See whether TD_DEATH is in any of the mask. */
  47. int idx = __td_eventword (TD_DEATH);
  48. uint32_t mask = __td_eventmask (TD_DEATH);
  49. if ((mask & (__pthread_threads_events.event_bits[idx]
  50. | THREAD_GETMEM_NC(self,
  51. p_eventbuf.eventmask.event_bits[idx])))
  52. != 0)
  53. {
  54. /* Yep, we have to signal the death. */
  55. THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
  56. THREAD_SETMEM(self, p_eventbuf.eventdata, self);
  57. __pthread_last_event = self;
  58. /* Now call the function to signal the event. */
  59. __linuxthreads_death_event();
  60. }
  61. }
  62. /* Say that we've terminated */
  63. THREAD_SETMEM(self, p_terminated, 1);
  64. /* See if someone is joining on us */
  65. joining = THREAD_GETMEM(self, p_joining);
  66. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  67. /* Restart joining thread if any */
  68. if (joining != NULL) restart(joining);
  69. /* If this is the initial thread, block until all threads have terminated.
  70. If another thread calls exit, we'll be terminated from our signal
  71. handler. */
  72. if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
  73. request.req_thread = self;
  74. request.req_kind = REQ_MAIN_THREAD_EXIT;
  75. TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
  76. (char *)&request, sizeof(request)));
  77. suspend(self);
  78. /* Main thread flushes stdio streams and runs atexit functions.
  79. It also calls a handler within LinuxThreads which sends a process exit
  80. request to the thread manager. */
  81. exit(0);
  82. }
  83. /* Threads other than the main one terminate without flushing stdio streams
  84. or running atexit functions. */
  85. _exit(0);
  86. }
  87. /* Function called by pthread_cancel to remove the thread from
  88. waiting on a condition variable queue. */
  89. static int join_extricate_func(void *obj, pthread_descr th)
  90. {
  91. __volatile__ pthread_descr self = thread_self();
  92. pthread_handle handle = obj;
  93. pthread_descr jo;
  94. int did_remove = 0;
  95. __pthread_lock(&handle->h_lock, self);
  96. jo = handle->h_descr;
  97. did_remove = jo->p_joining != NULL;
  98. jo->p_joining = NULL;
  99. __pthread_unlock(&handle->h_lock);
  100. return did_remove;
  101. }
  102. int pthread_join(pthread_t thread_id, void ** thread_return)
  103. {
  104. __volatile__ pthread_descr self = thread_self();
  105. struct pthread_request request;
  106. pthread_handle handle = thread_handle(thread_id);
  107. pthread_descr th;
  108. pthread_extricate_if extr;
  109. int already_canceled = 0;
  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 (nonexisting_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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  141. }
  142. suspend(self);
  143. /* Deregister extrication interface */
  144. __pthread_set_own_extricate_if(self, 0);
  145. /* This is a cancellation point */
  146. if (THREAD_GETMEM(self, p_woken_by_cancel)
  147. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  148. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  149. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  150. }
  151. __pthread_lock(&handle->h_lock, self);
  152. }
  153. /* Get return value */
  154. if (thread_return != NULL) *thread_return = th->p_retval;
  155. __pthread_unlock(&handle->h_lock);
  156. /* Send notification to thread manager */
  157. if (__pthread_manager_request >= 0) {
  158. request.req_thread = self;
  159. request.req_kind = REQ_FREE;
  160. request.req_args.free.thread_id = thread_id;
  161. TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
  162. (char *) &request, sizeof(request)));
  163. }
  164. return 0;
  165. }
  166. int pthread_detach(pthread_t thread_id)
  167. {
  168. int terminated;
  169. struct pthread_request request;
  170. pthread_handle handle = thread_handle(thread_id);
  171. pthread_descr th;
  172. __pthread_lock(&handle->h_lock, NULL);
  173. if (nonexisting_handle(handle, thread_id)) {
  174. __pthread_unlock(&handle->h_lock);
  175. return ESRCH;
  176. }
  177. th = handle->h_descr;
  178. /* If already detached, error */
  179. if (th->p_detached) {
  180. __pthread_unlock(&handle->h_lock);
  181. return EINVAL;
  182. }
  183. /* If already joining, don't do anything. */
  184. if (th->p_joining != NULL) {
  185. __pthread_unlock(&handle->h_lock);
  186. return 0;
  187. }
  188. /* Mark as detached */
  189. th->p_detached = 1;
  190. terminated = th->p_terminated;
  191. __pthread_unlock(&handle->h_lock);
  192. /* If already terminated, notify thread manager to reclaim resources */
  193. if (terminated && __pthread_manager_request >= 0) {
  194. request.req_thread = thread_self();
  195. request.req_kind = REQ_FREE;
  196. request.req_args.free.thread_id = thread_id;
  197. TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
  198. (char *) &request, sizeof(request)));
  199. }
  200. return 0;
  201. }