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"
25 #include <not-cancel.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 /* See whether we have to signal the death. */
49 if (THREAD_GETMEM(self
, p_report_events
))
51 /* See whether TD_DEATH is in any of the mask. */
52 int idx
= __td_eventword (TD_DEATH
);
53 uint32_t mask
= __td_eventmask (TD_DEATH
);
55 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
56 | THREAD_GETMEM_NC(self
,
57 p_eventbuf
.eventmask
.event_bits
[idx
])))
60 /* Yep, we have to signal the death. */
61 THREAD_SETMEM(self
, p_eventbuf
.eventnum
, TD_DEATH
);
62 THREAD_SETMEM(self
, p_eventbuf
.eventdata
, self
);
63 __pthread_last_event
= self
;
65 /* Now call the function to signal the event. */
66 __linuxthreads_death_event();
69 /* Say that we've terminated */
70 THREAD_SETMEM(self
, p_terminated
, 1);
71 /* See if someone is joining on us */
72 joining
= THREAD_GETMEM(self
, p_joining
);
73 __pthread_unlock(THREAD_GETMEM(self
, p_lock
));
74 /* Restart joining thread if any */
75 if (joining
!= NULL
) restart(joining
);
76 /* If this is the initial thread, block until all threads have terminated.
77 If another thread calls exit, we'll be terminated from our signal
79 if (self
== __pthread_main_thread
&& __pthread_manager_request
>= 0) {
80 request
.req_thread
= self
;
81 request
.req_kind
= REQ_MAIN_THREAD_EXIT
;
82 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
83 (char *)&request
, sizeof(request
)));
85 /* Main thread flushes stdio streams and runs atexit functions.
86 It also calls a handler within LinuxThreads which sends a process exit
87 request to the thread manager. */
90 /* Threads other than the main one terminate without flushing stdio streams
91 or running atexit functions. */
95 /* Function called by pthread_cancel to remove the thread from
96 waiting on a condition variable queue. */
98 static int join_extricate_func(void *obj
, pthread_descr th
)
100 volatile pthread_descr self
= thread_self();
101 pthread_handle handle
= obj
;
105 __pthread_lock(&handle
->h_lock
, self
);
106 jo
= handle
->h_descr
;
107 did_remove
= jo
->p_joining
!= NULL
;
108 jo
->p_joining
= NULL
;
109 __pthread_unlock(&handle
->h_lock
);
114 int pthread_join(pthread_t thread_id
, void ** thread_return
)
116 volatile pthread_descr self
= thread_self();
117 struct pthread_request request
;
118 pthread_handle handle
= thread_handle(thread_id
);
120 pthread_extricate_if extr
;
121 int already_canceled
= 0;
123 /* Set up extrication interface */
124 extr
.pu_object
= handle
;
125 extr
.pu_extricate_func
= join_extricate_func
;
127 __pthread_lock(&handle
->h_lock
, self
);
128 if (nonexisting_handle(handle
, thread_id
)) {
129 __pthread_unlock(&handle
->h_lock
);
132 th
= handle
->h_descr
;
134 __pthread_unlock(&handle
->h_lock
);
137 /* If detached or already joined, error */
138 if (th
->p_detached
|| th
->p_joining
!= NULL
) {
139 __pthread_unlock(&handle
->h_lock
);
142 /* If not terminated yet, suspend ourselves. */
143 if (! th
->p_terminated
) {
144 /* Register extrication interface */
145 __pthread_set_own_extricate_if(self
, &extr
);
146 if (!(THREAD_GETMEM(self
, p_canceled
)
147 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
148 th
->p_joining
= self
;
150 already_canceled
= 1;
151 __pthread_unlock(&handle
->h_lock
);
153 if (already_canceled
) {
154 __pthread_set_own_extricate_if(self
, 0);
155 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
159 /* Deregister extrication interface */
160 __pthread_set_own_extricate_if(self
, 0);
162 /* This is a cancellation point */
163 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
164 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
165 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
166 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
168 __pthread_lock(&handle
->h_lock
, self
);
170 /* Get return value */
171 if (thread_return
!= NULL
) *thread_return
= th
->p_retval
;
172 __pthread_unlock(&handle
->h_lock
);
173 /* Send notification to thread manager */
174 if (__pthread_manager_request
>= 0) {
175 request
.req_thread
= self
;
176 request
.req_kind
= REQ_FREE
;
177 request
.req_args
.free
.thread_id
= thread_id
;
178 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
179 (char *) &request
, sizeof(request
)));
184 int pthread_detach(pthread_t thread_id
)
187 struct pthread_request request
;
188 pthread_handle handle
= thread_handle(thread_id
);
191 __pthread_lock(&handle
->h_lock
, NULL
);
192 if (nonexisting_handle(handle
, thread_id
)) {
193 __pthread_unlock(&handle
->h_lock
);
196 th
= handle
->h_descr
;
197 /* If already detached, error */
198 if (th
->p_detached
) {
199 __pthread_unlock(&handle
->h_lock
);
202 /* If already joining, don't do anything. */
203 if (th
->p_joining
!= NULL
) {
204 __pthread_unlock(&handle
->h_lock
);
207 /* Mark as detached */
209 terminated
= th
->p_terminated
;
210 __pthread_unlock(&handle
->h_lock
);
211 /* If already terminated, notify thread manager to reclaim resources */
212 if (terminated
&& __pthread_manager_request
>= 0) {
213 request
.req_thread
= thread_self();
214 request
.req_kind
= REQ_FREE
;
215 request
.req_args
.free
.thread_id
= thread_id
;
216 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
217 (char *) &request
, sizeof(request
)));