Add pthread_getspecific(3) and pthread_setspecific(3).
This commit is contained in:
parent
7e452f97f6
commit
12dcfd7b47
|
@ -23,6 +23,7 @@ pthread_cond_timedwait.o \
|
||||||
pthread_cond_wait.o \
|
pthread_cond_wait.o \
|
||||||
pthread_equal.o \
|
pthread_equal.o \
|
||||||
pthread_exit.o \
|
pthread_exit.o \
|
||||||
|
pthread_getspecific.o \
|
||||||
pthread_initialize.o \
|
pthread_initialize.o \
|
||||||
pthread_key_create.o \
|
pthread_key_create.o \
|
||||||
pthread_key_delete.o \
|
pthread_key_delete.o \
|
||||||
|
@ -43,6 +44,7 @@ pthread_rwlock_trywrlock.o \
|
||||||
pthread_rwlock_unlock.o \
|
pthread_rwlock_unlock.o \
|
||||||
pthread_rwlock_wrlock.o \
|
pthread_rwlock_wrlock.o \
|
||||||
pthread_self.o \
|
pthread_self.o \
|
||||||
|
pthread_setspecific.o \
|
||||||
|
|
||||||
BINS:=libpthread.a
|
BINS:=libpthread.a
|
||||||
|
|
||||||
|
|
|
@ -139,6 +139,8 @@ typedef __pthread_t pthread_t;
|
||||||
struct pthread
|
struct pthread
|
||||||
{
|
{
|
||||||
struct uthread uthread;
|
struct uthread uthread;
|
||||||
|
void** keys;
|
||||||
|
size_t keys_length;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -228,7 +230,7 @@ void pthread_exit(void*);
|
||||||
/* TODO: pthread_getconcurrency */
|
/* TODO: pthread_getconcurrency */
|
||||||
/* TODO: pthread_getcpuclockid */
|
/* TODO: pthread_getcpuclockid */
|
||||||
/* TODO: pthread_getschedparam */
|
/* TODO: pthread_getschedparam */
|
||||||
/* TODO: pthread_getspecific */
|
void* pthread_getspecific(pthread_key_t);
|
||||||
/* TODO: pthread_join */
|
/* TODO: pthread_join */
|
||||||
int pthread_key_create(pthread_key_t*, void (*)(void*));
|
int pthread_key_create(pthread_key_t*, void (*)(void*));
|
||||||
int pthread_key_delete(pthread_key_t);
|
int pthread_key_delete(pthread_key_t);
|
||||||
|
@ -275,7 +277,7 @@ pthread_t pthread_self(void);
|
||||||
/* TODO: pthread_setconcurrency */
|
/* TODO: pthread_setconcurrency */
|
||||||
/* TODO: pthread_setschedparam */
|
/* TODO: pthread_setschedparam */
|
||||||
/* TODO: pthread_setschedprio */
|
/* TODO: pthread_setschedprio */
|
||||||
/* TODO: pthread_setspecific */
|
int pthread_setspecific(pthread_key_t, const void*);
|
||||||
/* TODO: pthread_spin_destroy */
|
/* TODO: pthread_spin_destroy */
|
||||||
/* TODO: pthread_spin_init */
|
/* TODO: pthread_spin_init */
|
||||||
/* TODO: pthread_spin_lock */
|
/* TODO: pthread_spin_lock */
|
||||||
|
|
|
@ -34,6 +34,28 @@ __attribute__((__noreturn__))
|
||||||
void pthread_exit(void* /*return_value*/)
|
void pthread_exit(void* /*return_value*/)
|
||||||
{
|
{
|
||||||
struct pthread* thread = pthread_self();
|
struct pthread* thread = pthread_self();
|
||||||
|
|
||||||
|
pthread_mutex_lock(&__pthread_keys_lock);
|
||||||
|
bool keys_left = true;
|
||||||
|
while ( keys_left )
|
||||||
|
{
|
||||||
|
keys_left = false;
|
||||||
|
for ( pthread_key_t key = 0; key < thread->keys_length; key++ )
|
||||||
|
{
|
||||||
|
void* key_value = thread->keys[key];
|
||||||
|
if ( !key_value )
|
||||||
|
continue;
|
||||||
|
thread->keys[key] = NULL;
|
||||||
|
if ( __pthread_keys[key].destructor )
|
||||||
|
__pthread_keys[key].destructor(key_value);
|
||||||
|
keys_left = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(thread->keys);
|
||||||
|
thread->keys = NULL;
|
||||||
|
thread->keys_length = 0;
|
||||||
|
pthread_mutex_unlock(&__pthread_keys_lock);
|
||||||
|
|
||||||
size_t num_threads = 1;
|
size_t num_threads = 1;
|
||||||
if ( num_threads == 1 )
|
if ( num_threads == 1 )
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
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_getspecific.c++
|
||||||
|
Thread-specific data management.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
extern "C" void* pthread_getspecific(pthread_key_t key)
|
||||||
|
{
|
||||||
|
struct pthread* thread = pthread_self();
|
||||||
|
|
||||||
|
if ( key < thread->keys_length )
|
||||||
|
return thread->keys[key];
|
||||||
|
|
||||||
|
pthread_mutex_lock(&__pthread_keys_lock);
|
||||||
|
|
||||||
|
assert(key < __pthread_keys_length);
|
||||||
|
assert(__pthread_keys[key].destructor);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&__pthread_keys_lock);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
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_setspecific.c++
|
||||||
|
Thread-specific data management.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern "C" int pthread_setspecific(pthread_key_t key, const void* value_const)
|
||||||
|
{
|
||||||
|
void* value = (void*) value_const;
|
||||||
|
|
||||||
|
struct pthread* thread = pthread_self();
|
||||||
|
|
||||||
|
if ( key < thread->keys_length )
|
||||||
|
return thread->keys[key] = value, 0;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&__pthread_keys_lock);
|
||||||
|
|
||||||
|
assert(key < __pthread_keys_length);
|
||||||
|
assert(__pthread_keys[key].destructor);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&__pthread_keys_lock);
|
||||||
|
|
||||||
|
size_t old_length = thread->keys_length;
|
||||||
|
size_t new_length = __pthread_keys_length;
|
||||||
|
size_t new_size = new_length * sizeof(void*);
|
||||||
|
void** new_keys = (void**) realloc(thread->keys, new_size);
|
||||||
|
if ( !new_keys )
|
||||||
|
return errno;
|
||||||
|
thread->keys = new_keys;
|
||||||
|
thread->keys_length = new_length;
|
||||||
|
for ( size_t i = old_length; i < new_length; i++ )
|
||||||
|
new_keys[i] = NULL;
|
||||||
|
|
||||||
|
return thread->keys[key] = value, 0;
|
||||||
|
}
|
Loading…
Reference in New Issue