(atomic_add): Don't allow address register for operand 0.
[glibc.git] / linuxthreads / join.c
bloba0cdb41cd3c7f7b20b54d6d2caf3adc563e2588e
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 <stdlib.h>
20 #include <unistd.h>
21 #include "pthread.h"
22 #include "internals.h"
23 #include "spinlock.h"
24 #include "restart.h"
26 void pthread_exit(void * retval)
28 __pthread_do_exit (retval, CURRENT_STACK_FRAME);
31 void __pthread_do_exit(void *retval, char *currentframe)
33 pthread_descr self = thread_self();
34 pthread_descr joining;
35 struct pthread_request request;
37 /* Reset the cancellation flag to avoid looping if the cleanup handlers
38 contain cancellation points */
39 THREAD_SETMEM(self, p_canceled, 0);
40 /* Call cleanup functions and destroy the thread-specific data */
41 __pthread_perform_cleanup(currentframe);
42 __pthread_destroy_specifics();
43 /* Store return value */
44 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
45 THREAD_SETMEM(self, p_retval, retval);
46 /* See whether we have to signal the death. */
47 if (THREAD_GETMEM(self, p_report_events))
49 /* See whether TD_DEATH is in any of the mask. */
50 int idx = __td_eventword (TD_DEATH);
51 uint32_t mask = __td_eventmask (TD_DEATH);
53 if ((mask & (__pthread_threads_events.event_bits[idx]
54 | THREAD_GETMEM_NC(self,
55 p_eventbuf.eventmask.event_bits[idx])))
56 != 0)
58 /* Yep, we have to signal the death. */
59 THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
60 THREAD_SETMEM(self, p_eventbuf.eventdata, self);
61 __pthread_last_event = self;
63 /* Now call the function to signal the event. */
64 __linuxthreads_death_event();
67 /* Say that we've terminated */
68 THREAD_SETMEM(self, p_terminated, 1);
69 /* See if someone is joining on us */
70 joining = THREAD_GETMEM(self, p_joining);
71 __pthread_unlock(THREAD_GETMEM(self, p_lock));
72 /* Restart joining thread if any */
73 if (joining != NULL) restart(joining);
74 /* If this is the initial thread, block until all threads have terminated.
75 If another thread calls exit, we'll be terminated from our signal
76 handler. */
77 if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
78 request.req_thread = self;
79 request.req_kind = REQ_MAIN_THREAD_EXIT;
80 __libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
81 suspend(self);
82 /* Main thread flushes stdio streams and runs atexit functions.
83 It also calls a handler within LinuxThreads which sends a process exit
84 request to the thread manager. */
85 exit(0);
87 /* Threads other than the main one terminate without flushing stdio streams
88 or running atexit functions. */
89 _exit(0);
92 /* Function called by pthread_cancel to remove the thread from
93 waiting on a condition variable queue. */
95 static int join_extricate_func(void *obj, pthread_descr th)
97 volatile pthread_descr self = thread_self();
98 pthread_handle handle = obj;
99 pthread_descr jo;
100 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);
108 return did_remove;
111 int pthread_join(pthread_t thread_id, void ** thread_return)
113 volatile pthread_descr self = thread_self();
114 struct pthread_request request;
115 pthread_handle handle = thread_handle(thread_id);
116 pthread_descr th;
117 pthread_extricate_if extr;
118 int already_canceled = 0;
120 /* Set up extrication interface */
121 extr.pu_object = handle;
122 extr.pu_extricate_func = join_extricate_func;
124 __pthread_lock(&handle->h_lock, self);
125 if (nonexisting_handle(handle, thread_id)) {
126 __pthread_unlock(&handle->h_lock);
127 return ESRCH;
129 th = handle->h_descr;
130 if (th == self) {
131 __pthread_unlock(&handle->h_lock);
132 return EDEADLK;
134 /* If detached or already joined, error */
135 if (th->p_detached || th->p_joining != NULL) {
136 __pthread_unlock(&handle->h_lock);
137 return EINVAL;
139 /* If not terminated yet, suspend ourselves. */
140 if (! th->p_terminated) {
141 /* Register extrication interface */
142 __pthread_set_own_extricate_if(self, &extr);
143 if (!(THREAD_GETMEM(self, p_canceled)
144 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
145 th->p_joining = self;
146 else
147 already_canceled = 1;
148 __pthread_unlock(&handle->h_lock);
150 if (already_canceled) {
151 __pthread_set_own_extricate_if(self, 0);
152 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
155 suspend(self);
156 /* Deregister extrication interface */
157 __pthread_set_own_extricate_if(self, 0);
159 /* This is a cancellation point */
160 if (THREAD_GETMEM(self, p_woken_by_cancel)
161 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
162 THREAD_SETMEM(self, p_woken_by_cancel, 0);
163 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
165 __pthread_lock(&handle->h_lock, self);
167 /* Get return value */
168 if (thread_return != NULL) *thread_return = th->p_retval;
169 __pthread_unlock(&handle->h_lock);
170 /* Send notification to thread manager */
171 if (__pthread_manager_request >= 0) {
172 request.req_thread = self;
173 request.req_kind = REQ_FREE;
174 request.req_args.free.thread_id = thread_id;
175 __libc_write(__pthread_manager_request,
176 (char *) &request, sizeof(request));
178 return 0;
181 int pthread_detach(pthread_t thread_id)
183 int terminated;
184 struct pthread_request request;
185 pthread_handle handle = thread_handle(thread_id);
186 pthread_descr th;
188 __pthread_lock(&handle->h_lock, NULL);
189 if (nonexisting_handle(handle, thread_id)) {
190 __pthread_unlock(&handle->h_lock);
191 return ESRCH;
193 th = handle->h_descr;
194 /* If already detached, error */
195 if (th->p_detached) {
196 __pthread_unlock(&handle->h_lock);
197 return EINVAL;
199 /* If already joining, don't do anything. */
200 if (th->p_joining != NULL) {
201 __pthread_unlock(&handle->h_lock);
202 return 0;
204 /* Mark as detached */
205 th->p_detached = 1;
206 terminated = th->p_terminated;
207 __pthread_unlock(&handle->h_lock);
208 /* If already terminated, notify thread manager to reclaim resources */
209 if (terminated && __pthread_manager_request >= 0) {
210 request.req_thread = thread_self();
211 request.req_kind = REQ_FREE;
212 request.req_args.free.thread_id = thread_id;
213 __libc_write(__pthread_manager_request,
214 (char *) &request, sizeof(request));
216 return 0;