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