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