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