initial check-in, version 0.5.0
[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.tlsdesc[0] = -1;
14         main_thread.tlsdesc[1] = (long)&main_thread;
15         main_thread.tlsdesc[2] = SIZE_MAX/PAGE_SIZE;
16         main_thread.tlsdesc[3] = 0x51;
17         main_thread.self = &main_thread;
18         main_thread.errno_ptr = __errno_location();
19         if (__set_thread_area(main_thread.tlsdesc) < 0)
20                 return -1;
21         syscall1(__NR_set_tid_address, (long)&main_thread.tid);
22         libc.errno_location = errno_location;
23         main_thread.tid = main_thread.pid = getpid();
24         return 0;
25 }
26
27 #undef pthread_self
28
29 pthread_t pthread_self()
30 {
31         static int init, failed;
32         if (!init) {
33                 if (failed) return 0;
34                 if (init_main_thread() < 0) failed = 1;
35                 if (failed) return 0;
36                 init = 1;
37         }
38         return __pthread_self();
39 }