summaryrefslogtreecommitdiff
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
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.
-rw-r--r--ChangeLog6
-rw-r--r--tests/join1.c2
-rw-r--r--tsd.c38
3 files changed, 19 insertions, 27 deletions
diff --git a/ChangeLog b/ChangeLog
index 1a788ea..043d2c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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"
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);
}