diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | tests/join1.c | 2 | ||||
-rw-r--r-- | tsd.c | 38 |
3 files changed, 19 insertions, 27 deletions
@@ -1,3 +1,9 @@ +2000-08-17 Ross Johnson <rpj@special.ise.canberra.edu.au> + + * tsd.c (pthread_create_key): Initialise temporary + key before returning it's address to avoid race + conditions. + 2000-08-13 Ross Johnson <rpj@special.ise.canberra.edu.au> * errno.c: Add _MD precompile condition; thus far diff --git a/tests/join1.c b/tests/join1.c index 8cc80e5..d74e0c4 100644 --- a/tests/join1.c +++ b/tests/join1.c @@ -1,7 +1,7 @@ /* * Test for pthread_join(). * - * Depends on API functions: pthread_create(), pthread_exit(). + * Depends on API functions: pthread_create(), pthread_join(), pthread_exit(). */ #include "test.h" @@ -64,48 +64,34 @@ pthread_key_create (pthread_key_t * key, void (*destructor) (void *)) */ { int result = 0; - - if ((*key = (pthread_key_t) calloc (1, sizeof (**key))) == NULL) + pthread_key_t *newkey; + + if ((newkey = (pthread_key_t) calloc (1, sizeof (*newkey))) == NULL) { result = ENOMEM; } - else if (((*key)->key = TlsAlloc ()) == TLS_OUT_OF_INDEXES) + else if ((newkey->key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { result = EAGAIN; - free (*key); - *key = NULL; + free (newkey); + newkey = NULL; } else if (destructor != NULL) { /* * Have to manage associations between thread and key; * Therefore, need a lock that allows multiple threads - * to gain exclusive access to the key->threads list - */ -#if 1 - /* + * to gain exclusive access to the key->threads list. + * * The mutex will only be created when it is first locked. */ - (*key)->threadsLock = PTHREAD_MUTEX_INITIALIZER; - (*key)->destructor = destructor; -#else - result = pthread_mutex_init (&((*key)->threadsLock), NULL); - - if (result != 0) - { - TlsFree ((*key)->key); - - free (*key); - *key = NULL; - } - else - { - (*key)->destructor = destructor; - } -#endif + newkey->threadsLock = PTHREAD_MUTEX_INITIALIZER; + newkey->destructor = destructor; } + *key = newkey; + return (result); } |