use bss instead of mmap for main thread's pthread thread-specific data
[musl] / src / thread / pthread_self.c
1 #include "pthread_impl.h"
2
3 static struct pthread main_thread;
4
5 /* pthread_key_create.c overrides this */
6 static const void *dummy[1] = { 0 };
7 weak_alias(dummy, __pthread_tsd_main);
8
9 #undef errno
10 static int *errno_location()
11 {
12         return __pthread_self()->errno_ptr;
13 }
14
15 static int init_main_thread()
16 {
17         main_thread.tsd = (void **)__pthread_tsd_main;
18         main_thread.self = &main_thread;
19         if (__set_thread_area(&main_thread) < 0)
20                 return -1;
21         main_thread.errno_ptr = __errno_location();
22         libc.errno_location = errno_location;
23         main_thread.tid = main_thread.pid = 
24                 syscall(SYS_set_tid_address, &main_thread.tid);
25         return 0;
26 }
27
28 pthread_t pthread_self()
29 {
30         static int init, failed;
31         if (!init) {
32                 if (failed) return 0;
33                 if (init_main_thread() < 0) failed = 1;
34                 if (failed) return 0;
35                 init = 1;
36         }
37         return __pthread_self();
38 }