1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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. */
15 /* Thread termination and joining */
23 #include "internals.h"
26 #include "debug.h" /* PDEBUG, added by StS */
28 libpthread_hidden_proto(pthread_exit)
29 void pthread_exit(void * retval)
31 __pthread_do_exit (retval, CURRENT_STACK_FRAME);
33 libpthread_hidden_def (pthread_exit)
35 void __pthread_do_exit(void *retval, char *currentframe)
37 pthread_descr self = thread_self();
38 pthread_descr joining;
39 struct pthread_request request;
40 PDEBUG("self=%p, pid=%d\n", self, self->p_pid);
42 /* obey POSIX behavior and prevent cancellation functions from
43 * being called more than once.
44 * http://sourceware.org/ml/libc-ports/2006-10/msg00043.html
46 THREAD_SETMEM(self, p_cancelstate, PTHREAD_CANCEL_DISABLE);
47 THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED);
49 /* Call cleanup functions and destroy the thread-specific data */
50 __pthread_perform_cleanup(currentframe);
51 __pthread_destroy_specifics();
52 /* Store return value */
53 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
54 THREAD_SETMEM(self, p_retval, retval);
55 /* See whether we have to signal the death. */
56 if (THREAD_GETMEM(self, p_report_events))
58 /* See whether TD_DEATH is in any of the mask. */
59 int idx = __td_eventword (TD_DEATH);
60 uint32_t mask = __td_eventmask (TD_DEATH);
62 if ((mask & (__pthread_threads_events.event_bits[idx]
63 | THREAD_GETMEM_NC(self,
64 p_eventbuf.eventmask).event_bits[idx]))
67 /* Yep, we have to signal the death. */
68 THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
69 THREAD_SETMEM(self, p_eventbuf.eventdata, self);
70 __pthread_last_event = self;
72 /* Now call the function to signal the event. */
73 __linuxthreads_death_event();
76 /* Say that we've terminated */
77 THREAD_SETMEM(self, p_terminated, 1);
78 /* See if someone is joining on us */
79 joining = THREAD_GETMEM(self, p_joining);
80 PDEBUG("joining = %p, pid=%d\n", joining, joining ? joining->p_pid : 0);
81 __pthread_unlock(THREAD_GETMEM(self, p_lock));
82 /* Restart joining thread if any */
83 if (joining != NULL) restart(joining);
84 /* If this is the initial thread, block until all threads have terminated.
85 If another thread calls exit, we'll be terminated from our signal
87 if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
88 request.req_thread = self;
89 request.req_kind = REQ_MAIN_THREAD_EXIT;
90 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
91 (char *)&request, sizeof(request)));
93 /* Main thread flushes stdio streams and runs atexit functions.
94 * It also calls a handler within LinuxThreads which sends a process exit
95 * request to the thread manager. */
98 /* Exit the process (but don't flush stdio streams, and don't run
103 /* Function called by pthread_cancel to remove the thread from
104 waiting on a condition variable queue. */
106 static int join_extricate_func(void *obj, pthread_descr th attribute_unused)
108 volatile pthread_descr self = thread_self();
109 pthread_handle handle = obj;
113 __pthread_lock(&handle->h_lock, self);
114 jo = handle->h_descr;
115 did_remove = jo->p_joining != NULL;
116 jo->p_joining = NULL;
117 __pthread_unlock(&handle->h_lock);
122 int pthread_join(pthread_t thread_id, void ** thread_return)
124 volatile pthread_descr self = thread_self();
125 struct pthread_request request;
126 pthread_handle handle = thread_handle(thread_id);
128 pthread_extricate_if extr;
129 int already_canceled = 0;
132 /* Set up extrication interface */
133 extr.pu_object = handle;
134 extr.pu_extricate_func = join_extricate_func;
136 __pthread_lock(&handle->h_lock, self);
137 if (invalid_handle(handle, thread_id)) {
138 __pthread_unlock(&handle->h_lock);
141 th = handle->h_descr;
143 __pthread_unlock(&handle->h_lock);
146 /* If detached or already joined, error */
147 if (th->p_detached || th->p_joining != NULL) {
148 __pthread_unlock(&handle->h_lock);
151 /* If not terminated yet, suspend ourselves. */
152 if (! th->p_terminated) {
153 /* Register extrication interface */
154 __pthread_set_own_extricate_if(self, &extr);
155 if (!(THREAD_GETMEM(self, p_canceled)
156 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
157 th->p_joining = self;
159 already_canceled = 1;
160 __pthread_unlock(&handle->h_lock);
162 if (already_canceled) {
163 __pthread_set_own_extricate_if(self, 0);
164 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
167 PDEBUG("before suspend\n");
169 PDEBUG("after suspend\n");
170 /* Deregister extrication interface */
171 __pthread_set_own_extricate_if(self, 0);
173 /* This is a cancellation point */
174 if (THREAD_GETMEM(self, p_woken_by_cancel)
175 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
176 THREAD_SETMEM(self, p_woken_by_cancel, 0);
177 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
179 __pthread_lock(&handle->h_lock, self);
181 /* Get return value */
182 if (thread_return != NULL) *thread_return = th->p_retval;
183 __pthread_unlock(&handle->h_lock);
184 /* Send notification to thread manager */
185 if (__pthread_manager_request >= 0) {
186 request.req_thread = self;
187 request.req_kind = REQ_FREE;
188 request.req_args.free.thread_id = thread_id;
189 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
190 (char *) &request, sizeof(request)));
195 int pthread_detach(pthread_t thread_id)
198 struct pthread_request request;
199 pthread_handle handle = thread_handle(thread_id);
202 __pthread_lock(&handle->h_lock, NULL);
203 if (invalid_handle(handle, thread_id)) {
204 __pthread_unlock(&handle->h_lock);
207 th = handle->h_descr;
208 /* If already detached, error */
209 if (th->p_detached) {
210 __pthread_unlock(&handle->h_lock);
213 /* If already joining, don't do anything. */
214 if (th->p_joining != NULL) {
215 __pthread_unlock(&handle->h_lock);
218 /* Mark as detached */
220 terminated = th->p_terminated;
221 __pthread_unlock(&handle->h_lock);
222 /* If already terminated, notify thread manager to reclaim resources */
223 if (terminated && __pthread_manager_request >= 0) {
224 request.req_thread = thread_self();
225 request.req_kind = REQ_FREE;
226 request.req_args.free.thread_id = thread_id;
227 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
228 (char *) &request, sizeof(request)));