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. --- ChangeLog | 23 +++++++++++++++++++++++ create.c | 4 ++-- exit.c | 7 ++----- global.c | 3 +++ private.c | 51 ++++++--------------------------------------------- sync.c | 4 ++-- tsd.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- 7 files changed, 84 insertions(+), 64 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3da9a14..6219728 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +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. + Mon Oct 12 00:00:44 1998 Ross Johnson * implement.h (_pthread_tsd_key_table): New. diff --git a/create.c b/create.c index f477585..a0b8d02 100644 --- a/create.c +++ b/create.c @@ -72,7 +72,7 @@ pthread_create(pthread_t *thread, ret = _pthread_new_thread(&new_thread); - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ if (ret == 0) @@ -134,7 +134,7 @@ pthread_create(pthread_t *thread, /* Remove the failed thread entry. */ _pthread_delete_thread(new_thread); - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ } diff --git a/exit.c b/exit.c index be7db75..3472dca 100644 --- a/exit.c +++ b/exit.c @@ -18,9 +18,6 @@ _pthread_vacuum(void) _pthread_handler_pop_all(_PTHREAD_CLEANUP_STACK, _PTHREAD_HANDLER_EXECUTE); - /* Run all TSD key destructors. */ - _pthread_destructor_pop_all(); - /* Pop any atfork handlers without executing them. */ _pthread_handler_pop_all(_PTHREAD_FORKPREPARE_STACK, _PTHREAD_HANDLER_NOEXECUTE); @@ -44,7 +41,7 @@ _pthread_exit(pthread_t thread, void * value, int return_code) to any joining threads. */ thread->joinvalueptr = value; - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ _pthread_vacuum(); @@ -64,7 +61,7 @@ _pthread_exit(pthread_t thread, void * value, int return_code) (void) _pthread_delete_thread(thread); } - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ _endthreadex(return_code); diff --git a/global.c b/global.c index 9ce662f..ee6de82 100644 --- a/global.c +++ b/global.c @@ -60,5 +60,8 @@ pthread_mutex_t _pthread_threads_mutex_table[_PTHREAD_MAX_THREADS]; /* Global TSD key array. */ _pthread_tsd_key_t _pthread_tsd_key_table[_POSIX_THREAD_KEYS_MAX]; +/* Mutex lock for TSD operations */ +pthread_mutex_t _pthread_tsd_mutex = PTHREAD_MUTEX_INITIALIZER; + /* Index to the next available TSD key. */ int _pthread_tsd_key_next = 0; diff --git a/private.c b/private.c index e3fac58..9126aaa 100644 --- a/private.c +++ b/private.c @@ -73,46 +73,6 @@ Once taken from _pthread_virgins[], used and freed threads are only ever pushed back onto _pthread_reuse[]. - The code for choosing a new (pthread_t) thread from the pool of - free thread structs looks like: - - if (_pthread_reuse_top >= 0) - { - new_thread = _pthread_reuse[_pthread_reuse_top--]; - } - else - { - if (_pthread_virgin_next < PTHREAD_THREADS_MAX) - { - new_thread = _pthread_virgin[_pthread_virgin_next++]; - } - else - { - return EAGAIN; - } - } - - - The code to free a thread is: - - _pthread_reuse[++_pthread_reuse_top] = thread; - - - We still need a means for pthread_self() to return its own thread - ID. - - We use the Win32 Thread Local Storage mechanism. A single call to - TlsAlloc() will make available a single 32 bit location to every - thread in the process, including those created after the call is - made. - - Provided we don't need to call pthread_self() after the Win32 - thread has terminated we can use the DLL entry point routine to - initialise TLS for each thread. Or we can use pthread_once() in - pthread_create() to do it. - - We can use either option. We'll use the DLL entry point routine. - */ int @@ -150,6 +110,7 @@ _pthread_new_thread(pthread_t * thread) new_thread->forkchildstack = NULL; *thread = new_thread; + _pthread_threads_count++; return 0; } @@ -170,10 +131,10 @@ _pthread_delete_thread(_pthread_t * thread) thread->ptstatus = _PTHREAD_REUSE; _pthread_reuse[++_pthread_reuse_top] = thread; + _pthread_threads_count--; + + return 0; } - else - { - return EINVAL; - } - return 0; + + return EINVAL; } diff --git a/sync.c b/sync.c index b2b8323..069d10a 100644 --- a/sync.c +++ b/sync.c @@ -83,7 +83,7 @@ pthread_join(pthread_t thread, void ** valueptr) thread->join_count++; - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ /* CANCELATION POINT */ @@ -138,7 +138,7 @@ pthread_join(pthread_t thread, void ** valueptr) ret = _pthread_delete_thread(thread); } - pthread_mutex_lock(&_pthread_table_mutex); + pthread_mutex_unlock(&_pthread_table_mutex); /* END CRITICAL SECTION */ return ret; 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