greatly simplify pthread_key_create (~20% size reduction)
[musl] / src / thread / pthread_key_create.c
1 #include "pthread_impl.h"
2
3 const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
4
5 static void nodtor(void *dummy)
6 {
7 }
8
9 int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
10 {
11         static void (*keys[PTHREAD_KEYS_MAX])(void *);
12         unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
13         unsigned j = i;
14
15         pthread_self();
16         libc.tsd_keys = keys;
17         if (!dtor) dtor = nodtor;
18         do {
19                 if (!a_cas_p(keys+j, 0, dtor)) {
20                         *k = j;
21                         return 0;
22                 }
23         } while ((j=(j+1)%PTHREAD_KEYS_MAX) != i);
24         return EAGAIN;
25 }