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 */
21 #include "internals.h"
25 #include <l4/util/util.h>
27 void __pthread_exit(void * retval)
29 __pthread_do_exit (retval, CURRENT_STACK_FRAME);
31 strong_alias (__pthread_exit, pthread_exit)
33 void __pthread_do_exit(void *retval, char *currentframe)
35 pthread_descr self = thread_self();
36 pthread_descr joining;
37 struct pthread_request request;
39 /* Reset the cancellation flag to avoid looping if the cleanup handlers
40 contain cancellation points */
41 THREAD_SETMEM(self, p_canceled, 0);
42 /* Call cleanup functions and destroy the thread-specific data */
43 __pthread_perform_cleanup(currentframe);
44 __pthread_destroy_specifics();
45 /* Store return value */
46 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
47 THREAD_SETMEM(self, p_retval, retval);
48 /* Say that we've terminated */
49 THREAD_SETMEM(self, p_terminated, 1);
50 /* See if someone is joining on us */
51 joining = THREAD_GETMEM(self, p_joining);
52 __pthread_unlock(THREAD_GETMEM(self, p_lock));
53 /* Restart joining thread if any */
60 /* If this is the initial thread, block until all threads have terminated.
61 If another thread calls exit, we'll be terminated from our signal
63 if (self == __pthread_main_thread && __pthread_manager_request >= 0)
65 request.req_thread = self;
66 request.req_kind = REQ_MAIN_THREAD_EXIT;
67 __pthread_send_manager_rq(&request, 1);
68 /* Main thread flushes stdio streams and runs atexit functions.
69 It also calls a handler within LinuxThreads which sends a process exit
70 request to the thread manager. */
73 /* Threads other than the main one terminate without flushing stdio streams
74 or running atexit functions. */
78 request.req_thread = self;
79 request.req_kind = REQ_THREAD_EXIT;
80 __pthread_send_manager_rq(&request, 1);
85 /* Function called by pthread_cancel to remove the thread from
86 waiting on a condition variable queue. */
88 static int join_extricate_func(void *obj, pthread_descr th)
90 __volatile__ pthread_descr self = thread_self();
91 pthread_handle handle = obj;
95 __pthread_lock(handle_to_lock(handle), self);
96 jo = handle_to_descr(handle);
97 did_remove = jo->p_joining != NULL;
99 __pthread_unlock(handle_to_lock(handle));
104 int pthread_join(pthread_t thread_id, void ** thread_return)
106 __volatile__ pthread_descr self = thread_self();
107 struct pthread_request request;
108 pthread_handle handle = thread_handle(thread_id);
110 pthread_extricate_if extr;
111 int already_canceled = 0;
113 /* Set up extrication interface */
114 extr.pu_object = handle;
115 extr.pu_extricate_func = join_extricate_func;
117 __pthread_lock(handle_to_lock(handle), self);
118 if (nonexisting_handle(handle, thread_id)) {
119 __pthread_unlock(handle_to_lock(handle));
122 th = handle_to_descr(handle);
124 __pthread_unlock(handle_to_lock(handle));
127 /* If detached or already joined, error */
128 if (th->p_detached || th->p_joining != NULL) {
129 __pthread_unlock(handle_to_lock(handle));
132 /* If not terminated yet, suspend ourselves. */
133 if (! th->p_terminated) {
134 /* Register extrication interface */
135 __pthread_set_own_extricate_if(self, &extr);
136 if (!(THREAD_GETMEM(self, p_canceled)
137 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
138 th->p_joining = self;
140 already_canceled = 1;
141 __pthread_unlock(handle_to_lock(handle));
143 if (already_canceled) {
144 __pthread_set_own_extricate_if(self, 0);
145 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
149 /* Deregister extrication interface */
150 __pthread_set_own_extricate_if(self, 0);
152 /* This is a cancellation point */
153 if (THREAD_GETMEM(self, p_woken_by_cancel)
154 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
155 THREAD_SETMEM(self, p_woken_by_cancel, 0);
156 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
158 __pthread_lock(handle_to_lock(handle), self);
160 /* Get return value */
161 if (thread_return != NULL) *thread_return = th->p_retval;
162 __pthread_unlock(handle_to_lock(handle));
163 /* Send notification to thread manager */
164 if (__pthread_manager_request >= 0) {
165 request.req_thread = self;
166 request.req_kind = REQ_FREE;
167 request.req_args.free.thread_id = thread_id;
168 __pthread_send_manager_rq(&request, 0);
173 int pthread_detach(pthread_t thread_id)
176 struct pthread_request request;
177 pthread_handle handle = thread_handle(thread_id);
180 __pthread_lock(handle_to_lock(handle), NULL);
181 if (nonexisting_handle(handle, thread_id)) {
182 __pthread_unlock(handle_to_lock(handle));
185 th = handle_to_descr(handle);
186 /* If already detached, error */
187 if (th->p_detached) {
188 __pthread_unlock(handle_to_lock(handle));
191 /* If already joining, don't do anything. */
192 if (th->p_joining != NULL) {
193 __pthread_unlock(handle_to_lock(handle));
196 /* Mark as detached */
198 terminated = th->p_terminated;
199 __pthread_unlock(handle_to_lock(handle));
200 /* If already terminated, notify thread manager to reclaim resources */
201 if (terminated && __pthread_manager_request >= 0) {
202 request.req_thread = thread_self();
203 request.req_kind = REQ_FREE;
204 request.req_args.free.thread_id = thread_id;
205 __pthread_send_manager_rq(&request, 0);