| 1 |
1325 |
phoenix |
/* 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. */
|
| 14 |
|
|
|
| 15 |
|
|
/* Thread termination and joining */
|
| 16 |
|
|
|
| 17 |
|
|
#include <features.h>
|
| 18 |
|
|
#define __USE_GNU
|
| 19 |
|
|
#include <errno.h>
|
| 20 |
|
|
#include <sched.h>
|
| 21 |
|
|
#include <unistd.h>
|
| 22 |
|
|
#include <stdlib.h>
|
| 23 |
|
|
#include "pthread.h"
|
| 24 |
|
|
#include "internals.h"
|
| 25 |
|
|
#include "spinlock.h"
|
| 26 |
|
|
#include "restart.h"
|
| 27 |
|
|
#include "debug.h" /* PDEBUG, added by StS */
|
| 28 |
|
|
|
| 29 |
|
|
void pthread_exit(void * retval)
|
| 30 |
|
|
{
|
| 31 |
|
|
pthread_descr self = thread_self();
|
| 32 |
|
|
pthread_descr joining;
|
| 33 |
|
|
struct pthread_request request;
|
| 34 |
|
|
PDEBUG("self=%p, pid=%d\n", self, self->p_pid);
|
| 35 |
|
|
|
| 36 |
|
|
/* Reset the cancellation flag to avoid looping if the cleanup handlers
|
| 37 |
|
|
contain cancellation points */
|
| 38 |
|
|
THREAD_SETMEM(self, p_canceled, 0);
|
| 39 |
|
|
/* Call cleanup functions and destroy the thread-specific data */
|
| 40 |
|
|
__pthread_perform_cleanup();
|
| 41 |
|
|
__pthread_destroy_specifics();
|
| 42 |
|
|
/* Store return value */
|
| 43 |
|
|
__pthread_lock(THREAD_GETMEM(self, p_lock), self);
|
| 44 |
|
|
THREAD_SETMEM(self, p_retval, retval);
|
| 45 |
|
|
/* Say that we've terminated */
|
| 46 |
|
|
THREAD_SETMEM(self, p_terminated, 1);
|
| 47 |
|
|
/* See whether we have to signal the death. */
|
| 48 |
|
|
if (THREAD_GETMEM(self, p_report_events))
|
| 49 |
|
|
{
|
| 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);
|
| 53 |
|
|
|
| 54 |
|
|
if ((mask & (__pthread_threads_events.event_bits[idx]
|
| 55 |
|
|
| THREAD_GETMEM(self,
|
| 56 |
|
|
p_eventbuf.eventmask).event_bits[idx]))
|
| 57 |
|
|
!= 0)
|
| 58 |
|
|
{
|
| 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;
|
| 63 |
|
|
|
| 64 |
|
|
/* Now call the function to signal the event. */
|
| 65 |
|
|
__linuxthreads_death_event();
|
| 66 |
|
|
}
|
| 67 |
|
|
}
|
| 68 |
|
|
/* See if someone is joining on us */
|
| 69 |
|
|
joining = THREAD_GETMEM(self, p_joining);
|
| 70 |
|
|
PDEBUG("joining = %p, pid=%d\n", joining, joining->p_pid);
|
| 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
|
| 76 |
|
|
handler. */
|
| 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)));
|
| 82 |
|
|
suspend(self);
|
| 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. */
|
| 86 |
|
|
exit(0);
|
| 87 |
|
|
}
|
| 88 |
|
|
/* Exit the process (but don't flush stdio streams, and don't run
|
| 89 |
|
|
atexit functions). */
|
| 90 |
|
|
_exit(0);
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
/* Function called by pthread_cancel to remove the thread from
|
| 94 |
|
|
waiting on a condition variable queue. */
|
| 95 |
|
|
|
| 96 |
|
|
static int join_extricate_func(void *obj, pthread_descr th)
|
| 97 |
|
|
{
|
| 98 |
|
|
volatile pthread_descr self = thread_self();
|
| 99 |
|
|
pthread_handle handle = obj;
|
| 100 |
|
|
pthread_descr jo;
|
| 101 |
|
|
int did_remove = 0;
|
| 102 |
|
|
|
| 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);
|
| 108 |
|
|
|
| 109 |
|
|
return did_remove;
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
int pthread_join(pthread_t thread_id, void ** thread_return)
|
| 113 |
|
|
{
|
| 114 |
|
|
volatile pthread_descr self = thread_self();
|
| 115 |
|
|
struct pthread_request request;
|
| 116 |
|
|
pthread_handle handle = thread_handle(thread_id);
|
| 117 |
|
|
pthread_descr th;
|
| 118 |
|
|
pthread_extricate_if extr;
|
| 119 |
|
|
int already_canceled = 0;
|
| 120 |
|
|
PDEBUG("\n");
|
| 121 |
|
|
|
| 122 |
|
|
/* Set up extrication interface */
|
| 123 |
|
|
extr.pu_object = handle;
|
| 124 |
|
|
extr.pu_extricate_func = join_extricate_func;
|
| 125 |
|
|
|
| 126 |
|
|
__pthread_lock(&handle->h_lock, self);
|
| 127 |
|
|
if (invalid_handle(handle, thread_id)) {
|
| 128 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 129 |
|
|
return ESRCH;
|
| 130 |
|
|
}
|
| 131 |
|
|
th = handle->h_descr;
|
| 132 |
|
|
if (th == self) {
|
| 133 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 134 |
|
|
return EDEADLK;
|
| 135 |
|
|
}
|
| 136 |
|
|
/* If detached or already joined, error */
|
| 137 |
|
|
if (th->p_detached || th->p_joining != NULL) {
|
| 138 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 139 |
|
|
return EINVAL;
|
| 140 |
|
|
}
|
| 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;
|
| 148 |
|
|
else
|
| 149 |
|
|
already_canceled = 1;
|
| 150 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 151 |
|
|
|
| 152 |
|
|
if (already_canceled) {
|
| 153 |
|
|
__pthread_set_own_extricate_if(self, 0);
|
| 154 |
|
|
pthread_exit(PTHREAD_CANCELED);
|
| 155 |
|
|
}
|
| 156 |
|
|
|
| 157 |
|
|
PDEBUG("before suspend\n");
|
| 158 |
|
|
suspend(self);
|
| 159 |
|
|
PDEBUG("after suspend\n");
|
| 160 |
|
|
/* Deregister extrication interface */
|
| 161 |
|
|
__pthread_set_own_extricate_if(self, 0);
|
| 162 |
|
|
|
| 163 |
|
|
/* This is a cancellation point */
|
| 164 |
|
|
if (THREAD_GETMEM(self, p_woken_by_cancel)
|
| 165 |
|
|
&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
|
| 166 |
|
|
THREAD_SETMEM(self, p_woken_by_cancel, 0);
|
| 167 |
|
|
pthread_exit(PTHREAD_CANCELED);
|
| 168 |
|
|
}
|
| 169 |
|
|
__pthread_lock(&handle->h_lock, self);
|
| 170 |
|
|
}
|
| 171 |
|
|
/* Get return value */
|
| 172 |
|
|
if (thread_return != NULL) *thread_return = th->p_retval;
|
| 173 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 174 |
|
|
/* Send notification to thread manager */
|
| 175 |
|
|
if (__pthread_manager_request >= 0) {
|
| 176 |
|
|
request.req_thread = self;
|
| 177 |
|
|
request.req_kind = REQ_FREE;
|
| 178 |
|
|
request.req_args.free.thread_id = thread_id;
|
| 179 |
|
|
TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
|
| 180 |
|
|
(char *) &request, sizeof(request)));
|
| 181 |
|
|
}
|
| 182 |
|
|
return 0;
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
int pthread_detach(pthread_t thread_id)
|
| 186 |
|
|
{
|
| 187 |
|
|
int terminated;
|
| 188 |
|
|
struct pthread_request request;
|
| 189 |
|
|
pthread_handle handle = thread_handle(thread_id);
|
| 190 |
|
|
pthread_descr th;
|
| 191 |
|
|
|
| 192 |
|
|
__pthread_lock(&handle->h_lock, NULL);
|
| 193 |
|
|
if (invalid_handle(handle, thread_id)) {
|
| 194 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 195 |
|
|
return ESRCH;
|
| 196 |
|
|
}
|
| 197 |
|
|
th = handle->h_descr;
|
| 198 |
|
|
/* If already detached, error */
|
| 199 |
|
|
if (th->p_detached) {
|
| 200 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 201 |
|
|
return EINVAL;
|
| 202 |
|
|
}
|
| 203 |
|
|
/* If already joining, don't do anything. */
|
| 204 |
|
|
if (th->p_joining != NULL) {
|
| 205 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 206 |
|
|
return 0;
|
| 207 |
|
|
}
|
| 208 |
|
|
/* Mark as detached */
|
| 209 |
|
|
th->p_detached = 1;
|
| 210 |
|
|
terminated = th->p_terminated;
|
| 211 |
|
|
__pthread_unlock(&handle->h_lock);
|
| 212 |
|
|
/* If already terminated, notify thread manager to reclaim resources */
|
| 213 |
|
|
if (terminated && __pthread_manager_request >= 0) {
|
| 214 |
|
|
request.req_thread = thread_self();
|
| 215 |
|
|
request.req_kind = REQ_FREE;
|
| 216 |
|
|
request.req_args.free.thread_id = thread_id;
|
| 217 |
|
|
TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
|
| 218 |
|
|
(char *) &request, sizeof(request)));
|
| 219 |
|
|
}
|
| 220 |
|
|
return 0;
|
| 221 |
|
|
}
|