Add pthread_join(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-09 23:20:45 +01:00
parent 35037df036
commit aba97b772a
6 changed files with 59 additions and 3 deletions

View File

@ -30,6 +30,7 @@ pthread_equal.o \
pthread_exit.o \
pthread_getspecific.o \
pthread_initialize.o \
pthread_join.o \
pthread_key_create.o \
pthread_key_delete.o \
pthread_mutexattr_destroy.o \

View File

@ -138,8 +138,10 @@ typedef __pthread_t pthread_t;
struct pthread
{
struct uthread uthread;
pthread_mutex_t join_lock;
void* (*entry_function)(void*);
void* entry_cookie;
void* exit_result;
void** keys;
size_t keys_length;
};
@ -242,7 +244,7 @@ void pthread_exit(void*);
/* TODO: pthread_getcpuclockid */
/* TODO: pthread_getschedparam */
void* pthread_getspecific(pthread_key_t);
/* TODO: pthread_join */
int pthread_join(pthread_t, void**);
int pthread_key_create(pthread_key_t*, void (*)(void*));
int pthread_key_delete(pthread_key_t);
/* TODO: pthread_mutex_consistent */

View File

@ -203,6 +203,10 @@ int pthread_create(pthread_t* restrict thread_ptr,
thread->uthread.tls_size = tls_size;
thread->uthread.arg_mmap = self->uthread.arg_mmap;
thread->uthread.arg_size = self->uthread.arg_size;
thread->join_lock = PTHREAD_NORMAL_MUTEX_INITIALIZER_NP;
thread->join_lock.lock = 1 /* LOCKED_VALUE */;
thread->join_lock.type = PTHREAD_MUTEX_NORMAL;
thread->join_lock.owner = (unsigned long) thread;
thread->entry_function = entry_function;
thread->entry_cookie = entry_cookie;

View File

@ -31,7 +31,7 @@
extern "C"
__attribute__((__noreturn__))
void pthread_exit(void* /*return_value*/)
void pthread_exit(void* return_value)
{
struct pthread* thread = pthread_self();
@ -61,10 +61,13 @@ void pthread_exit(void* /*return_value*/)
pthread_mutex_unlock(&__pthread_num_threads_lock);
if ( num_threads == 1 )
exit(0);
thread->exit_result = return_value;
struct exit_thread extended;
memset(&extended, 0, sizeof(extended));
extended.unmap_from = thread->uthread.stack_mmap;
extended.unmap_size = thread->uthread.stack_size;
exit_thread(0, EXIT_THREAD_UNMAP, &extended);
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();
}

View File

@ -72,4 +72,10 @@ static void elf_note_sortix_pthread_size()
extern "C" void pthread_initialize(void)
{
struct pthread* self = pthread_self();
self->join_lock = PTHREAD_NORMAL_MUTEX_INITIALIZER_NP;
self->join_lock.lock = 1 /* LOCKED_VALUE */;
self->join_lock.type = PTHREAD_MUTEX_NORMAL;
self->join_lock.owner = (unsigned long) self;
}

View File

@ -0,0 +1,40 @@
/*******************************************************************************
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_join.c++
Wait for thread termination.
*******************************************************************************/
#include <sys/mman.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
extern "C" int pthread_join(pthread_t thread, void** result_ptr)
{
pthread_mutex_lock(&thread->join_lock);
pthread_mutex_unlock(&thread->join_lock);
void* result = thread->exit_result;
munmap(thread->uthread.tls_mmap, thread->uthread.tls_size);
if ( result_ptr )
*result_ptr = result;
return 0;
}