add pthread_atfork interface
[musl] / src / thread / pthread_setspecific.c
1 #include "pthread_impl.h"
2
3 int pthread_setspecific(pthread_key_t k, const void *x)
4 {
5         struct pthread *self = pthread_self();
6         /* Handle the case of the main thread */
7         if (!self->tsd) {
8                 if (!x) return 0;
9                 if (!(self->tsd = calloc(sizeof(void *), PTHREAD_KEYS_MAX)))
10                         return ENOMEM;
11         }
12         /* Avoid unnecessary COW */
13         if (self->tsd[k] != x) {
14                 self->tsd[k] = (void *)x;
15                 self->tsd_used = 1;
16         }
17         return 0;
18 }