1 #include "pthread_impl.h"
3 static void dummy_1(pthread_t self)
6 weak_alias(dummy_1, __pthread_tsd_run_dtors);
7 weak_alias(dummy_1, __sigtimer_handler);
9 #ifdef __pthread_unwind_next
10 #undef __pthread_unwind_next
11 #define __pthread_unwind_next __pthread_unwind_next_3
14 void __pthread_unwind_next(struct __ptcb *cb)
18 if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
20 self = pthread_self();
22 LOCK(&self->exitlock);
24 __pthread_tsd_run_dtors(self);
26 /* Mark this thread dead before decrementing count */
29 if (!a_fetch_add(&libc.threads_minus_1, -1))
32 if (self->detached && self->map_base) {
33 syscall(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
34 __unmapself(self->map_base, self->map_size);
40 static void docancel(struct pthread *self)
42 struct __ptcb cb = { .__next = self->cancelbuf };
43 self->canceldisable = 1;
44 self->cancelasync = 0;
45 __pthread_unwind_next(&cb);
48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
50 struct pthread *self = __pthread_self();
51 if (si->si_code == SI_TIMER) __sigtimer_handler(self);
52 if (self->cancel && !self->canceldisable &&
53 (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx))))
57 static void cancelpt(int x)
59 struct pthread *self = __pthread_self();
64 if (self->cancel && self->cancelpoint==1 && !self->canceldisable)
71 self->canceldisable += x;
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. */
83 volatile int lock, hold, blocks, cnt;
89 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
91 struct pthread *self = __pthread_self();
94 if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
96 /* Threads which have already decremented themselves from the
97 * thread count must not increment rs.cnt or otherwise act. */
99 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
103 r = __syscall(rs.nr, rs.arg[0], rs.arg[1],
104 rs.arg[2], rs.arg[3], rs.arg[4], rs.arg[5]);
105 if (r < 0) rs.err=-r;
108 __wake(&rs.cnt, 1, 1);
110 __wait(&rs.hold, 0, 1, 1);
112 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
115 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
118 sigset_t set = { 0 };
119 struct pthread *self = __pthread_self();
120 sigaddset(&set, SIGSYSCALL);
123 while ((i=rs.blocks))
124 __wait(&rs.blocks, 0, i, 1);
126 __libc_sigprocmask(SIG_BLOCK, &set, 0);
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;
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);
144 /* Handle any lingering signals with no-op */
145 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
147 /* Resume other threads' signal handlers and wait for them */
149 __wake(&rs.hold, -1, 0);
150 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
152 if (rs.err) errno = rs.err, ret = -1;
153 else ret = syscall(nr, a, b, c, d, e, f);
159 static void init_threads()
161 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
163 libc.lockfile = __lockfile;
164 libc.cancelpt = cancelpt;
165 libc.rsyscall = rsyscall;
167 sigfillset(&sa.sa_mask);
168 sa.sa_sigaction = rsyscall_handler;
169 __libc_sigaction(SIGSYSCALL, &sa, 0);
171 sigemptyset(&sa.sa_mask);
172 sa.sa_sigaction = cancel_handler;
173 __libc_sigaction(SIGCANCEL, &sa, 0);
175 sigaddset(&sa.sa_mask, SIGSYSCALL);
176 sigaddset(&sa.sa_mask, SIGCANCEL);
177 __libc_sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
180 static int start(void *p)
182 struct pthread *self = p;
183 if (self->unblock_cancel) {
186 sigaddset(&set, SIGCANCEL);
187 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
189 pthread_exit(self->start(self->start_arg));
193 int __uniclone(void *, int (*)(), void *);
195 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
197 /* pthread_key_create.c overrides this */
198 static const size_t dummy = 0;
199 weak_alias(dummy, __pthread_tsd_size);
201 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
206 struct pthread *self = pthread_self(), *new;
207 unsigned char *map, *stack, *tsd;
208 static const pthread_attr_t default_attr;
210 if (!self) return ENOSYS;
211 if (!init && ++init) init_threads();
213 if (!attr) attr = &default_attr;
214 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
215 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
216 size += __pthread_tsd_size;
217 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
218 if (!map) return EAGAIN;
219 if (guard) mprotect(map, guard, PROT_NONE);
221 tsd = map + size - __pthread_tsd_size;
222 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
224 new->map_size = size;
225 new->pid = self->pid;
226 new->errno_ptr = &new->errno_val;
228 new->start_arg = arg;
230 new->tsd = (void *)tsd;
231 new->detached = attr->_a_detach;
233 new->unblock_cancel = self->cancel;
234 new->result = PTHREAD_CANCELED;
235 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
236 new->tlsdesc[1] = (uintptr_t)new;
237 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
239 /* We must synchronize new thread creation with rsyscall
240 * delivery. This looks to be the least expensive way: */
242 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
244 a_inc(&libc.threads_minus_1);
245 ret = __uniclone(stack, start, new);
248 if (rs.lock) __wake(&rs.blocks, 1, 1);
251 a_dec(&libc.threads_minus_1);
259 void pthread_exit(void *result)
261 struct pthread *self = pthread_self();
262 self->result = result;