1 | #define _GNU_SOURCE
|
---|
2 | #include "pthread_impl.h"
|
---|
3 | #include "stdio_impl.h"
|
---|
4 | #include "libc.h"
|
---|
5 | #include <sys/mman.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <stddef.h>
|
---|
8 |
|
---|
9 | void *__mmap(void *, size_t, int, int, int, off_t);
|
---|
10 | int __munmap(void *, size_t);
|
---|
11 | int __mprotect(void *, size_t, int);
|
---|
12 |
|
---|
13 | static void dummy_0()
|
---|
14 | {
|
---|
15 | }
|
---|
16 | #ifndef __c2__
|
---|
17 | weak_alias(dummy_0, __acquire_ptc);
|
---|
18 | weak_alias(dummy_0, __release_ptc);
|
---|
19 | #else
|
---|
20 | extern void __acquire_ptc();
|
---|
21 | extern void __release_ptc();
|
---|
22 | #endif
|
---|
23 | weak_alias(dummy_0, __pthread_tsd_run_dtors);
|
---|
24 | weak_alias(dummy_0, __do_orphaned_stdio_locks);
|
---|
25 | weak_alias(dummy_0, __dl_thread_cleanup);
|
---|
26 |
|
---|
27 | _Noreturn void __pthread_exit(void *result)
|
---|
28 | {
|
---|
29 | pthread_t self = __pthread_self();
|
---|
30 | sigset_t set;
|
---|
31 |
|
---|
32 | self->canceldisable = 1;
|
---|
33 | self->cancelasync = 0;
|
---|
34 | self->result = result;
|
---|
35 |
|
---|
36 | while (self->cancelbuf) {
|
---|
37 | void (*f)(void *) = self->cancelbuf->__f;
|
---|
38 | void *x = self->cancelbuf->__x;
|
---|
39 | self->cancelbuf = self->cancelbuf->__next;
|
---|
40 | f(x);
|
---|
41 | }
|
---|
42 |
|
---|
43 | __pthread_tsd_run_dtors();
|
---|
44 |
|
---|
45 | __lock(self->exitlock);
|
---|
46 |
|
---|
47 | /* Mark this thread dead before decrementing count */
|
---|
48 | __lock(self->killlock);
|
---|
49 | self->dead = 1;
|
---|
50 |
|
---|
51 | /* Block all signals before decrementing the live thread count.
|
---|
52 | * This is important to ensure that dynamically allocated TLS
|
---|
53 | * is not under-allocated/over-committed, and possibly for other
|
---|
54 | * reasons as well. */
|
---|
55 | __block_all_sigs(&set);
|
---|
56 |
|
---|
57 | /* Wait to unlock the kill lock, which governs functions like
|
---|
58 | * pthread_kill which target a thread id, until signals have
|
---|
59 | * been blocked. This precludes observation of the thread id
|
---|
60 | * as a live thread (with application code running in it) after
|
---|
61 | * the thread was reported dead by ESRCH being returned. */
|
---|
62 | __unlock(self->killlock);
|
---|
63 |
|
---|
64 | /* It's impossible to determine whether this is "the last thread"
|
---|
65 | * until performing the atomic decrement, since multiple threads
|
---|
66 | * could exit at the same time. For the last thread, revert the
|
---|
67 | * decrement and unblock signals to give the atexit handlers and
|
---|
68 | * stdio cleanup code a consistent state. */
|
---|
69 | if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
|
---|
70 | libc.threads_minus_1 = 0;
|
---|
71 | __restore_sigs(&set);
|
---|
72 | exit(0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Process robust list in userspace to handle non-pshared mutexes
|
---|
76 | * and the detached thread case where the robust list head will
|
---|
77 | * be invalid when the kernel would process it. */
|
---|
78 | __vm_lock();
|
---|
79 | volatile void *volatile *rp;
|
---|
80 | while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
|
---|
81 | pthread_mutex_t *m = (void *)((char *)rp
|
---|
82 | - offsetof(pthread_mutex_t, _m_next));
|
---|
83 | int waiters = m->_m_waiters;
|
---|
84 | int priv = (m->_m_type & 128) ^ 128;
|
---|
85 | self->robust_list.pending = rp;
|
---|
86 | self->robust_list.head = *rp;
|
---|
87 | int cont = a_swap(&m->_m_lock, 0x40000000);
|
---|
88 | self->robust_list.pending = 0;
|
---|
89 | if (cont < 0 || waiters)
|
---|
90 | __wake(&m->_m_lock, 1, priv);
|
---|
91 | }
|
---|
92 | __vm_unlock();
|
---|
93 |
|
---|
94 | __do_orphaned_stdio_locks();
|
---|
95 | __dl_thread_cleanup();
|
---|
96 |
|
---|
97 | if (self->detached && self->map_base) {
|
---|
98 | /* Detached threads must avoid the kernel clear_child_tid
|
---|
99 | * feature, since the virtual address will have been
|
---|
100 | * unmapped and possibly already reused by a new mapping
|
---|
101 | * at the time the kernel would perform the write. In
|
---|
102 | * the case of threads that started out detached, the
|
---|
103 | * initial clone flags are correct, but if the thread was
|
---|
104 | * detached later (== 2), we need to clear it here. */
|
---|
105 | if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
|
---|
106 |
|
---|
107 | /* Robust list will no longer be valid, and was already
|
---|
108 | * processed above, so unregister it with the kernel. */
|
---|
109 | if (self->robust_list.off)
|
---|
110 | __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
|
---|
111 |
|
---|
112 | /* Since __unmapself bypasses the normal munmap code path,
|
---|
113 | * explicitly wait for vmlock holders first. */
|
---|
114 | __vm_wait();
|
---|
115 |
|
---|
116 | /* The following call unmaps the thread's stack mapping
|
---|
117 | * and then exits without touching the stack. */
|
---|
118 | __unmapself(self->map_base, self->map_size);
|
---|
119 | }
|
---|
120 |
|
---|
121 | for (;;) __syscall(SYS_exit, 0);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void __do_cleanup_push(struct __ptcb *cb)
|
---|
125 | {
|
---|
126 | struct pthread *self = __pthread_self();
|
---|
127 | cb->__next = self->cancelbuf;
|
---|
128 | self->cancelbuf = cb;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void __do_cleanup_pop(struct __ptcb *cb)
|
---|
132 | {
|
---|
133 | __pthread_self()->cancelbuf = cb->__next;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static int start(void *p)
|
---|
137 | {
|
---|
138 | pthread_t self = p;
|
---|
139 | /* States for startlock:
|
---|
140 | * 0 = no need for start sync
|
---|
141 | * 1 = waiting for parent to do work
|
---|
142 | * 2 = failure in parent, child must abort
|
---|
143 | * 3 = success in parent, child must restore sigmask */
|
---|
144 | if (self->startlock[0]) {
|
---|
145 | __wait(self->startlock, 0, 1, 1);
|
---|
146 | if (self->startlock[0] == 2) {
|
---|
147 | self->detached = 2;
|
---|
148 | pthread_exit(0);
|
---|
149 | }
|
---|
150 | __restore_sigs(self->sigmask);
|
---|
151 | }
|
---|
152 | if (self->unblock_cancel)
|
---|
153 | __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
|
---|
154 | SIGPT_SET, 0, _NSIG/8);
|
---|
155 | __pthread_exit(self->start(self->start_arg));
|
---|
156 | return 0;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int start_c11(void *p)
|
---|
160 | {
|
---|
161 | pthread_t self = p;
|
---|
162 | int (*start)(void*) = (int(*)(void*)) self->start;
|
---|
163 | __pthread_exit((void *)(uintptr_t)start(self->start_arg));
|
---|
164 | return 0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
|
---|
168 |
|
---|
169 | /* pthread_key_create.c overrides this */
|
---|
170 | #ifndef __c2__
|
---|
171 | static volatile size_t dummy = 0;
|
---|
172 | weak_alias(dummy, __pthread_tsd_size);
|
---|
173 | static void *dummy_tsd[1] = { 0 };
|
---|
174 | weak_alias(dummy_tsd, __pthread_tsd_main);
|
---|
175 | #else
|
---|
176 | extern volatile size_t __pthread_tsd_size;
|
---|
177 | extern void *__pthread_tsd_main;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | volatile int __block_new_threads = 0;
|
---|
181 | size_t __default_stacksize = DEFAULT_STACK_SIZE;
|
---|
182 | size_t __default_guardsize = DEFAULT_GUARD_SIZE;
|
---|
183 |
|
---|
184 | #ifndef __c2__
|
---|
185 | static FILE *volatile dummy_file = 0;
|
---|
186 | weak_alias(dummy_file, __stdin_used);
|
---|
187 | weak_alias(dummy_file, __stdout_used);
|
---|
188 | weak_alias(dummy_file, __stderr_used);
|
---|
189 | #else
|
---|
190 | extern FILE *volatile __stdin_used;
|
---|
191 | extern FILE *volatile __stdout_used;
|
---|
192 | extern FILE *volatile __stderr_used;
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | static void init_file_lock(FILE *f)
|
---|
196 | {
|
---|
197 | if (f && f->lock<0) f->lock = 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void *__copy_tls(unsigned char *);
|
---|
201 |
|
---|
202 | int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
|
---|
203 | {
|
---|
204 | int ret, c11 = (attrp == __ATTRP_C11_THREAD);
|
---|
205 | size_t size, guard;
|
---|
206 | struct pthread *self, *new;
|
---|
207 | unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
|
---|
208 | unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
|
---|
209 | | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
|
---|
210 | | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
|
---|
211 | int do_sched = 0;
|
---|
212 | pthread_attr_t attr = { 0 };
|
---|
213 |
|
---|
214 | if (!libc.can_do_threads) return ENOSYS;
|
---|
215 | self = __pthread_self();
|
---|
216 | if (!libc.threaded) {
|
---|
217 | for (FILE *f=*__ofl_lock(); f; f=f->next)
|
---|
218 | init_file_lock(f);
|
---|
219 | __ofl_unlock();
|
---|
220 | init_file_lock(__stdin_used);
|
---|
221 | init_file_lock(__stdout_used);
|
---|
222 | init_file_lock(__stderr_used);
|
---|
223 | __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
|
---|
224 | self->tsd = (void **)__pthread_tsd_main;
|
---|
225 | libc.threaded = 1;
|
---|
226 | }
|
---|
227 | if (attrp && !c11) attr = *attrp;
|
---|
228 |
|
---|
229 | __acquire_ptc();
|
---|
230 | if (!attrp || c11) {
|
---|
231 | attr._a_stacksize = __default_stacksize;
|
---|
232 | attr._a_guardsize = __default_guardsize;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
|
---|
236 |
|
---|
237 | if (attr._a_stackaddr) {
|
---|
238 | size_t need = libc.tls_size + __pthread_tsd_size;
|
---|
239 | size = attr._a_stacksize;
|
---|
240 | stack = (void *)(attr._a_stackaddr & -16);
|
---|
241 | stack_limit = (void *)(attr._a_stackaddr - size);
|
---|
242 | /* Use application-provided stack for TLS only when
|
---|
243 | * it does not take more than ~12% or 2k of the
|
---|
244 | * application's stack space. */
|
---|
245 | if (need < size/8 && need < 2048) {
|
---|
246 | tsd = stack - __pthread_tsd_size;
|
---|
247 | stack = tsd - libc.tls_size;
|
---|
248 | memset(stack, 0, need);
|
---|
249 | } else {
|
---|
250 | size = ROUND(need);
|
---|
251 | guard = 0;
|
---|
252 | }
|
---|
253 | } else {
|
---|
254 | guard = ROUND(attr._a_guardsize);
|
---|
255 | size = guard + ROUND(attr._a_stacksize
|
---|
256 | + libc.tls_size + __pthread_tsd_size);
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (!tsd) {
|
---|
260 | if (guard) {
|
---|
261 | map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
|
---|
262 | if (map == MAP_FAILED) goto fail;
|
---|
263 | if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
|
---|
264 | && errno != ENOSYS) {
|
---|
265 | __munmap(map, size);
|
---|
266 | goto fail;
|
---|
267 | }
|
---|
268 | } else {
|
---|
269 | map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
|
---|
270 | if (map == MAP_FAILED) goto fail;
|
---|
271 | }
|
---|
272 | tsd = map + size - __pthread_tsd_size;
|
---|
273 | if (!stack) {
|
---|
274 | stack = tsd - libc.tls_size;
|
---|
275 | stack_limit = map + guard;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | new = __copy_tls(tsd - libc.tls_size);
|
---|
280 | new->map_base = map;
|
---|
281 | new->map_size = size;
|
---|
282 | new->stack = stack;
|
---|
283 | new->stack_size = stack - stack_limit;
|
---|
284 | new->start = entry;
|
---|
285 | new->start_arg = arg;
|
---|
286 | new->self = new;
|
---|
287 | new->tsd = (void *)tsd;
|
---|
288 | new->locale = &libc.global_locale;
|
---|
289 | if (attr._a_detach) {
|
---|
290 | new->detached = 1;
|
---|
291 | flags -= CLONE_CHILD_CLEARTID;
|
---|
292 | }
|
---|
293 | if (attr._a_sched) {
|
---|
294 | do_sched = new->startlock[0] = 1;
|
---|
295 | __block_app_sigs(new->sigmask);
|
---|
296 | }
|
---|
297 | new->robust_list.head = &new->robust_list.head;
|
---|
298 | new->unblock_cancel = self->cancel;
|
---|
299 | new->CANARY = self->CANARY;
|
---|
300 |
|
---|
301 | a_inc(&libc.threads_minus_1);
|
---|
302 | ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
|
---|
303 |
|
---|
304 | __release_ptc();
|
---|
305 |
|
---|
306 | if (do_sched) {
|
---|
307 | __restore_sigs(new->sigmask);
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (ret < 0) {
|
---|
311 | a_dec(&libc.threads_minus_1);
|
---|
312 | if (map) __munmap(map, size);
|
---|
313 | return EAGAIN;
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (do_sched) {
|
---|
317 | ret = __syscall(SYS_sched_setscheduler, new->tid,
|
---|
318 | attr._a_policy, &attr._a_prio);
|
---|
319 | a_store(new->startlock, ret<0 ? 2 : 3);
|
---|
320 | __wake(new->startlock, 1, 1);
|
---|
321 | if (ret < 0) return -ret;
|
---|
322 | }
|
---|
323 |
|
---|
324 | *res = new;
|
---|
325 | return 0;
|
---|
326 | fail:
|
---|
327 | __release_ptc();
|
---|
328 | return EAGAIN;
|
---|
329 | }
|
---|
330 |
|
---|
331 | #ifndef __c2__
|
---|
332 | weak_alias(__pthread_exit, pthread_exit);
|
---|
333 | weak_alias(__pthread_create, pthread_create);
|
---|
334 | #else
|
---|
335 | _Noreturn void pthread_exit(void *result)
|
---|
336 | {
|
---|
337 | __pthread_exit(result);
|
---|
338 | }
|
---|
339 |
|
---|
340 | int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
|
---|
341 | {
|
---|
342 | return __pthread_create(res, attrp, entry, arg);
|
---|
343 | }
|
---|
344 | #endif
|
---|