1 #include "pthread_impl.h"
3 void __pthread_unwind_next(struct __ptcb *cb)
5 int i, j, not_finished;
8 if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
10 self = pthread_self();
11 if (self->cancel) self->result = PTHREAD_CANCELLED;
13 LOCK(&self->exitlock);
15 not_finished = self->tsd_used;
16 for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
18 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
19 if (self->tsd[i] && libc.tsd_keys[i]) {
20 void *tmp = self->tsd[i];
22 libc.tsd_keys[i](tmp);
28 /* Mark this thread dead before decrementing count */
31 if (!a_fetch_add(&libc.threads_minus_1, -1))
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);
42 static void docancel(struct pthread *self)
44 struct __ptcb cb = { .__next = self->cancelbuf };
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 > 0 || si->si_pid != self->pid) return;
53 if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
58 static void cancelpt(int x)
60 struct pthread *self = __pthread_self();
61 if (self->canceldisable) return;
62 self->cancelpoint = x;
63 if (self->cancel) docancel(self);
66 /* "rsyscall" is a mechanism by which a thread can synchronously force all
67 * other threads to perform an arbitrary syscall. It is necessary to work
68 * around the non-conformant implementation of setuid() et al on Linux,
69 * which affect only the calling thread and not the whole process. This
70 * implementation performs some tricks with signal delivery to work around
71 * the fact that it does not keep any list of threads in userspace. */
74 volatile int lock, hold, blocks, cnt;
80 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
82 struct pthread *self = __pthread_self();
84 if (si->si_code > 0 || si->si_pid != self->pid ||
85 rs.cnt == libc.threads_minus_1) return;
87 /* Threads which have already decremented themselves from the
88 * thread count must not increment rs.cnt or otherwise act. */
90 __wait(&rs.hold, 0, 1, 1);
94 if (syscall(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
95 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
98 __wake(&rs.cnt, 1, 1);
100 __wait(&rs.hold, 0, 1, 1);
102 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
105 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
108 sigset_t set = { 0 };
109 struct pthread *self = __pthread_self();
110 sigaddset(&set, SIGSYSCALL);
113 while ((i=rs.blocks))
114 __wait(&rs.blocks, 0, i, 1);
116 __libc_sigprocmask(SIG_BLOCK, &set, 0);
119 rs.arg[0] = a; rs.arg[1] = b;
120 rs.arg[2] = c; rs.arg[3] = d;
121 rs.arg[4] = d; rs.arg[5] = f;
126 /* Dispatch signals until all threads respond */
127 for (i=libc.threads_minus_1; i; i--)
128 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
129 while ((i=rs.cnt) < libc.threads_minus_1) {
130 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
131 __wait(&rs.cnt, 0, i, 1);
134 /* Handle any lingering signals with no-op */
135 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
137 /* Resume other threads' signal handlers and wait for them */
139 __wake(&rs.hold, -1, 0);
140 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
142 if (rs.err) errno = rs.err, ret = -1;
143 else ret = syscall(nr, a, b, c, d, e, f);
149 static void init_threads()
151 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
153 libc.lockfile = __lockfile;
154 libc.cancelpt = cancelpt;
155 libc.rsyscall = rsyscall;
156 sa.sa_sigaction = cancel_handler;
157 __libc_sigaction(SIGCANCEL, &sa, 0);
158 sigaddset(&sa.sa_mask, SIGSYSCALL);
159 sigaddset(&sa.sa_mask, SIGCANCEL);
160 sa.sa_sigaction = rsyscall_handler;
161 __libc_sigaction(SIGSYSCALL, &sa, 0);
162 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
165 static int start(void *p)
167 struct pthread *self = p;
168 pthread_exit(self->start(self->start_arg));
172 int __uniclone(void *, int (*)(), void *);
174 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
176 /* pthread_key_create.c overrides this */
177 static const size_t dummy = 0;
178 weak_alias(dummy, __pthread_tsd_size);
180 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
185 struct pthread *self = pthread_self(), *new;
186 unsigned char *map, *stack, *tsd;
187 static const pthread_attr_t default_attr;
189 if (!self) return errno = ENOSYS;
190 if (!init && ++init) init_threads();
192 if (!attr) attr = &default_attr;
193 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
194 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
195 size += __pthread_tsd_size;
196 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
197 if (!map) return EAGAIN;
198 if (guard) mprotect(map, guard, PROT_NONE);
200 tsd = map + size - __pthread_tsd_size;
201 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
203 new->map_size = size;
204 new->pid = self->pid;
205 new->errno_ptr = &new->errno_val;
207 new->start_arg = arg;
209 new->tsd = (void *)tsd;
210 new->detached = attr->_a_detach;
212 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
213 new->tlsdesc[1] = (uintptr_t)new;
214 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
216 /* We must synchronize new thread creation with rsyscall
217 * delivery. This looks to be the least expensive way: */
219 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
221 a_inc(&libc.threads_minus_1);
222 ret = __uniclone(stack, start, new);
225 if (rs.lock) __wake(&rs.blocks, 1, 1);
228 a_dec(&libc.threads_minus_1);
236 void pthread_exit(void *result)
238 struct pthread *self = pthread_self();
239 self->result = result;