finish unifying thread register handling in preparation for porting
[musl] / src / thread / pthread_self.c
1 #include "pthread_impl.h"
2
3 static struct pthread main_thread;
4
5 #undef errno
6 static int *errno_location()
7 {
8         return __pthread_self()->errno_ptr;
9 }
10
11 static int init_main_thread()
12 {
13         main_thread.self = &main_thread;
14         if (__set_thread_area(&main_thread) < 0)
15                 return -1;
16         syscall1(__NR_set_tid_address, (long)&main_thread.tid);
17         main_thread.errno_ptr = __errno_location();
18         libc.errno_location = errno_location;
19         main_thread.tid = main_thread.pid = getpid();
20         return 0;
21 }
22
23 pthread_t pthread_self()
24 {
25         static int init, failed;
26         if (!init) {
27                 if (failed) return 0;
28                 if (init_main_thread() < 0) failed = 1;
29                 if (failed) return 0;
30                 init = 1;
31         }
32         return __pthread_self();
33 }