55d20c9f8a9ac759a6747cac9e073a170db24c22
[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 static int *errno_location()
10 {
11         return __pthread_self()->errno_ptr;
12 }
13
14 static int init_main_thread()
15 {
16         if (__set_thread_area(&main_thread) < 0) return -1;
17         main_thread.canceldisable = libc.canceldisable;
18         main_thread.tsd = (void **)__pthread_tsd_main;
19         main_thread.self = libc.main_thread = &main_thread;
20         main_thread.errno_ptr = __errno_location();
21         libc.errno_location = errno_location;
22         main_thread.tid = main_thread.pid = 
23                 __syscall(SYS_set_tid_address, &main_thread.tid);
24         return 0;
25 }
26
27 pthread_t pthread_self()
28 {
29         static int init, failed;
30         if (!init) {
31                 if (failed) return 0;
32                 if (init_main_thread() < 0) failed = 1;
33                 if (failed) return 0;
34                 init = 1;
35         }
36         return __pthread_self();
37 }