add proper fuxed-based locking for stdio
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2 #include "stdio_impl.h"
3
4 static void dummy_0()
5 {
6 }
7 weak_alias(dummy_0, __synccall_lock);
8 weak_alias(dummy_0, __synccall_unlock);
9 weak_alias(dummy_0, __pthread_tsd_run_dtors);
10
11 #ifdef __pthread_unwind_next
12 #undef __pthread_unwind_next
13 #define __pthread_unwind_next __pthread_unwind_next_3
14 #endif
15
16 void __pthread_unwind_next(struct __ptcb *cb)
17 {
18         pthread_t self = pthread_self();
19         int n;
20
21         if (cb->__next) {
22                 self->cancelbuf = cb->__next->__next;
23                 longjmp((void *)cb->__next->__jb, 1);
24         }
25
26         __pthread_tsd_run_dtors();
27
28         __lock(&self->exitlock);
29
30         /* Mark this thread dead before decrementing count */
31         __lock(&self->killlock);
32         self->dead = 1;
33         a_store(&self->killlock, 0);
34
35         do n = libc.threads_minus_1;
36         while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
37         if (!n) exit(0);
38
39         if (self->detached && self->map_base) {
40                 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
41                 __unmapself(self->map_base, self->map_size);
42         }
43
44         __syscall(SYS_exit, 0);
45 }
46
47 static int start(void *p)
48 {
49         struct pthread *self = p;
50         if (self->unblock_cancel)
51                 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
52         pthread_exit(self->start(self->start_arg));
53         return 0;
54 }
55
56 int __uniclone(void *, int (*)(), void *);
57
58 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
59
60 /* pthread_key_create.c overrides this */
61 static const size_t dummy = 0;
62 weak_alias(dummy, __pthread_tsd_size);
63
64 static FILE *const dummy_file = 0;
65 weak_alias(dummy_file, __stdin_used);
66 weak_alias(dummy_file, __stdout_used);
67 weak_alias(dummy_file, __stderr_used);
68
69 static void init_file_lock(FILE *f)
70 {
71         if (f && f->lock<0) f->lock = 0;
72 }
73
74 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
75 {
76         int ret;
77         size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
78         size_t guard = DEFAULT_GUARD_SIZE;
79         struct pthread *self = pthread_self(), *new;
80         unsigned char *map, *stack, *tsd;
81
82         if (!self) return ENOSYS;
83         if (!libc.threaded) {
84                 for (FILE *f=libc.ofl_head; f; f=f->next)
85                         init_file_lock(f);
86                 init_file_lock(__stdin_used);
87                 init_file_lock(__stdout_used);
88                 init_file_lock(__stderr_used);
89                 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
90                 libc.threaded = 1;
91         }
92
93         if (attr) {
94                 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
95                 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
96         }
97         size += __pthread_tsd_size;
98         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
99         if (!map) return EAGAIN;
100         if (guard) mprotect(map, guard, PROT_NONE);
101
102         tsd = map + size - __pthread_tsd_size;
103         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
104         new->map_base = map;
105         new->map_size = size;
106         new->pid = self->pid;
107         new->errno_ptr = &new->errno_val;
108         new->start = entry;
109         new->start_arg = arg;
110         new->self = new;
111         new->tsd = (void *)tsd;
112         if (attr) new->detached = attr->_a_detach;
113         new->unblock_cancel = self->cancel;
114         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
115         new->tlsdesc[1] = (uintptr_t)new;
116         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
117
118         __synccall_lock();
119
120         a_inc(&libc.threads_minus_1);
121         ret = __uniclone(stack, start, new);
122
123         __synccall_unlock();
124
125         if (ret < 0) {
126                 a_dec(&libc.threads_minus_1);
127                 munmap(map, size);
128                 return EAGAIN;
129         }
130         *res = new;
131         return 0;
132 }
133
134 void pthread_exit(void *result)
135 {
136         struct pthread *self = pthread_self();
137         struct __ptcb cb = { .__next = self->cancelbuf };
138         self->result = result;
139         __pthread_unwind_next(&cb);
140 }