From 14ca2184531d8d3394e1ebaa81947e33b66229d2 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 9 Jan 2014 23:48:43 +0100 Subject: [PATCH] Add pthread_attr_getdetachstate(3) and pthread_attr_setdetachstate(3). --- libpthread/Makefile | 2 ++ libpthread/include/__/pthread.h | 2 ++ libpthread/include/pthread.h | 9 +++--- libpthread/pthread_attr_getdetachstate.c++ | 31 +++++++++++++++++++ libpthread/pthread_attr_init.c++ | 1 + libpthread/pthread_attr_setdetachstate.c++ | 35 ++++++++++++++++++++++ libpthread/pthread_create.c++ | 1 + libpthread/pthread_exit.c++ | 17 ++++++++--- libpthread/pthread_initialize.c++ | 1 + 9 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 libpthread/pthread_attr_getdetachstate.c++ create mode 100644 libpthread/pthread_attr_setdetachstate.c++ diff --git a/libpthread/Makefile b/libpthread/Makefile index f6a3f729..ff7a9d7c 100644 --- a/libpthread/Makefile +++ b/libpthread/Makefile @@ -12,8 +12,10 @@ CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti OBJS=\ pthread_attr_destroy.o \ +pthread_attr_getdetachstate.o \ pthread_attr_getstacksize.o \ pthread_attr_init.o \ +pthread_attr_setdetachstate.o \ pthread_attr_setstacksize.o \ pthread_condattr_destroy.o \ pthread_condattr_getclock.o \ diff --git a/libpthread/include/__/pthread.h b/libpthread/include/__/pthread.h index e168a9c5..2ee38a6e 100644 --- a/libpthread/include/__/pthread.h +++ b/libpthread/include/__/pthread.h @@ -37,11 +37,13 @@ __BEGIN_DECLS typedef struct { __SIZE_TYPE__ stack_size; + int detach_state; } __pthread_attr_t; #else typedef struct { __SIZE_TYPE__ __pthread_stack_size; + int __pthread_detached_state; } __pthread_attr_t; #endif diff --git a/libpthread/include/pthread.h b/libpthread/include/pthread.h index c5f09508..a1e3c332 100644 --- a/libpthread/include/pthread.h +++ b/libpthread/include/pthread.h @@ -51,8 +51,8 @@ __BEGIN_DECLS /* TODO: #define PTHREAD_CANCEL_DEFERRED */ /* TODO: #define PTHREAD_CANCEL_DISABLE */ /* TODO: #define PTHREAD_CANCELED */ -/* TODO: #define PTHREAD_CREATE_DETACHED */ -/* TODO: #define PTHREAD_CREATE_JOINABLE */ +#define PTHREAD_CREATE_DETACHED 1 +#define PTHREAD_CREATE_JOINABLE 0 /* TODO: #define PTHREAD_EXPLICIT_SCHED */ /* TODO: #define PTHREAD_INHERIT_SCHED */ #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL @@ -144,6 +144,7 @@ struct pthread void* exit_result; void** keys; size_t keys_length; + int detach_state; }; #endif @@ -188,7 +189,7 @@ struct pthread* pthread_allocate_tls(void); /* TODO: pthread_atfork */ int pthread_attr_destroy(pthread_attr_t*); -/* TODO: pthread_attr_getdetachstate */ +int pthread_attr_getdetachstate(const pthread_attr_t*, int*); /* TODO: pthread_attr_getguardsize */ /* TODO: pthread_attr_getinheritsched */ /* TODO: pthread_attr_getschedparam */ @@ -198,7 +199,7 @@ int pthread_attr_destroy(pthread_attr_t*); int pthread_attr_getstacksize(const pthread_attr_t* __restrict, size_t* __restrict); int pthread_attr_init(pthread_attr_t*); -/* TODO: pthread_attr_setdetachstate */ +int pthread_attr_setdetachstate(pthread_attr_t*, int); /* TODO: pthread_attr_setguardsize */ /* TODO: pthread_attr_setinheritsched */ /* TODO: pthread_attr_setschedparam */ diff --git a/libpthread/pthread_attr_getdetachstate.c++ b/libpthread/pthread_attr_getdetachstate.c++ new file mode 100644 index 00000000..02eeb12f --- /dev/null +++ b/libpthread/pthread_attr_getdetachstate.c++ @@ -0,0 +1,31 @@ +/******************************************************************************* + + 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_attr_getdetachstate.c++ + Gets the requested detach state in a thread attribute object. + +*******************************************************************************/ + +#include + +extern "C" +int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detach_state) +{ + return *detach_state = attr->detach_state, 0; +} diff --git a/libpthread/pthread_attr_init.c++ b/libpthread/pthread_attr_init.c++ index 24aad161..f52b1ab1 100644 --- a/libpthread/pthread_attr_init.c++ +++ b/libpthread/pthread_attr_init.c++ @@ -31,5 +31,6 @@ extern "C" int pthread_attr_init(pthread_attr_t* attr) { memset(attr, 0, sizeof(*attr)); attr->stack_size = DEFAULT_STACK_SIZE; + attr->detach_state = PTHREAD_CREATE_JOINABLE; return 0; } diff --git a/libpthread/pthread_attr_setdetachstate.c++ b/libpthread/pthread_attr_setdetachstate.c++ new file mode 100644 index 00000000..8460b938 --- /dev/null +++ b/libpthread/pthread_attr_setdetachstate.c++ @@ -0,0 +1,35 @@ +/******************************************************************************* + + 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_attr_setdetachstate.c++ + Sets the requested detach state in a thread attribute object. + +*******************************************************************************/ + +#include +#include + +extern "C" +int pthread_attr_setdetachstate(pthread_attr_t* attr, int detach_state) +{ + if ( detach_state != PTHREAD_CREATE_JOINABLE && + detach_state != PTHREAD_CREATE_DETACHED ) + return errno = EINVAL; + return attr->detach_state = detach_state, 0; +} diff --git a/libpthread/pthread_create.c++ b/libpthread/pthread_create.c++ index f174fb32..bd0b03ff 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_state = attr->detach_state; thread->entry_function = entry_function; thread->entry_cookie = entry_cookie; diff --git a/libpthread/pthread_exit.c++ b/libpthread/pthread_exit.c++ index c2835a19..4674b71c 100644 --- a/libpthread/pthread_exit.c++ +++ b/libpthread/pthread_exit.c++ @@ -66,8 +66,17 @@ void pthread_exit(void* return_value) memset(&extended, 0, sizeof(extended)); extended.unmap_from = thread->uthread.stack_mmap; extended.unmap_size = thread->uthread.stack_size; - extended.zero_from = &thread->join_lock.lock; - extended.zero_size = sizeof(thread->join_lock.lock); - exit_thread(0, EXIT_THREAD_UNMAP | EXIT_THREAD_ZERO, &extended); - __builtin_unreachable(); + if ( thread->detach_state == PTHREAD_CREATE_JOINABLE ) + { + extended.zero_from = &thread->join_lock.lock; + extended.zero_size = sizeof(thread->join_lock.lock); + exit_thread(0, EXIT_THREAD_UNMAP | EXIT_THREAD_ZERO, &extended); + __builtin_unreachable(); + } + else + { + munmap(thread->uthread.tls_mmap, thread->uthread.tls_size); + exit_thread(0, EXIT_THREAD_UNMAP, &extended); + __builtin_unreachable(); + } } diff --git a/libpthread/pthread_initialize.c++ b/libpthread/pthread_initialize.c++ index 9c744db0..8b878193 100644 --- a/libpthread/pthread_initialize.c++ +++ b/libpthread/pthread_initialize.c++ @@ -78,4 +78,5 @@ 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_state = PTHREAD_CREATE_JOINABLE; }