join.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 ? joining->p_pid : 0);
  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(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(write(__pthread_manager_request,
  172. (char *) &request, sizeof(request)));
  173. }
  174. return 0;
  175. }
  176. int pthread_tryjoin_np(pthread_t thread_id, void ** thread_return)
  177. {
  178. volatile pthread_descr self = thread_self();
  179. struct pthread_request request;
  180. pthread_handle handle = thread_handle(thread_id);
  181. pthread_descr th;
  182. int result = 0;
  183. /* Make sure the descriptor is valid. */
  184. __pthread_lock(&handle->h_lock, self);
  185. if (invalid_handle(handle, thread_id)) {
  186. result = ESRCH;
  187. goto err;
  188. }
  189. th = handle->h_descr;
  190. /* Is the thread joinable?. */
  191. if (th->p_detached || th->p_joining != NULL) {
  192. result = EINVAL;
  193. goto err;
  194. }
  195. if (th == self) {
  196. result = EDEADLK;
  197. goto err;
  198. }
  199. /* Return right away if the thread hasn't terminated yet. */
  200. if (! th->p_terminated) {
  201. result = EBUSY;
  202. goto err;
  203. }
  204. /* Get return value */
  205. if (thread_return != NULL) *thread_return = th->p_retval;
  206. __pthread_unlock(&handle->h_lock);
  207. /* Send notification to thread manager */
  208. if (__pthread_manager_request >= 0) {
  209. request.req_thread = self;
  210. request.req_kind = REQ_FREE;
  211. request.req_args.free.thread_id = thread_id;
  212. TEMP_FAILURE_RETRY(write(__pthread_manager_request,
  213. (char *) &request, sizeof(request)));
  214. }
  215. return 0;
  216. err:
  217. __pthread_unlock(&handle->h_lock);
  218. return result;
  219. }
  220. int pthread_timedjoin_np(pthread_t thread_id, void ** thread_return,
  221. const struct timespec *abstime)
  222. {
  223. volatile pthread_descr self = thread_self();
  224. struct pthread_request request;
  225. pthread_handle handle = thread_handle(thread_id);
  226. pthread_descr th;
  227. pthread_extricate_if extr;
  228. int already_canceled = 0;
  229. int result = 0;
  230. PDEBUG("\n");
  231. /* Set up extrication interface */
  232. extr.pu_object = handle;
  233. extr.pu_extricate_func = join_extricate_func;
  234. __pthread_lock(&handle->h_lock, self);
  235. if (invalid_handle(handle, thread_id)) {
  236. result = ESRCH;
  237. goto err;
  238. }
  239. th = handle->h_descr;
  240. if (th == self) {
  241. result = EDEADLK;
  242. goto err;
  243. }
  244. /* If detached or already joined, error */
  245. if (th->p_detached || th->p_joining != NULL) {
  246. result = EINVAL;
  247. goto err;
  248. }
  249. /* If not terminated yet, suspend ourselves. */
  250. if (! th->p_terminated) {
  251. /* Register extrication interface */
  252. __pthread_set_own_extricate_if(self, &extr);
  253. if (!(THREAD_GETMEM(self, p_canceled)
  254. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  255. th->p_joining = self;
  256. else
  257. already_canceled = 1;
  258. __pthread_unlock(&handle->h_lock);
  259. if (already_canceled) {
  260. __pthread_set_own_extricate_if(self, 0);
  261. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  262. }
  263. PDEBUG("before suspend\n");
  264. result = (timedsuspend(self, abstime) == 0) ? ETIMEDOUT : 0;
  265. PDEBUG("after suspend\n");
  266. /* Deregister extrication interface */
  267. __pthread_set_own_extricate_if(self, 0);
  268. /* This is a cancellation point */
  269. if (result == 0
  270. && THREAD_GETMEM(self, p_woken_by_cancel)
  271. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  272. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  273. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  274. }
  275. __pthread_lock(&handle->h_lock, self);
  276. }
  277. /* We might have timed out. */
  278. if (result == 0) {
  279. /* Get return value */
  280. if (thread_return != NULL) *thread_return = th->p_retval;
  281. }
  282. else
  283. th->p_joining = NULL;
  284. __pthread_unlock(&handle->h_lock);
  285. if (result == 0) {
  286. /* Send notification to thread manager */
  287. if (__pthread_manager_request >= 0) {
  288. request.req_thread = self;
  289. request.req_kind = REQ_FREE;
  290. request.req_args.free.thread_id = thread_id;
  291. TEMP_FAILURE_RETRY(write(__pthread_manager_request,
  292. (char *) &request, sizeof(request)));
  293. }
  294. }
  295. return result;
  296. err:
  297. __pthread_unlock(&handle->h_lock);
  298. return result;
  299. }
  300. int pthread_detach(pthread_t thread_id)
  301. {
  302. int terminated;
  303. struct pthread_request request;
  304. pthread_handle handle = thread_handle(thread_id);
  305. pthread_descr th;
  306. __pthread_lock(&handle->h_lock, NULL);
  307. if (invalid_handle(handle, thread_id)) {
  308. __pthread_unlock(&handle->h_lock);
  309. return ESRCH;
  310. }
  311. th = handle->h_descr;
  312. /* If already detached, error */
  313. if (th->p_detached) {
  314. __pthread_unlock(&handle->h_lock);
  315. return EINVAL;
  316. }
  317. /* If already joining, don't do anything. */
  318. if (th->p_joining != NULL) {
  319. __pthread_unlock(&handle->h_lock);
  320. return 0;
  321. }
  322. /* Mark as detached */
  323. th->p_detached = 1;
  324. terminated = th->p_terminated;
  325. __pthread_unlock(&handle->h_lock);
  326. /* If already terminated, notify thread manager to reclaim resources */
  327. if (terminated && __pthread_manager_request >= 0) {
  328. request.req_thread = thread_self();
  329. request.req_kind = REQ_FREE;
  330. request.req_args.free.thread_id = thread_id;
  331. TEMP_FAILURE_RETRY(write(__pthread_manager_request,
  332. (char *) &request, sizeof(request)));
  333. }
  334. return 0;
  335. }