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 syscall4(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
30 if (!a_fetch_add(&libc.threads_minus_1, -1))
33 if (self->detached && self->map_base)
34 __unmapself(self->map_base, self->map_size);
39 static void docancel(struct pthread *self)
41 struct __ptcb cb = { .__next = self->cancelbuf };
42 __pthread_unwind_next(&cb);
45 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
47 struct pthread *self = __pthread_self();
49 if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
54 static void cancelpt(int x)
56 struct pthread *self = __pthread_self();
57 if (self->canceldisable) return;
58 self->cancelpoint = x;
59 if (self->cancel) docancel(self);
62 /* "rsyscall" is a mechanism by which a thread can synchronously force all
63 * other threads to perform an arbitrary syscall. It is necessary to work
64 * around the non-conformant implementation of setuid() et al on Linux,
65 * which affect only the calling thread and not the whole process. This
66 * implementation performs some tricks with signal delivery to work around
67 * the fact that it does not keep any list of threads in userspace. */
70 volatile int lock, hold, blocks, cnt;
76 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
78 if (rs.cnt == libc.threads_minus_1) return;
80 if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
81 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
84 __wake(&rs.cnt, 1, 1);
86 __wait(&rs.hold, 0, 1, 1);
88 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
91 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
95 struct pthread *self = __pthread_self();
96 sigaddset(&set, SIGSYSCALL);
100 __wait(&rs.blocks, 0, i, 1);
102 __libc_sigprocmask(SIG_BLOCK, &set, 0);
105 rs.arg[0] = a; rs.arg[1] = b;
106 rs.arg[2] = c; rs.arg[3] = d;
107 rs.arg[4] = d; rs.arg[5] = f;
112 /* Dispatch signals until all threads respond */
113 for (i=libc.threads_minus_1; i; i--)
114 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
115 while ((i=rs.cnt) < libc.threads_minus_1) {
116 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
117 __wait(&rs.cnt, 0, i, 1);
120 /* Handle any lingering signals with no-op */
121 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
123 /* Resume other threads' signal handlers and wait for them */
125 __wake(&rs.hold, -1, 0);
126 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
128 if (rs.err) errno = rs.err, ret = -1;
129 else ret = syscall6(nr, a, b, c, d, e, f);
135 static void init_threads()
137 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
139 libc.cancelpt = cancelpt;
140 libc.rsyscall = rsyscall;
141 sa.sa_sigaction = cancel_handler;
142 __libc_sigaction(SIGCANCEL, &sa, 0);
143 sigaddset(&sa.sa_mask, SIGSYSCALL);
144 sigaddset(&sa.sa_mask, SIGCANCEL);
145 sa.sa_sigaction = rsyscall_handler;
146 __libc_sigaction(SIGSYSCALL, &sa, 0);
147 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
150 static int start(void *p)
152 struct pthread *self = p;
153 pthread_exit(self->start(self->start_arg));
157 int __uniclone(void *, int (*)(), void *);
159 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
161 /* pthread_key_create.c overrides this */
162 static const size_t dummy = 0;
163 weak_alias(dummy, __pthread_tsd_size);
165 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
170 struct pthread *self = pthread_self(), *new;
171 unsigned char *map, *stack, *tsd;
172 static const pthread_attr_t default_attr;
174 if (!self) return errno = ENOSYS;
175 if (!init && ++init) init_threads();
177 if (!attr) attr = &default_attr;
178 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
179 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
180 size += __pthread_tsd_size;
181 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
182 if (!map) return EAGAIN;
183 mprotect(map, guard, PROT_NONE);
185 tsd = map + size - __pthread_tsd_size;
186 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
188 new->map_size = size;
189 new->pid = self->pid;
190 new->errno_ptr = &new->errno_val;
192 new->start_arg = arg;
194 new->tsd = (void *)tsd;
195 new->detached = attr->_a_detach;
197 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
198 new->tlsdesc[1] = (uintptr_t)new;
199 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
201 /* We must synchronize new thread creation with rsyscall
202 * delivery. This looks to be the least expensive way: */
204 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
206 a_inc(&libc.threads_minus_1);
207 ret = __uniclone(stack, start, new);
210 if (rs.lock) __wake(&rs.blocks, 1, 1);
213 a_dec(&libc.threads_minus_1);
221 void pthread_exit(void *result)
223 struct pthread *self = pthread_self();
224 self->result = result;