Update.
[glibc.git] / linuxthreads / join.c
blob95c0ab69632374b817ee68bc19e0bac73c297fcb
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. */
15 /* Thread termination and joining */
17 #include <errno.h>
18 #include <sched.h>
19 #include <unistd.h>
20 #include "pthread.h"
21 #include "internals.h"
22 #include "spinlock.h"
23 #include "restart.h"
25 void pthread_exit(void * retval)
27 pthread_descr self = thread_self();
28 pthread_descr joining;
29 struct pthread_request request;
31 /* Reset the cancellation flag to avoid looping if the cleanup handlers
32 contain cancellation points */
33 THREAD_SETMEM(self, p_canceled, 0);
34 /* Call cleanup functions and destroy the thread-specific data */
35 __pthread_perform_cleanup();
36 __pthread_destroy_specifics();
37 /* Store return value */
38 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
39 THREAD_SETMEM(self, p_retval, retval);
40 /* See whether we have to signal the death. */
41 if (THREAD_GETMEM(self, p_report_events))
43 /* See whether TD_DEATH is in any of the mask. */
44 int idx = __td_eventword (TD_DEATH);
45 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)
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;
57 /* Now call the function to signal the event. */
58 __linuxthreads_death_event();
61 /* Say that we've terminated */
62 THREAD_SETMEM(self, p_terminated, 1);
63 /* See if someone is joining on us */
64 joining = THREAD_GETMEM(self, p_joining);
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 __libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
75 suspend(self);
76 /* Main thread flushes stdio streams and runs atexit functions.
77 It also calls a handler within LinuxThreads which sends a process exit
78 request to the thread manager. */
79 exit(0);
81 /* Threads other than the main one terminate without flushing stdio streams
82 or running atexit functions. */
83 _exit(0);
86 /* Function called by pthread_cancel to remove the thread from
87 waiting on a condition variable queue. */
89 static int join_extricate_func(void *obj, pthread_descr th)
91 volatile pthread_descr self = thread_self();
92 pthread_handle handle = obj;
93 pthread_descr jo;
94 int did_remove = 0;
96 __pthread_lock(&handle->h_lock, self);
97 jo = handle->h_descr;
98 did_remove = jo->p_joining != NULL;
99 jo->p_joining = NULL;
100 __pthread_unlock(&handle->h_lock);
102 return did_remove;
105 int pthread_join(pthread_t thread_id, void ** thread_return)
107 volatile pthread_descr self = thread_self();
108 struct pthread_request request;
109 pthread_handle handle = thread_handle(thread_id);
110 pthread_descr th;
111 pthread_extricate_if extr;
112 int already_canceled = 0;
114 /* Set up extrication interface */
115 extr.pu_object = handle;
116 extr.pu_extricate_func = join_extricate_func;
118 __pthread_lock(&handle->h_lock, self);
119 if (invalid_handle(handle, thread_id)) {
120 __pthread_unlock(&handle->h_lock);
121 return ESRCH;
123 th = handle->h_descr;
124 if (th == self) {
125 __pthread_unlock(&handle->h_lock);
126 return EDEADLK;
128 /* If detached or already joined, error */
129 if (th->p_detached || th->p_joining != NULL) {
130 __pthread_unlock(&handle->h_lock);
131 return EINVAL;
133 /* If not terminated yet, suspend ourselves. */
134 if (! th->p_terminated) {
135 /* Register extrication interface */
136 __pthread_set_own_extricate_if(self, &extr);
137 if (!(THREAD_GETMEM(self, p_canceled)
138 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
139 th->p_joining = self;
140 else
141 already_canceled = 1;
142 __pthread_unlock(&handle->h_lock);
144 if (already_canceled) {
145 __pthread_set_own_extricate_if(self, 0);
146 pthread_exit(PTHREAD_CANCELED);
149 suspend(self);
150 /* Deregister extrication interface */
151 __pthread_set_own_extricate_if(self, 0);
153 /* This is a cancellation point */
154 if (THREAD_GETMEM(self, p_woken_by_cancel)
155 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
156 THREAD_SETMEM(self, p_woken_by_cancel, 0);
157 pthread_exit(PTHREAD_CANCELED);
159 __pthread_lock(&handle->h_lock, self);
161 /* Get return value */
162 if (thread_return != NULL) *thread_return = th->p_retval;
163 __pthread_unlock(&handle->h_lock);
164 /* Send notification to thread manager */
165 if (__pthread_manager_request >= 0) {
166 request.req_thread = self;
167 request.req_kind = REQ_FREE;
168 request.req_args.free.thread_id = thread_id;
169 __libc_write(__pthread_manager_request,
170 (char *) &request, sizeof(request));
172 return 0;
175 int pthread_detach(pthread_t thread_id)
177 int terminated;
178 struct pthread_request request;
179 pthread_handle handle = thread_handle(thread_id);
180 pthread_descr th;
182 __pthread_lock(&handle->h_lock, NULL);
183 if (invalid_handle(handle, thread_id)) {
184 __pthread_unlock(&handle->h_lock);
185 return ESRCH;
187 th = handle->h_descr;
188 /* If already detached, error */
189 if (th->p_detached) {
190 __pthread_unlock(&handle->h_lock);
191 return EINVAL;
193 /* If already joining, don't do anything. */
194 if (th->p_joining != NULL) {
195 __pthread_unlock(&handle->h_lock);
196 return 0;
198 /* Mark as detached */
199 th->p_detached = 1;
200 terminated = th->p_terminated;
201 __pthread_unlock(&handle->h_lock);
202 /* If already terminated, notify thread manager to reclaim resources */
203 if (terminated && __pthread_manager_request >= 0) {
204 request.req_thread = thread_self();
205 request.req_kind = REQ_FREE;
206 request.req_args.free.thread_id = thread_id;
207 __libc_write(__pthread_manager_request,
208 (char *) &request, sizeof(request));
210 return 0;