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
);
30 strong_alias (__pthread_exit
, pthread_exit
);
32 void __pthread_do_exit(void *retval
, char *currentframe
)
34 pthread_descr self
= thread_self();
35 pthread_descr joining
;
36 struct pthread_request request
;
38 /* Reset the cancellation flag to avoid looping if the cleanup handlers
39 contain cancellation points */
40 THREAD_SETMEM(self
, p_canceled
, 0);
41 /* Call cleanup functions and destroy the thread-specific data */
42 __pthread_perform_cleanup(currentframe
);
43 __pthread_destroy_specifics();
44 /* Store return value */
45 __pthread_lock(THREAD_GETMEM(self
, p_lock
), self
);
46 THREAD_SETMEM(self
, p_retval
, retval
);
47 /* See whether we have to signal the death. */
48 if (THREAD_GETMEM(self
, p_report_events
))
50 /* See whether TD_DEATH is in any of the mask. */
51 int idx
= __td_eventword (TD_DEATH
);
52 uint32_t mask
= __td_eventmask (TD_DEATH
);
54 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
55 | THREAD_GETMEM_NC(self
,
56 p_eventbuf
.eventmask
.event_bits
[idx
])))
59 /* Yep, we have to signal the death. */
60 THREAD_SETMEM(self
, p_eventbuf
.eventnum
, TD_DEATH
);
61 THREAD_SETMEM(self
, p_eventbuf
.eventdata
, self
);
62 __pthread_last_event
= self
;
64 /* Now call the function to signal the event. */
65 __linuxthreads_death_event();
68 /* Say that we've terminated */
69 THREAD_SETMEM(self
, p_terminated
, 1);
70 /* See if someone is joining on us */
71 joining
= THREAD_GETMEM(self
, p_joining
);
72 __pthread_unlock(THREAD_GETMEM(self
, p_lock
));
73 /* Restart joining thread if any */
74 if (joining
!= NULL
) restart(joining
);
75 /* If this is the initial thread, block until all threads have terminated.
76 If another thread calls exit, we'll be terminated from our signal
78 if (self
== __pthread_main_thread
&& __pthread_manager_request
>= 0) {
79 request
.req_thread
= self
;
80 request
.req_kind
= REQ_MAIN_THREAD_EXIT
;
81 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
82 (char *)&request
, sizeof(request
)));
84 /* Main thread flushes stdio streams and runs atexit functions.
85 It also calls a handler within LinuxThreads which sends a process exit
86 request to the thread manager. */
89 /* Threads other than the main one terminate without flushing stdio streams
90 or running atexit functions. */
94 /* Function called by pthread_cancel to remove the thread from
95 waiting on a condition variable queue. */
97 static int join_extricate_func(void *obj
, pthread_descr th
)
99 volatile pthread_descr self
= thread_self();
100 pthread_handle handle
= obj
;
104 __pthread_lock(&handle
->h_lock
, self
);
105 jo
= handle
->h_descr
;
106 did_remove
= jo
->p_joining
!= NULL
;
107 jo
->p_joining
= NULL
;
108 __pthread_unlock(&handle
->h_lock
);
113 int pthread_join(pthread_t thread_id
, void ** thread_return
)
115 volatile pthread_descr self
= thread_self();
116 struct pthread_request request
;
117 pthread_handle handle
= thread_handle(thread_id
);
119 pthread_extricate_if extr
;
120 int already_canceled
= 0;
122 /* Set up extrication interface */
123 extr
.pu_object
= handle
;
124 extr
.pu_extricate_func
= join_extricate_func
;
126 __pthread_lock(&handle
->h_lock
, self
);
127 if (nonexisting_handle(handle
, thread_id
)) {
128 __pthread_unlock(&handle
->h_lock
);
131 th
= handle
->h_descr
;
133 __pthread_unlock(&handle
->h_lock
);
136 /* If detached or already joined, error */
137 if (th
->p_detached
|| th
->p_joining
!= NULL
) {
138 __pthread_unlock(&handle
->h_lock
);
141 /* If not terminated yet, suspend ourselves. */
142 if (! th
->p_terminated
) {
143 /* Register extrication interface */
144 __pthread_set_own_extricate_if(self
, &extr
);
145 if (!(THREAD_GETMEM(self
, p_canceled
)
146 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
147 th
->p_joining
= self
;
149 already_canceled
= 1;
150 __pthread_unlock(&handle
->h_lock
);
152 if (already_canceled
) {
153 __pthread_set_own_extricate_if(self
, 0);
154 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
158 /* Deregister extrication interface */
159 __pthread_set_own_extricate_if(self
, 0);
161 /* This is a cancellation point */
162 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
163 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
164 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
165 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
167 __pthread_lock(&handle
->h_lock
, self
);
169 /* Get return value */
170 if (thread_return
!= NULL
) *thread_return
= th
->p_retval
;
171 __pthread_unlock(&handle
->h_lock
);
172 /* Send notification to thread manager */
173 if (__pthread_manager_request
>= 0) {
174 request
.req_thread
= self
;
175 request
.req_kind
= REQ_FREE
;
176 request
.req_args
.free
.thread_id
= thread_id
;
177 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
178 (char *) &request
, sizeof(request
)));
183 int pthread_detach(pthread_t thread_id
)
186 struct pthread_request request
;
187 pthread_handle handle
= thread_handle(thread_id
);
190 __pthread_lock(&handle
->h_lock
, NULL
);
191 if (nonexisting_handle(handle
, thread_id
)) {
192 __pthread_unlock(&handle
->h_lock
);
195 th
= handle
->h_descr
;
196 /* If already detached, error */
197 if (th
->p_detached
) {
198 __pthread_unlock(&handle
->h_lock
);
201 /* If already joining, don't do anything. */
202 if (th
->p_joining
!= NULL
) {
203 __pthread_unlock(&handle
->h_lock
);
206 /* Mark as detached */
208 terminated
= th
->p_terminated
;
209 __pthread_unlock(&handle
->h_lock
);
210 /* If already terminated, notify thread manager to reclaim resources */
211 if (terminated
&& __pthread_manager_request
>= 0) {
212 request
.req_thread
= thread_self();
213 request
.req_kind
= REQ_FREE
;
214 request
.req_args
.free
.thread_id
= thread_id
;
215 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
216 (char *) &request
, sizeof(request
)));