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_descr self
= thread_self();
29 pthread_descr joining
;
30 struct pthread_request request
;
32 /* Reset the cancellation flag to avoid looping if the cleanup handlers
33 contain cancellation points */
34 THREAD_SETMEM(self
, p_canceled
, 0);
35 /* Call cleanup functions and destroy the thread-specific data */
36 __pthread_perform_cleanup();
37 __pthread_destroy_specifics();
38 /* Store return value */
39 __pthread_lock(THREAD_GETMEM(self
, p_lock
), self
);
40 THREAD_SETMEM(self
, p_retval
, retval
);
41 /* See whether we have to signal the death. */
42 if (THREAD_GETMEM(self
, p_report_events
))
44 /* See whether TD_DEATH is in any of the mask. */
45 int idx
= __td_eventword (TD_DEATH
);
46 uint32_t mask
= __td_eventmask (TD_DEATH
);
48 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
49 | THREAD_GETMEM_NC(self
,
50 p_eventbuf
.eventmask
.event_bits
[idx
])))
53 /* Yep, we have to signal the death. */
54 THREAD_SETMEM(self
, p_eventbuf
.eventnum
, TD_DEATH
);
55 THREAD_SETMEM(self
, p_eventbuf
.eventdata
, self
);
56 __pthread_last_event
= self
;
58 /* Now call the function to signal the event. */
59 __linuxthreads_death_event();
62 /* Say that we've terminated */
63 THREAD_SETMEM(self
, p_terminated
, 1);
64 /* See if someone is joining on us */
65 joining
= THREAD_GETMEM(self
, p_joining
);
66 __pthread_unlock(THREAD_GETMEM(self
, p_lock
));
67 /* Restart joining thread if any */
68 if (joining
!= NULL
) restart(joining
);
69 /* If this is the initial thread, block until all threads have terminated.
70 If another thread calls exit, we'll be terminated from our signal
72 if (self
== __pthread_main_thread
&& __pthread_manager_request
>= 0) {
73 request
.req_thread
= self
;
74 request
.req_kind
= REQ_MAIN_THREAD_EXIT
;
75 __libc_write(__pthread_manager_request
, (char *)&request
, sizeof(request
));
77 /* Main thread flushes stdio streams and runs atexit functions.
78 It also calls a handler within LinuxThreads which sends a process exit
79 request to the thread manager. */
82 /* Threads other than the main one terminate without flushing stdio streams
83 or running atexit functions. */
87 /* Function called by pthread_cancel to remove the thread from
88 waiting on a condition variable queue. */
90 static int join_extricate_func(void *obj
, pthread_descr th
)
92 volatile pthread_descr self
= thread_self();
93 pthread_handle handle
= obj
;
97 __pthread_lock(&handle
->h_lock
, self
);
99 did_remove
= jo
->p_joining
!= NULL
;
100 jo
->p_joining
= NULL
;
101 __pthread_unlock(&handle
->h_lock
);
106 int pthread_join(pthread_t thread_id
, void ** thread_return
)
108 volatile pthread_descr self
= thread_self();
109 struct pthread_request request
;
110 pthread_handle handle
= thread_handle(thread_id
);
112 pthread_extricate_if extr
;
113 int already_canceled
= 0;
115 /* Set up extrication interface */
116 extr
.pu_object
= handle
;
117 extr
.pu_extricate_func
= join_extricate_func
;
119 __pthread_lock(&handle
->h_lock
, self
);
120 if (nonexisting_handle(handle
, thread_id
)) {
121 __pthread_unlock(&handle
->h_lock
);
124 th
= handle
->h_descr
;
126 __pthread_unlock(&handle
->h_lock
);
129 /* If detached or already joined, error */
130 if (th
->p_detached
|| th
->p_joining
!= NULL
) {
131 __pthread_unlock(&handle
->h_lock
);
134 /* If not terminated yet, suspend ourselves. */
135 if (! th
->p_terminated
) {
136 /* Register extrication interface */
137 __pthread_set_own_extricate_if(self
, &extr
);
138 if (!(THREAD_GETMEM(self
, p_canceled
)
139 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
140 th
->p_joining
= self
;
142 already_canceled
= 1;
143 __pthread_unlock(&handle
->h_lock
);
145 if (already_canceled
) {
146 __pthread_set_own_extricate_if(self
, 0);
147 pthread_exit(PTHREAD_CANCELED
);
151 /* Deregister extrication interface */
152 __pthread_set_own_extricate_if(self
, 0);
154 /* This is a cancellation point */
155 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
156 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
157 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
158 pthread_exit(PTHREAD_CANCELED
);
160 __pthread_lock(&handle
->h_lock
, self
);
162 /* Get return value */
163 if (thread_return
!= NULL
) *thread_return
= th
->p_retval
;
164 __pthread_unlock(&handle
->h_lock
);
165 /* Send notification to thread manager */
166 if (__pthread_manager_request
>= 0) {
167 request
.req_thread
= self
;
168 request
.req_kind
= REQ_FREE
;
169 request
.req_args
.free
.thread_id
= thread_id
;
170 __libc_write(__pthread_manager_request
,
171 (char *) &request
, sizeof(request
));
176 int pthread_detach(pthread_t thread_id
)
179 struct pthread_request request
;
180 pthread_handle handle
= thread_handle(thread_id
);
183 __pthread_lock(&handle
->h_lock
, NULL
);
184 if (invalid_handle(handle
, thread_id
)) {
185 __pthread_unlock(&handle
->h_lock
);
188 th
= handle
->h_descr
;
189 /* If already detached, error */
190 if (th
->p_detached
) {
191 __pthread_unlock(&handle
->h_lock
);
194 /* If already joining, don't do anything. */
195 if (th
->p_joining
!= NULL
) {
196 __pthread_unlock(&handle
->h_lock
);
199 /* Mark as detached */
201 terminated
= th
->p_terminated
;
202 __pthread_unlock(&handle
->h_lock
);
203 /* If already terminated, notify thread manager to reclaim resources */
204 if (terminated
&& __pthread_manager_request
>= 0) {
205 request
.req_thread
= thread_self();
206 request
.req_kind
= REQ_FREE
;
207 request
.req_args
.free
.thread_id
= thread_id
;
208 __libc_write(__pthread_manager_request
,
209 (char *) &request
, sizeof(request
));