projects
/
musl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
re-enable vdso clock_gettime on arm (32-bit) with workaround
[musl]
/
src
/
thread
/
pthread_key_create.c
diff --git
a/src/thread/pthread_key_create.c
b/src/thread/pthread_key_create.c
index
fa8a654
..
d112094
100644
(file)
--- a/
src/thread/pthread_key_create.c
+++ b/
src/thread/pthread_key_create.c
@@
-1,48
+1,91
@@
#include "pthread_impl.h"
#include "pthread_impl.h"
-
const
size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
+
volatile
size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
static void (*keys[PTHREAD_KEYS_MAX])(void *);
void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
static void (*keys[PTHREAD_KEYS_MAX])(void *);
+static pthread_rwlock_t key_lock = PTHREAD_RWLOCK_INITIALIZER;
+
+static pthread_key_t next_key;
+
static void nodtor(void *dummy)
{
}
static void nodtor(void *dummy)
{
}
-int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
+static void dummy_0(void)
+{
+}
+
+weak_alias(dummy_0, __tl_lock);
+weak_alias(dummy_0, __tl_unlock);
+
+int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
{
{
- unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
- unsigned j = i;
+ pthread_t self = __pthread_self();
- pthread_self();
+ /* This can only happen in the main thread before
+ * pthread_create has been called. */
+ if (!self->tsd) self->tsd = __pthread_tsd_main;
+
+ /* Purely a sentinel value since null means slot is free. */
if (!dtor) dtor = nodtor;
if (!dtor) dtor = nodtor;
+
+ __pthread_rwlock_wrlock(&key_lock);
+ pthread_key_t j = next_key;
do {
do {
- if (!a_cas_p(keys+j, 0, dtor)) {
- *k = j;
+ if (!keys[j]) {
+ keys[next_key = *k = j] = dtor;
+ __pthread_rwlock_unlock(&key_lock);
return 0;
}
return 0;
}
- } while ((j=(j+1)%PTHREAD_KEYS_MAX) != i);
+ } while ((j=(j+1)%PTHREAD_KEYS_MAX) != next_key);
+
+ __pthread_rwlock_unlock(&key_lock);
return EAGAIN;
}
return EAGAIN;
}
-int pthread_key_delete(pthread_key_t k)
+int
__
pthread_key_delete(pthread_key_t k)
{
{
+ sigset_t set;
+ pthread_t self = __pthread_self(), td=self;
+
+ __block_app_sigs(&set);
+ __pthread_rwlock_wrlock(&key_lock);
+
+ __tl_lock();
+ do td->tsd[k] = 0;
+ while ((td=td->next)!=self);
+ __tl_unlock();
+
keys[k] = 0;
keys[k] = 0;
+
+ __pthread_rwlock_unlock(&key_lock);
+ __restore_sigs(&set);
+
return 0;
}
return 0;
}
-void __pthread_tsd_run_dtors(
pthread_t self
)
+void __pthread_tsd_run_dtors()
{
{
- int i, j, not_finished = self->tsd_used;
- for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
- not_finished = 0;
+ pthread_t self = __pthread_self();
+ int i, j;
+ for (j=0; self->tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
+ __pthread_rwlock_rdlock(&key_lock);
+ self->tsd_used = 0;
for (i=0; i<PTHREAD_KEYS_MAX; i++) {
for (i=0; i<PTHREAD_KEYS_MAX; i++) {
- if (self->tsd[i] && keys[i]) {
- void *tmp = self->tsd[i];
- self->tsd[i] = 0;
- keys[i](tmp);
- not_finished = 1;
+ void *val = self->tsd[i];
+ void (*dtor)(void *) = keys[i];
+ self->tsd[i] = 0;
+ if (val && dtor && dtor != nodtor) {
+ __pthread_rwlock_unlock(&key_lock);
+ dtor(val);
+ __pthread_rwlock_rdlock(&key_lock);
}
}
}
}
+ __pthread_rwlock_unlock(&key_lock);
}
}
}
}
+
+weak_alias(__pthread_key_create, pthread_key_create);
+weak_alias(__pthread_key_delete, pthread_key_delete);