d829fa26950c30d841cd7f3a946c6feda997d09b
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 void __pthread_unwind_next(struct __ptcb *cb)
4 {
5         int i, j, not_finished;
6         pthread_t self;
7
8         if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
9
10         self = pthread_self();
11         if (self->cancel) self->result = PTHREAD_CANCELLED;
12
13         LOCK(&self->exitlock);
14
15         not_finished = self->tsd_used;
16         for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
17                 not_finished = 0;
18                 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
19                         if (self->tsd[i] && libc.tsd_keys[i]) {
20                                 void *tmp = self->tsd[i];
21                                 self->tsd[i] = 0;
22                                 libc.tsd_keys[i](tmp);
23                                 not_finished = 1;
24                         }
25                 }
26         }
27
28         /* Mark this thread dead before decrementing count */
29         self->dead = 1;
30
31         if (!a_fetch_add(&libc.threads_minus_1, -1))
32                 exit(0);
33
34         if (self->detached && self->map_base) {
35                 syscall4(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
36                 __unmapself(self->map_base, self->map_size);
37         }
38
39         __syscall_exit(0);
40 }
41
42 static void docancel(struct pthread *self)
43 {
44         struct __ptcb cb = { .__next = self->cancelbuf };
45         __pthread_unwind_next(&cb);
46 }
47
48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
49 {
50         struct pthread *self = __pthread_self();
51         if (si->si_code > 0 || si->si_pid != self->pid) return;
52         self->cancel = 1;
53         if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
54                 return;
55         docancel(self);
56 }
57
58 static void cancelpt(int x)
59 {
60         struct pthread *self = __pthread_self();
61         if (self->canceldisable) return;
62         self->cancelpoint = x;
63         if (self->cancel) docancel(self);
64 }
65
66 /* "rsyscall" is a mechanism by which a thread can synchronously force all
67  * other threads to perform an arbitrary syscall. It is necessary to work
68  * around the non-conformant implementation of setuid() et al on Linux,
69  * which affect only the calling thread and not the whole process. This
70  * implementation performs some tricks with signal delivery to work around
71  * the fact that it does not keep any list of threads in userspace. */
72
73 static struct {
74         volatile int lock, hold, blocks, cnt;
75         unsigned long arg[6];
76         int nr;
77         int err;
78 } rs;
79
80 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
81 {
82         struct pthread *self = __pthread_self();
83
84         if (si->si_code > 0 || si->si_pid != self->pid ||
85                 rs.cnt == libc.threads_minus_1) return;
86
87         /* Threads which have already decremented themselves from the
88          * thread count must not increment rs.cnt or otherwise act. */
89         if (self->dead) {
90                 __wait(&rs.hold, 0, 1, 1);
91                 return;
92         }
93
94         if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
95                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
96
97         a_inc(&rs.cnt);
98         __wake(&rs.cnt, 1, 1);
99         while(rs.hold)
100                 __wait(&rs.hold, 0, 1, 1);
101         a_dec(&rs.cnt);
102         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
103 }
104
105 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
106 {
107         int i, ret;
108         sigset_t set = { 0 };
109         struct pthread *self = __pthread_self();
110         sigaddset(&set, SIGSYSCALL);
111
112         LOCK(&rs.lock);
113         while ((i=rs.blocks))
114                 __wait(&rs.blocks, 0, i, 1);
115
116         __libc_sigprocmask(SIG_BLOCK, &set, 0);
117
118         rs.nr = nr;
119         rs.arg[0] = a; rs.arg[1] = b;
120         rs.arg[2] = c; rs.arg[3] = d;
121         rs.arg[4] = d; rs.arg[5] = f;
122         rs.hold = 1;
123         rs.err = 0;
124         rs.cnt = 0;
125
126         /* Dispatch signals until all threads respond */
127         for (i=libc.threads_minus_1; i; i--)
128                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
129         while ((i=rs.cnt) < libc.threads_minus_1) {
130                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
131                 __wait(&rs.cnt, 0, i, 1);
132         }
133
134         /* Handle any lingering signals with no-op */
135         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
136
137         /* Resume other threads' signal handlers and wait for them */
138         rs.hold = 0;
139         __wake(&rs.hold, -1, 0);
140         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
141
142         if (rs.err) errno = rs.err, ret = -1;
143         else ret = syscall6(nr, a, b, c, d, e, f);
144
145         UNLOCK(&rs.lock);
146         return ret;
147 }
148
149 static void init_threads()
150 {
151         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
152         libc.lock = __lock;
153         libc.cancelpt = cancelpt;
154         libc.rsyscall = rsyscall;
155         sa.sa_sigaction = cancel_handler;
156         __libc_sigaction(SIGCANCEL, &sa, 0);
157         sigaddset(&sa.sa_mask, SIGSYSCALL);
158         sigaddset(&sa.sa_mask, SIGCANCEL);
159         sa.sa_sigaction = rsyscall_handler;
160         __libc_sigaction(SIGSYSCALL, &sa, 0);
161         sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
162 }
163
164 static int start(void *p)
165 {
166         struct pthread *self = p;
167         pthread_exit(self->start(self->start_arg));
168         return 0;
169 }
170
171 int __uniclone(void *, int (*)(), void *);
172
173 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
174
175 /* pthread_key_create.c overrides this */
176 static const size_t dummy = 0;
177 weak_alias(dummy, __pthread_tsd_size);
178
179 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
180 {
181         static int init;
182         int ret;
183         size_t size, guard;
184         struct pthread *self = pthread_self(), *new;
185         unsigned char *map, *stack, *tsd;
186         static const pthread_attr_t default_attr;
187
188         if (!self) return errno = ENOSYS;
189         if (!init && ++init) init_threads();
190
191         if (!attr) attr = &default_attr;
192         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
193         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
194         size += __pthread_tsd_size;
195         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
196         if (!map) return EAGAIN;
197         mprotect(map, guard, PROT_NONE);
198
199         tsd = map + size - __pthread_tsd_size;
200         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
201         new->map_base = map;
202         new->map_size = size;
203         new->pid = self->pid;
204         new->errno_ptr = &new->errno_val;
205         new->start = entry;
206         new->start_arg = arg;
207         new->self = new;
208         new->tsd = (void *)tsd;
209         new->detached = attr->_a_detach;
210         new->attr = *attr;
211         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
212         new->tlsdesc[1] = (uintptr_t)new;
213         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
214
215         /* We must synchronize new thread creation with rsyscall
216          * delivery. This looks to be the least expensive way: */
217         a_inc(&rs.blocks);
218         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
219
220         a_inc(&libc.threads_minus_1);
221         ret = __uniclone(stack, start, new);
222
223         a_dec(&rs.blocks);
224         if (rs.lock) __wake(&rs.blocks, 1, 1);
225
226         if (ret < 0) {
227                 a_dec(&libc.threads_minus_1);
228                 munmap(map, size);
229                 return EAGAIN;
230         }
231         *res = new;
232         return 0;
233 }
234
235 void pthread_exit(void *result)
236 {
237         struct pthread *self = pthread_self();
238         self->result = result;
239         docancel(self);
240 }