summaryrefslogtreecommitdiff
path: root/tsd.c
diff options
context:
space:
mode:
authorrpj <rpj>2000-08-17 10:10:52 +0000
committerrpj <rpj>2000-08-17 10:10:52 +0000
commit951895c3aa196e90d4a5ecefe337d8773b8d33b7 (patch)
tree5a28b0b5aed60f301410a10ab62778f591cab474 /tsd.c
parentb05acba4bad2d8d55f6f4ce3a952b60baf43ab80 (diff)
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.
Diffstat (limited to 'tsd.c')
-rw-r--r--tsd.c38
1 files changed, 12 insertions, 26 deletions
diff --git a/tsd.c b/tsd.c
index 1381ce2..442cf2b 100644
--- a/tsd.c
+++ b/tsd.c
@@ -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);
}