mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-26 15:00:06 +00:00
00a2f9aa41
1998-08-26 15:46 Ulrich Drepper <drepper@cygnus.com> * internals.h: Define THREAD_GETMEM and THREAD_SETMEM to default if not already defined. (struct _pthread_descr_struct): Add p_self and p_nr field. * manager.c (__pthread_handles): Define second element to point to manager thread. (__pthread_handles_num): Initialize to 2. (__pthread_manager): Use INIT_THREAD_SELF with two arguments. (pthread_start_thread): Likewise. (pthread_handle_create): Start search for free slot at entry 2. Initialize new fields p_self and p_nr. Call __clone with CLONE_PTRACE if available. (pthread_free): Call FREE_THREAD_SELF if available. * pthread.c (__pthread_initial_thread): Initialize new fields. (__pthread_manager_thread): Likewise. (__pthread_initialize_manager): Call __clone with CLONE_PTRACE. * cancel.c: Use THREAD_GETMEM and THREAD_SETMEM to access the elements of the thread descriptor. * condvar.c: Likewise. * errno.c: Likewise. * join.c: Likewise. * manager.c: Likewise. * pthread.c: Likewise. * ptlongjmp.c: Likewise. * semaphore.c: Likewise. * signals.c: Likewise. * specific.c: Likewise. * spinlock.c: Likewise. * sysdeps/alpha/pt-machine.h (INIT_THREAD_SELF): Add extra parameter. * sysdeps/i386/useldt.h: New file. * sysdeps/i386/i686/pt-machine.h: Show how to use this file. * sysdeps/sparc/sparc32/pt-machine.h: Define THREAD_GETMEM and THREAD_SETMEM using __thread_self. * sysdeps/sparc/sparc64/pt-machine.h: Likewise.
147 lines
4.9 KiB
C
147 lines
4.9 KiB
C
/* Linuxthreads - a simple clone()-based implementation of Posix */
|
|
/* threads for Linux. */
|
|
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
|
|
/* */
|
|
/* This program is free software; you can redistribute it and/or */
|
|
/* modify it under the terms of the GNU Library General Public License */
|
|
/* as published by the Free Software Foundation; either version 2 */
|
|
/* of the License, or (at your option) any later version. */
|
|
/* */
|
|
/* This program is distributed in the hope that it will be useful, */
|
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
/* GNU Library General Public License for more details. */
|
|
|
|
/* Thread termination and joining */
|
|
|
|
#include <errno.h>
|
|
#include <sched.h>
|
|
#include <unistd.h>
|
|
#include "pthread.h"
|
|
#include "internals.h"
|
|
#include "spinlock.h"
|
|
#include "restart.h"
|
|
|
|
void pthread_exit(void * retval)
|
|
{
|
|
pthread_descr self = thread_self();
|
|
pthread_descr joining;
|
|
struct pthread_request request;
|
|
|
|
/* Reset the cancellation flag to avoid looping if the cleanup handlers
|
|
contain cancellation points */
|
|
THREAD_SETMEM(self, p_canceled, 0);
|
|
/* Call cleanup functions and destroy the thread-specific data */
|
|
__pthread_perform_cleanup();
|
|
__pthread_destroy_specifics();
|
|
/* Store return value */
|
|
__pthread_lock(THREAD_GETMEM(self, p_lock));
|
|
THREAD_SETMEM(self, p_retval, retval);
|
|
/* Say that we've terminated */
|
|
THREAD_SETMEM(self, p_terminated, 1);
|
|
/* See if someone is joining on us */
|
|
joining = THREAD_GETMEM(self, p_joining);
|
|
__pthread_unlock(THREAD_GETMEM(self, p_lock));
|
|
/* Restart joining thread if any */
|
|
if (joining != NULL) restart(joining);
|
|
/* If this is the initial thread, block until all threads have terminated.
|
|
If another thread calls exit, we'll be terminated from our signal
|
|
handler. */
|
|
if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
|
|
request.req_thread = self;
|
|
request.req_kind = REQ_MAIN_THREAD_EXIT;
|
|
__libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
|
|
suspend(self);
|
|
}
|
|
/* Exit the process (but don't flush stdio streams, and don't run
|
|
atexit functions). */
|
|
_exit(0);
|
|
}
|
|
|
|
int pthread_join(pthread_t thread_id, void ** thread_return)
|
|
{
|
|
volatile pthread_descr self = thread_self();
|
|
struct pthread_request request;
|
|
pthread_handle handle = thread_handle(thread_id);
|
|
pthread_descr th;
|
|
|
|
__pthread_lock(&handle->h_lock);
|
|
if (invalid_handle(handle, thread_id)) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return ESRCH;
|
|
}
|
|
th = handle->h_descr;
|
|
if (th == self) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return EDEADLK;
|
|
}
|
|
/* If detached or already joined, error */
|
|
if (th->p_detached || th->p_joining != NULL) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return EINVAL;
|
|
}
|
|
/* If not terminated yet, suspend ourselves. */
|
|
if (! th->p_terminated) {
|
|
th->p_joining = self;
|
|
__pthread_unlock(&handle->h_lock);
|
|
suspend_with_cancellation(self);
|
|
/* This is a cancellation point */
|
|
if (THREAD_GETMEM(self, p_canceled)
|
|
&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
|
|
th->p_joining = NULL;
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
}
|
|
__pthread_lock(&handle->h_lock);
|
|
}
|
|
/* Get return value */
|
|
if (thread_return != NULL) *thread_return = th->p_retval;
|
|
__pthread_unlock(&handle->h_lock);
|
|
/* Send notification to thread manager */
|
|
if (__pthread_manager_request >= 0) {
|
|
request.req_thread = self;
|
|
request.req_kind = REQ_FREE;
|
|
request.req_args.free.thread_id = thread_id;
|
|
__libc_write(__pthread_manager_request,
|
|
(char *) &request, sizeof(request));
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int pthread_detach(pthread_t thread_id)
|
|
{
|
|
int terminated;
|
|
struct pthread_request request;
|
|
pthread_handle handle = thread_handle(thread_id);
|
|
pthread_descr th;
|
|
|
|
__pthread_lock(&handle->h_lock);
|
|
if (invalid_handle(handle, thread_id)) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return ESRCH;
|
|
}
|
|
th = handle->h_descr;
|
|
/* If already detached, error */
|
|
if (th->p_detached) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return EINVAL;
|
|
}
|
|
/* If already joining, don't do anything. */
|
|
if (th->p_joining != NULL) {
|
|
__pthread_unlock(&handle->h_lock);
|
|
return 0;
|
|
}
|
|
/* Mark as detached */
|
|
th->p_detached = 1;
|
|
terminated = th->p_terminated;
|
|
__pthread_unlock(&handle->h_lock);
|
|
/* If already terminated, notify thread manager to reclaim resources */
|
|
if (terminated && __pthread_manager_request >= 0) {
|
|
request.req_thread = thread_self();
|
|
request.req_kind = REQ_FREE;
|
|
request.req_args.free.thread_id = thread_id;
|
|
__libc_write(__pthread_manager_request,
|
|
(char *) &request, sizeof(request));
|
|
}
|
|
return 0;
|
|
}
|