Add pthread_attr_getdetachstate(3) and pthread_attr_setdetachstate(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-09 23:48:43 +01:00
parent aba97b772a
commit 14ca218453
9 changed files with 91 additions and 8 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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 */

View File

@ -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 <http://www.gnu.org/licenses/>.
pthread_attr_getdetachstate.c++
Gets the requested detach state in a thread attribute object.
*******************************************************************************/
#include <pthread.h>
extern "C"
int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detach_state)
{
return *detach_state = attr->detach_state, 0;
}

View File

@ -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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
pthread_attr_setdetachstate.c++
Sets the requested detach state in a thread attribute object.
*******************************************************************************/
#include <errno.h>
#include <pthread.h>
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;
}

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_state = attr->detach_state;
thread->entry_function = entry_function;
thread->entry_cookie = entry_cookie;

View File

@ -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();
}
}

View File

@ -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;
}