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