From 35037df03647b3097d9ee65b7724b8dd47976b84 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 3 Sep 2013 15:08:38 +0200 Subject: [PATCH] Add pthread_attr_setstacksize(3) and pthread_attr_getstacksize(3). --- libpthread/Makefile | 2 ++ libpthread/include/__/pthread.h | 2 ++ libpthread/include/pthread.h | 5 ++-- libpthread/pthread_attr_getstacksize.c++ | 33 ++++++++++++++++++++++++ libpthread/pthread_attr_init.c++ | 5 +++- libpthread/pthread_attr_setstacksize.c++ | 31 ++++++++++++++++++++++ libpthread/pthread_create.c++ | 13 +++++++--- 7 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 libpthread/pthread_attr_getstacksize.c++ create mode 100644 libpthread/pthread_attr_setstacksize.c++ diff --git a/libpthread/Makefile b/libpthread/Makefile index c7029c1b..9e1ea196 100644 --- a/libpthread/Makefile +++ b/libpthread/Makefile @@ -12,7 +12,9 @@ CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti OBJS=\ pthread_attr_destroy.o \ +pthread_attr_getstacksize.o \ pthread_attr_init.o \ +pthread_attr_setstacksize.o \ pthread_condattr_destroy.o \ pthread_condattr_getclock.o \ pthread_condattr_init.o \ diff --git a/libpthread/include/__/pthread.h b/libpthread/include/__/pthread.h index 8554dd5f..e168a9c5 100644 --- a/libpthread/include/__/pthread.h +++ b/libpthread/include/__/pthread.h @@ -36,10 +36,12 @@ __BEGIN_DECLS #if defined(__is_sortix_libpthread) typedef struct { + __SIZE_TYPE__ stack_size; } __pthread_attr_t; #else typedef struct { + __SIZE_TYPE__ __pthread_stack_size; } __pthread_attr_t; #endif diff --git a/libpthread/include/pthread.h b/libpthread/include/pthread.h index 1d507dde..50632da5 100644 --- a/libpthread/include/pthread.h +++ b/libpthread/include/pthread.h @@ -193,7 +193,8 @@ int pthread_attr_destroy(pthread_attr_t*); /* TODO: pthread_attr_getschedpolicy */ /* TODO: pthread_attr_getscope */ /* TODO: pthread_attr_getstack */ -/* TODO: pthread_attr_getstacksize */ +int pthread_attr_getstacksize(const pthread_attr_t* __restrict, + size_t* __restrict); int pthread_attr_init(pthread_attr_t*); /* TODO: pthread_attr_setdetachstate */ /* TODO: pthread_attr_setguardsize */ @@ -202,7 +203,7 @@ int pthread_attr_init(pthread_attr_t*); /* TODO: pthread_attr_setschedpolicy */ /* TODO: pthread_attr_setscope */ /* TODO: pthread_attr_setstack */ -/* TODO: pthread_attr_setstacksize */ +int pthread_attr_setstacksize(pthread_attr_t*, size_t); /* TODO: pthread_barrier_destroy */ /* TODO: pthread_barrier_init */ /* TODO: pthread_barrier_wait */ diff --git a/libpthread/pthread_attr_getstacksize.c++ b/libpthread/pthread_attr_getstacksize.c++ new file mode 100644 index 00000000..a1b607f2 --- /dev/null +++ b/libpthread/pthread_attr_getstacksize.c++ @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + 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_getstacksize.c++ + Gets the requested stack size in a thread attribute object. + +*******************************************************************************/ + +#include + +extern "C" +int pthread_attr_getstacksize(const pthread_attr_t* restrict attr, + size_t* restrict stack_size_ptr) +{ + *stack_size_ptr = attr->stack_size; + return 0; +} diff --git a/libpthread/pthread_attr_init.c++ b/libpthread/pthread_attr_init.c++ index 5634f273..24aad161 100644 --- a/libpthread/pthread_attr_init.c++ +++ b/libpthread/pthread_attr_init.c++ @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of Sortix libpthread. @@ -25,8 +25,11 @@ #include #include +static const unsigned long DEFAULT_STACK_SIZE = 64 * 1024; + extern "C" int pthread_attr_init(pthread_attr_t* attr) { memset(attr, 0, sizeof(*attr)); + attr->stack_size = DEFAULT_STACK_SIZE; return 0; } diff --git a/libpthread/pthread_attr_setstacksize.c++ b/libpthread/pthread_attr_setstacksize.c++ new file mode 100644 index 00000000..03b3373a --- /dev/null +++ b/libpthread/pthread_attr_setstacksize.c++ @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + 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_setstacksize.c++ + Sets the requested stack size in a thread attribute object. + +*******************************************************************************/ + +#include + +extern "C" int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) +{ + attr->stack_size = stack_size; + return 0; +} diff --git a/libpthread/pthread_create.c++ b/libpthread/pthread_create.c++ index afb6d81e..ec7c2fb1 100644 --- a/libpthread/pthread_create.c++ +++ b/libpthread/pthread_create.c++ @@ -121,8 +121,6 @@ static void setup_thread_state(struct pthread* thread, tforkregs_t* regs) } #endif -static const unsigned long DEFAULT_STACK_SIZE = 64 * 1024; - extern "C" { pthread_mutex_t __pthread_num_threads_lock = PTHREAD_MUTEX_INITIALIZER; @@ -131,12 +129,19 @@ extern "C" extern "C" int pthread_create(pthread_t* restrict thread_ptr, - const pthread_attr_t* restrict /*attr*/, + const pthread_attr_t* restrict attr, void* (*entry_function)(void*), void* restrict entry_cookie) { assert(thread_ptr); + pthread_attr_t default_attr; + if ( !attr ) + { + pthread_attr_init(&default_attr); + attr = &default_attr; + } + struct pthread* self = pthread_self(); // We can't create a thread local storage copy (and thus a new thread) if @@ -204,7 +209,7 @@ int pthread_create(pthread_t* restrict thread_ptr, // Set up a stack for the new thread. int stack_prot = PROT_READ | PROT_WRITE; int stack_flags = MAP_PRIVATE | MAP_ANONYMOUS; - thread->uthread.stack_size = DEFAULT_STACK_SIZE; + thread->uthread.stack_size = attr->stack_size; thread->uthread.stack_mmap = mmap(NULL, thread->uthread.stack_size, stack_prot, stack_flags, -1, 0); if ( thread->uthread.stack_mmap == MAP_FAILED )