Update.
[glibc.git] / linuxthreads / join.c
blob71db541391b922d43ff56f13c4bdbaef6ef0c2c4
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 /* Say that we've terminated */
41 THREAD_SETMEM(self, p_terminated, 1);
42 /* See whether we have to signal the death. */
43 if (THREAD_GETMEM(self, p_report_events))
45 /* See whether TD_DEATH is in any of the mask. */
46 int idx = __td_eventword (TD_DEATH);
47 uint32_t mask = __td_eventmask (TD_DEATH);
49 if ((mask & (__pthread_threads_events.event_bits[idx]
50 | THREAD_GETMEM(self,
51 p_eventbuf.eventmask).event_bits[idx]))
52 != 0)
54 /* Yep, we have to signal the death. */
55 THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
56 THREAD_SETMEM(self, p_eventbuf.eventdata, self);
57 __pthread_last_event = self;
59 /* Now call the function to signal the event. */
60 __linuxthreads_death_event();
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);
77 /* Exit the process (but don't flush stdio streams, and don't run
78 atexit functions). */
79 _exit(0);
82 int pthread_join(pthread_t thread_id, void ** thread_return)
84 volatile pthread_descr self = thread_self();
85 struct pthread_request request;
86 pthread_handle handle = thread_handle(thread_id);
87 pthread_descr th;
89 __pthread_lock(&handle->h_lock, self);
90 if (invalid_handle(handle, thread_id)) {
91 __pthread_unlock(&handle->h_lock);
92 return ESRCH;
94 th = handle->h_descr;
95 if (th == self) {
96 __pthread_unlock(&handle->h_lock);
97 return EDEADLK;
99 /* If detached or already joined, error */
100 if (th->p_detached || th->p_joining != NULL) {
101 __pthread_unlock(&handle->h_lock);
102 return EINVAL;
104 /* If not terminated yet, suspend ourselves. */
105 if (! th->p_terminated) {
106 th->p_joining = self;
107 __pthread_unlock(&handle->h_lock);
108 suspend_with_cancellation(self);
109 /* This is a cancellation point */
110 if (THREAD_GETMEM(self, p_canceled)
111 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
112 th->p_joining = NULL;
113 pthread_exit(PTHREAD_CANCELED);
115 __pthread_lock(&handle->h_lock, self);
117 /* Get return value */
118 if (thread_return != NULL) *thread_return = th->p_retval;
119 __pthread_unlock(&handle->h_lock);
120 /* Send notification to thread manager */
121 if (__pthread_manager_request >= 0) {
122 request.req_thread = self;
123 request.req_kind = REQ_FREE;
124 request.req_args.free.thread_id = thread_id;
125 __libc_write(__pthread_manager_request,
126 (char *) &request, sizeof(request));
128 return 0;
131 int pthread_detach(pthread_t thread_id)
133 int terminated;
134 struct pthread_request request;
135 pthread_handle handle = thread_handle(thread_id);
136 pthread_descr th;
138 __pthread_lock(&handle->h_lock, NULL);
139 if (invalid_handle(handle, thread_id)) {
140 __pthread_unlock(&handle->h_lock);
141 return ESRCH;
143 th = handle->h_descr;
144 /* If already detached, error */
145 if (th->p_detached) {
146 __pthread_unlock(&handle->h_lock);
147 return EINVAL;
149 /* If already joining, don't do anything. */
150 if (th->p_joining != NULL) {
151 __pthread_unlock(&handle->h_lock);
152 return 0;
154 /* Mark as detached */
155 th->p_detached = 1;
156 terminated = th->p_terminated;
157 __pthread_unlock(&handle->h_lock);
158 /* If already terminated, notify thread manager to reclaim resources */
159 if (terminated && __pthread_manager_request >= 0) {
160 request.req_thread = thread_self();
161 request.req_kind = REQ_FREE;
162 request.req_args.free.thread_id = thread_id;
163 __libc_write(__pthread_manager_request,
164 (char *) &request, sizeof(request));
166 return 0;