From 8c4d7f6bc1d365906724c92e4143fa021bf8a757 Mon Sep 17 00:00:00 2001 From: rpj Date: Wed, 14 Oct 1998 06:49:46 +0000 Subject: Wed Oct 14 21:09:24 1998 Ross Johnson * private.c (_pthread_new_thread): Increment _pthread_threads_count. (_pthread_delete_thread): Decrement _pthread_threads_count. Remove some comments. * exit.c (_pthread_exit): : Fix two pthread_mutex_lock() calls that should have been pthread_mutex_unlock() calls. (_pthread_vacuum): Remove call to _pthread_destructor_pop_all(). * create.c (pthread_create): Fix two pthread_mutex_lock() calls that should have been pthread_mutex_unlock() calls. * global.c (_pthread_tsd_mutex): Add mutex for TSD operations. * tsd.c (pthread_key_create): Add critical section. (pthread_setspecific): Ditto. (pthread_getspecific): Ditto. (pthread_key_delete): Ditto. * sync.c (pthread_join): Fix two pthread_mutex_lock() calls that should have been pthread_mutex_unlock() calls. --- tsd.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) (limited to 'tsd.c') diff --git a/tsd.c b/tsd.c index 8e53b37..7b801db 100644 --- a/tsd.c +++ b/tsd.c @@ -57,26 +57,42 @@ int pthread_key_create(pthread_key_t *key, void (*destructor)(void *)) { pthread_key_t k; + int ret = 0; + + /* CRITICAL SECTION */ + pthread_mutex_lock(&_pthread_tsd_mutex); if (_pthread_tsd_key_next >= PTHREAD_KEYS_MAX) - return EAGAIN; + ret = EAGAIN; k = _pthread_tsd_key_next++; _pthread_tsd_key_table[k].in_use = _PTHREAD_TSD_KEY_INUSE; _pthread_tsd_key_table[k].destructor = destructor; + pthread_mutex_unlock(&_pthread_tsd_mutex); + /* END CRITICAL SECTION */ + *key = k; - return 0; + return ret; } int pthread_setspecific(pthread_key_t key, void *value) { void ** keys; + int inuse; - if (_pthread_tsd_key_table[key].in_use != _PTHREAD_TSD_KEY_INUSE) + /* CRITICAL SECTION */ + pthread_mutex_lock(&_pthread_tsd_mutex); + + inuse = (_pthread_tsd_key_table[key].in_use == _PTHREAD_TSD_KEY_INUSE); + + pthread_mutex_unlock(&_pthread_tsd_mutex); + /* END CRITICAL SECTION */ + + if (! inuse) return EINVAL; keys = (void **) TlsGetValue(_pthread_TSD_keys_TlsIndex); @@ -89,8 +105,17 @@ void * pthread_getspecific(pthread_key_t key) { void ** keys; + int inuse; - if (_pthread_tsd_key_table[key].in_use != _PTHREAD_TSD_KEY_INUSE) + /* CRITICAL SECTION */ + pthread_mutex_lock(&_pthread_tsd_mutex); + + inuse = (_pthread_tsd_key_table[key].in_use == _PTHREAD_TSD_KEY_INUSE); + + pthread_mutex_unlock(&_pthread_tsd_mutex); + /* END CRITICAL SECTION */ + + if (! inuse) return EINVAL; keys = (void **) TlsGetValue(_pthread_TSD_keys_TlsIndex); @@ -100,12 +125,23 @@ pthread_getspecific(pthread_key_t key) int pthread_key_delete(pthread_key_t key) { - if (_pthread_tsd_key_table[key].in_use != _PTHREAD_TSD_KEY_INUSE) - return EINVAL; + int ret = 0; - _pthread_tsd_key_table[key].in_use = _PTHREAD_TSD_KEY_DELETED; - _pthread_tsd_key_table[key].destructor = NULL; + /* CRITICAL SECTION */ + pthread_mutex_lock(&_pthread_tsd_mutex); - return 0; + if (_pthread_tsd_key_table[key].in_use != _PTHREAD_TSD_KEY_INUSE) + { + ret = EINVAL; + } + else + { + _pthread_tsd_key_table[key].in_use = _PTHREAD_TSD_KEY_DELETED; + _pthread_tsd_key_table[key].destructor = NULL; + } + + pthread_mutex_unlock(&_pthread_tsd_mutex); + /* END CRITICAL SECTION */ + + return ret; } - -- cgit v1.2.3