From cc643bd751c36ce92a8bf070b8e5a98c21a9e39a Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 10 Jan 2014 15:43:16 +0100 Subject: [PATCH] Add pthread_detach(3). --- libpthread/Makefile | 1 + libpthread/include/pthread.h | 3 ++- libpthread/pthread_create.c++ | 1 + libpthread/pthread_detach.c++ | 36 +++++++++++++++++++++++++++++++ libpthread/pthread_exit.c++ | 1 + libpthread/pthread_initialize.c++ | 1 + 6 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 libpthread/pthread_detach.c++ diff --git a/libpthread/Makefile b/libpthread/Makefile index ff7a9d7c..bfa37769 100644 --- a/libpthread/Makefile +++ b/libpthread/Makefile @@ -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 \ diff --git a/libpthread/include/pthread.h b/libpthread/include/pthread.h index a1e3c332..50107a2c 100644 --- a/libpthread/include/pthread.h +++ b/libpthread/include/pthread.h @@ -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*); diff --git a/libpthread/pthread_create.c++ b/libpthread/pthread_create.c++ index bd0b03ff..50faf3f9 100644 --- a/libpthread/pthread_create.c++ +++ b/libpthread/pthread_create.c++ @@ -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; diff --git a/libpthread/pthread_detach.c++ b/libpthread/pthread_detach.c++ new file mode 100644 index 00000000..4d6e47fa --- /dev/null +++ b/libpthread/pthread_detach.c++ @@ -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 . + + pthread_detach.c++ + Detach a thread. + +*******************************************************************************/ + +#include +#include + +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; +} diff --git a/libpthread/pthread_exit.c++ b/libpthread/pthread_exit.c++ index 4674b71c..ee916ba6 100644 --- a/libpthread/pthread_exit.c++ +++ b/libpthread/pthread_exit.c++ @@ -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)); diff --git a/libpthread/pthread_initialize.c++ b/libpthread/pthread_initialize.c++ index 8b878193..96dab78f 100644 --- a/libpthread/pthread_initialize.c++ +++ b/libpthread/pthread_initialize.c++ @@ -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; }