initial check-in, version 0.5.0
[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         int i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
13         int j = i;
14
15         libc.tsd_keys = keys;
16         if (!dtor) dtor = nodtor;
17         /* Cheap trick - &k cannot match any destructor pointer */
18         while (a_cas_p(keys+j, 0, &k)
19                 && (j=(j+1)%PTHREAD_KEYS_MAX) != i);
20         if (keys[j] != (void (*)(void *))&k)
21                 return EAGAIN;
22         keys[j] = dtor;
23         *k = j;
24         return 0;
25 }