Add pthread_detach(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-10 15:43:16 +01:00
parent 14ca218453
commit cc643bd751
6 changed files with 42 additions and 1 deletions

View File

@ -28,6 +28,7 @@ pthread_cond_signal.o \
pthread_cond_timedwait.o \
pthread_cond_wait.o \
pthread_create.o \
pthread_detach.o \
pthread_equal.o \
pthread_exit.o \
pthread_getspecific.o \

View File

@ -139,6 +139,7 @@ struct pthread
{
struct uthread uthread;
pthread_mutex_t join_lock;
pthread_mutex_t detach_lock;
void* (*entry_function)(void*);
void* entry_cookie;
void* exit_result;
@ -237,7 +238,7 @@ int pthread_create(pthread_t* __restrict,
const pthread_attr_t* __restrict,
void* (*)(void*),
void* __restrict);
/* TODO: pthread_detach */
int pthread_detach(pthread_t);
int pthread_equal(pthread_t, pthread_t);
__attribute__((__noreturn__))
void pthread_exit(void*);

View File

@ -207,6 +207,7 @@ int pthread_create(pthread_t* restrict thread_ptr,
thread->join_lock.lock = 1 /* LOCKED_VALUE */;
thread->join_lock.type = PTHREAD_MUTEX_NORMAL;
thread->join_lock.owner = (unsigned long) thread;
thread->detach_lock = PTHREAD_NORMAL_MUTEX_INITIALIZER_NP;
thread->detach_state = attr->detach_state;
thread->entry_function = entry_function;
thread->entry_cookie = entry_cookie;

View File

@ -0,0 +1,36 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014.
This file is part of Sortix libpthread.
Sortix libpthread is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Sortix libpthread 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Sortix libpthread. If not, see <http://www.gnu.org/licenses/>.
pthread_detach.c++
Detach a thread.
*******************************************************************************/
#include <assert.h>
#include <pthread.h>
extern "C" int pthread_detach(pthread_t thread)
{
if ( pthread_mutex_trylock(&thread->detach_lock) != 0 )
return pthread_join(thread, NULL);
assert(thread->detach_state == PTHREAD_CREATE_JOINABLE);
thread->detach_state = PTHREAD_CREATE_DETACHED;
pthread_mutex_unlock(&thread->detach_lock);
return 0;
}

View File

@ -61,6 +61,7 @@ void pthread_exit(void* return_value)
pthread_mutex_unlock(&__pthread_num_threads_lock);
if ( num_threads == 1 )
exit(0);
pthread_mutex_lock(&thread->detach_lock);
thread->exit_result = return_value;
struct exit_thread extended;
memset(&extended, 0, sizeof(extended));

View File

@ -78,5 +78,6 @@ extern "C" void pthread_initialize(void)
self->join_lock.lock = 1 /* LOCKED_VALUE */;
self->join_lock.type = PTHREAD_MUTEX_NORMAL;
self->join_lock.owner = (unsigned long) self;
self->detach_lock = PTHREAD_NORMAL_MUTEX_INITIALIZER_NP;
self->detach_state = PTHREAD_CREATE_JOINABLE;
}