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 */
22 #include "internals.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
])))
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
77 if (self
== __pthread_main_thread
&& __pthread_manager_request
>= 0) {
78 request
.req_thread
= self
;
79 request
.req_kind
= REQ_MAIN_THREAD_EXIT
;
80 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
81 (char *)&request
, sizeof(request
)));
83 /* Main thread flushes stdio streams and runs atexit functions.
84 It also calls a handler within LinuxThreads which sends a process exit
85 request to the thread manager. */
88 /* Threads other than the main one terminate without flushing stdio streams
89 or running atexit functions. */
93 /* Function called by pthread_cancel to remove the thread from
94 waiting on a condition variable queue. */
96 static int join_extricate_func(void *obj
, pthread_descr th
)
98 volatile pthread_descr self
= thread_self();
99 pthread_handle handle
= obj
;
103 __pthread_lock(&handle
->h_lock
, self
);
104 jo
= handle
->h_descr
;
105 did_remove
= jo
->p_joining
!= NULL
;
106 jo
->p_joining
= NULL
;
107 __pthread_unlock(&handle
->h_lock
);
112 int pthread_join(pthread_t thread_id
, void ** thread_return
)
114 volatile pthread_descr self
= thread_self();
115 struct pthread_request request
;
116 pthread_handle handle
= thread_handle(thread_id
);
118 pthread_extricate_if extr
;
119 int already_canceled
= 0;
121 /* Set up extrication interface */
122 extr
.pu_object
= handle
;
123 extr
.pu_extricate_func
= join_extricate_func
;
125 __pthread_lock(&handle
->h_lock
, self
);
126 if (nonexisting_handle(handle
, thread_id
)) {
127 __pthread_unlock(&handle
->h_lock
);
130 th
= handle
->h_descr
;
132 __pthread_unlock(&handle
->h_lock
);
135 /* If detached or already joined, error */
136 if (th
->p_detached
|| th
->p_joining
!= NULL
) {
137 __pthread_unlock(&handle
->h_lock
);
140 /* If not terminated yet, suspend ourselves. */
141 if (! th
->p_terminated
) {
142 /* Register extrication interface */
143 __pthread_set_own_extricate_if(self
, &extr
);
144 if (!(THREAD_GETMEM(self
, p_canceled
)
145 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
146 th
->p_joining
= self
;
148 already_canceled
= 1;
149 __pthread_unlock(&handle
->h_lock
);
151 if (already_canceled
) {
152 __pthread_set_own_extricate_if(self
, 0);
153 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
157 /* Deregister extrication interface */
158 __pthread_set_own_extricate_if(self
, 0);
160 /* This is a cancellation point */
161 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
162 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
163 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
164 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
166 __pthread_lock(&handle
->h_lock
, self
);
168 /* Get return value */
169 if (thread_return
!= NULL
) *thread_return
= th
->p_retval
;
170 __pthread_unlock(&handle
->h_lock
);
171 /* Send notification to thread manager */
172 if (__pthread_manager_request
>= 0) {
173 request
.req_thread
= self
;
174 request
.req_kind
= REQ_FREE
;
175 request
.req_args
.free
.thread_id
= thread_id
;
176 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
177 (char *) &request
, sizeof(request
)));
182 int pthread_detach(pthread_t thread_id
)
185 struct pthread_request request
;
186 pthread_handle handle
= thread_handle(thread_id
);
189 __pthread_lock(&handle
->h_lock
, NULL
);
190 if (nonexisting_handle(handle
, thread_id
)) {
191 __pthread_unlock(&handle
->h_lock
);
194 th
= handle
->h_descr
;
195 /* If already detached, error */
196 if (th
->p_detached
) {
197 __pthread_unlock(&handle
->h_lock
);
200 /* If already joining, don't do anything. */
201 if (th
->p_joining
!= NULL
) {
202 __pthread_unlock(&handle
->h_lock
);
205 /* Mark as detached */
207 terminated
= th
->p_terminated
;
208 __pthread_unlock(&handle
->h_lock
);
209 /* If already terminated, notify thread manager to reclaim resources */
210 if (terminated
&& __pthread_manager_request
>= 0) {
211 request
.req_thread
= thread_self();
212 request
.req_kind
= REQ_FREE
;
213 request
.req_args
.free
.thread_id
= thread_id
;
214 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
215 (char *) &request
, sizeof(request
)));