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 if (!a_fetch_add(&libc.threads_minus_1, -1))
16 LOCK(&self->exitlock);
18 not_finished = self->tsd_used;
19 for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
21 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
22 if (self->tsd[i] && libc.tsd_keys[i]) {
23 void *tmp = self->tsd[i];
25 libc.tsd_keys[i](tmp);
31 if (self->detached && self->map_base)
32 __unmapself(self->map_base, self->map_size);
37 static void docancel(struct pthread *self)
39 struct __ptcb cb = { .__next = self->cancelbuf };
40 __pthread_unwind_next(&cb);
43 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
45 struct pthread *self = __pthread_self();
47 if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
52 static void cancelpt(int x)
54 struct pthread *self = __pthread_self();
55 if (self->canceldisable) return;
56 self->cancelpoint = x;
57 if (self->cancel) docancel(self);
60 /* "rsyscall" is a mechanism by which a thread can synchronously force all
61 * other threads to perform an arbitrary syscall. It is necessary to work
62 * around the non-conformant implementation of setuid() et al on Linux,
63 * which affect only the calling thread and not the whole process. This
64 * implementation performs some tricks with signal delivery to work around
65 * the fact that it does not keep any list of threads in userspace. */
68 volatile int lock, hold, blocks, cnt;
74 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
76 if (rs.cnt == libc.threads_minus_1) return;
78 if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
79 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
82 __wake(&rs.cnt, 1, 1);
84 __wait(&rs.hold, 0, 1, 1);
86 if (!rs.cnt) __wake(&rs.cnt, 1, 1);
89 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
93 struct pthread *self = __pthread_self();
94 sigaddset(&set, SIGSYSCALL);
98 __wait(&rs.blocks, 0, i, 1);
100 __libc_sigprocmask(SIG_BLOCK, &set, 0);
103 rs.arg[0] = a; rs.arg[1] = b;
104 rs.arg[2] = c; rs.arg[3] = d;
105 rs.arg[4] = d; rs.arg[5] = f;
110 /* Dispatch signals until all threads respond */
111 for (i=libc.threads_minus_1; i; i--)
112 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
113 while ((i=rs.cnt) < libc.threads_minus_1) {
114 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
115 __wait(&rs.cnt, 0, i, 1);
118 /* Handle any lingering signals with no-op */
119 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
121 /* Resume other threads' signal handlers and wait for them */
123 __wake(&rs.hold, -1, 0);
124 while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
126 if (rs.err) errno = rs.err, ret = -1;
127 else ret = syscall6(nr, a, b, c, d, e, f);
133 static void init_threads()
135 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
137 libc.cancelpt = cancelpt;
138 libc.rsyscall = rsyscall;
139 sa.sa_sigaction = cancel_handler;
140 __libc_sigaction(SIGCANCEL, &sa, 0);
141 sigaddset(&sa.sa_mask, SIGSYSCALL);
142 sigaddset(&sa.sa_mask, SIGCANCEL);
143 sa.sa_sigaction = rsyscall_handler;
144 __libc_sigaction(SIGSYSCALL, &sa, 0);
145 sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
148 static int start(void *p)
150 struct pthread *self = p;
151 pthread_exit(self->start(self->start_arg));
155 int __uniclone(void *, int (*)(), void *);
157 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
159 /* pthread_key_create.c overrides this */
160 static const size_t dummy = 0;
161 weak_alias(dummy, __pthread_tsd_size);
163 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
168 struct pthread *self = pthread_self(), *new;
169 unsigned char *map, *stack, *tsd;
170 static const pthread_attr_t default_attr;
172 if (!self) return errno = ENOSYS;
173 if (!init && ++init) init_threads();
175 if (!attr) attr = &default_attr;
176 guard = ROUND(attr->__guardsize + DEFAULT_GUARD_SIZE);
177 size = guard + ROUND(attr->__stacksize + DEFAULT_STACK_SIZE);
178 size += __pthread_tsd_size;
179 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
180 if (!map) return EAGAIN;
181 mprotect(map, guard, PROT_NONE);
183 tsd = map + size - __pthread_tsd_size;
184 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
186 new->map_size = size;
187 new->pid = self->pid;
188 new->errno_ptr = &new->errno_val;
190 new->start_arg = arg;
192 new->tsd = (void *)tsd;
193 new->detached = attr->__detach;
195 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
196 new->tlsdesc[1] = (uintptr_t)new;
197 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
199 /* We must synchronize new thread creation with rsyscall
200 * delivery. This looks to be the least expensive way: */
202 while (rs.lock) __wait(&rs.lock, 0, 1, 1);
204 a_inc(&libc.threads_minus_1);
205 ret = __uniclone(stack, start, new);
208 if (rs.lock) __wake(&rs.blocks, 1, 1);
211 a_dec(&libc.threads_minus_1);
219 void pthread_exit(void *result)
221 struct pthread *self = pthread_self();
222 self->result = result;