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