1 #include "pthread_impl.h"
3 #ifdef __pthread_unwind_next
4 #undef __pthread_unwind_next
5 #define __pthread_unwind_next __pthread_unwind_next_3
8 void __pthread_unwind_next(struct __ptcb *cb)
10 int i, j, not_finished;
13 if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
15 self = pthread_self();
16 if (self->cancel) self->result = PTHREAD_CANCELLED;
18 LOCK(&self->exitlock);
20 not_finished = self->tsd_used;
21 for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
23 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
24 if (self->tsd[i] && libc.tsd_keys[i]) {
25 void *tmp = self->tsd[i];
27 libc.tsd_keys[i](tmp);
33 /* Mark this thread dead before decrementing count */
36 if (!a_fetch_add(&libc.threads_minus_1, -1))
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);
47 static void docancel(struct pthread *self)
49 struct __ptcb cb = { .__next = self->cancelbuf };
51 self->canceldisable = 1;
52 self->cancelasync = 0;
54 sigaddset(&set, SIGCANCEL);
55 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
56 __pthread_unwind_next(&cb);
59 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
61 struct pthread *self = __pthread_self();
62 if (si->si_code > 0 || si->si_pid != self->pid) return;
64 if (self->canceldisable) return;
65 if (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx)))
69 static void cancelpt(int x)
71 struct pthread *self = __pthread_self();
72 if (self->canceldisable) return;
73 if ((self->cancelpoint+=x)==1 && x>=0 && self->cancel)
77 /* "rsyscall" is a mechanism by which a thread can synchronously force all
78 * other threads to perform an arbitrary syscall. It is necessary to work
79 * around the non-conformant implementation of setuid() et al on Linux,
80 * which affect only the calling thread and not the whole process. This
81 * implementation performs some tricks with signal delivery to work around
82 * the fact that it does not keep any list of threads in userspace. */
85 volatile int lock, hold, blocks, cnt;
91 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
93 struct pthread *self = __pthread_self();
95 if (si->si_code > 0 || si->si_pid != self->pid ||
96 rs.cnt == libc.threads_minus_1) return;
98 /* Threads which have already decremented themselves from the
99 * thread count must not increment rs.cnt or otherwise act. */
101 __wait(&rs.hold, 0, 1, 1);
105 if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
106 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
109 __wake(&rs.cnt, 1, 1);
111 __wait(&rs.hold, 0, 1, 1);
113 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
116 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
119 sigset_t set = { 0 };
120 struct pthread *self = __pthread_self();
121 sigaddset(&set, SIGSYSCALL);
124 while ((i=rs.blocks))
125 __wait(&rs.blocks, 0, i, 1);
127 __libc_sigprocmask(SIG_BLOCK, &set, 0);
130 rs.arg[0] = a; rs.arg[1] = b;
131 rs.arg[2] = c; rs.arg[3] = d;
132 rs.arg[4] = d; rs.arg[5] = f;
137 /* Dispatch signals until all threads respond */
138 for (i=libc.threads_minus_1; i; i--)
139 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
140 while ((i=rs.cnt) < libc.threads_minus_1) {
141 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
142 __wait(&rs.cnt, 0, i, 1);
145 /* Handle any lingering signals with no-op */
146 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
148 /* Resume other threads' signal handlers and wait for them */
150 __wake(&rs.hold, -1, 0);
151 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
153 if (rs.err) errno = rs.err, ret = -1;
154 else ret = syscall(nr, a, b, c, d, e, f);
160 static void init_threads()
162 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
164 libc.lockfile = __lockfile;
165 libc.cancelpt = cancelpt;
166 libc.rsyscall = rsyscall;
167 sa.sa_sigaction = cancel_handler;
168 __libc_sigaction(SIGCANCEL, &sa, 0);
169 sigaddset(&sa.sa_mask, SIGSYSCALL);
170 sigaddset(&sa.sa_mask, SIGCANCEL);
171 sa.sa_sigaction = rsyscall_handler;
172 __libc_sigaction(SIGSYSCALL, &sa, 0);
173 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
176 static int start(void *p)
178 struct pthread *self = p;
179 pthread_exit(self->start(self->start_arg));
183 int __uniclone(void *, int (*)(), void *);
185 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
187 /* pthread_key_create.c overrides this */
188 static const size_t dummy = 0;
189 weak_alias(dummy, __pthread_tsd_size);
191 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
196 struct pthread *self = pthread_self(), *new;
197 unsigned char *map, *stack, *tsd;
198 static const pthread_attr_t default_attr;
200 if (!self) return errno = ENOSYS;
201 if (!init && ++init) init_threads();
203 if (!attr) attr = &default_attr;
204 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
205 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
206 size += __pthread_tsd_size;
207 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
208 if (!map) return EAGAIN;
209 if (guard) mprotect(map, guard, PROT_NONE);
211 tsd = map + size - __pthread_tsd_size;
212 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
214 new->map_size = size;
215 new->pid = self->pid;
216 new->errno_ptr = &new->errno_val;
218 new->start_arg = arg;
220 new->tsd = (void *)tsd;
221 new->detached = attr->_a_detach;
223 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
224 new->tlsdesc[1] = (uintptr_t)new;
225 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
227 /* We must synchronize new thread creation with rsyscall
228 * delivery. This looks to be the least expensive way: */
230 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
232 a_inc(&libc.threads_minus_1);
233 ret = __uniclone(stack, start, new);
236 if (rs.lock) __wake(&rs.blocks, 1, 1);
239 a_dec(&libc.threads_minus_1);
247 void pthread_exit(void *result)
249 struct pthread *self = pthread_self();
250 self->result = result;