X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fthread%2Fpthread_key_create.c;h=d112094122b316d161a6fd17f1c08d12d0b58e05;hb=6aeb9c6703670649ee09b3c8575fb428168bb75c;hp=bfcd59764d52081f1802d707ff5d002e88add0e0;hpb=df7d0dfb9c686df31149d09008ba92834bed9803;p=musl diff --git a/src/thread/pthread_key_create.c b/src/thread/pthread_key_create.c index bfcd5976..d1120941 100644 --- a/src/thread/pthread_key_create.c +++ b/src/thread/pthread_key_create.c @@ -5,54 +5,87 @@ 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 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; - - if (libc.has_thread_pointer) { - pthread_t 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; - } + pthread_t 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; + + __pthread_rwlock_wrlock(&key_lock); + pthread_key_t j = next_key; do { - if (!a_cas_p(keys+j, 0, (void *)dtor)) { - *k = j; + if (!keys[j]) { + keys[next_key = *k = j] = dtor; + __pthread_rwlock_unlock(&key_lock); 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; } 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; + + __pthread_rwlock_unlock(&key_lock); + __restore_sigs(&set); + return 0; } void __pthread_tsd_run_dtors() { pthread_t self = __pthread_self(); - int i, j, not_finished = self->tsd_used; - for (j=0; not_finished && jtsd_used && jtsd_used = 0; for (i=0; itsd[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_delete, pthread_key_delete); weak_alias(__pthread_key_create, pthread_key_create); +weak_alias(__pthread_key_delete, pthread_key_delete);