1 #include "pthread_impl.h"
2 #include "stdio_impl.h"
7 weak_alias(dummy_0, __synccall_lock);
8 weak_alias(dummy_0, __synccall_unlock);
9 weak_alias(dummy_0, __pthread_tsd_run_dtors);
11 #ifdef __pthread_unwind_next
12 #undef __pthread_unwind_next
13 #define __pthread_unwind_next __pthread_unwind_next_3
16 void __pthread_unwind_next(struct __ptcb *cb)
18 pthread_t self = pthread_self();
22 self->cancelbuf = cb->__next->__next;
23 longjmp((void *)cb->__next->__jb, 1);
26 __pthread_tsd_run_dtors();
28 __lock(&self->exitlock);
30 /* Mark this thread dead before decrementing count */
31 __lock(&self->killlock);
33 a_store(&self->killlock, 0);
35 do n = libc.threads_minus_1;
36 while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
39 if (self->detached && self->map_base) {
40 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
41 __unmapself(self->map_base, self->map_size);
44 __syscall(SYS_exit, 0);
47 static int start(void *p)
49 struct pthread *self = p;
50 if (self->unblock_cancel)
51 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
52 pthread_exit(self->start(self->start_arg));
56 int __uniclone(void *, int (*)(), void *);
58 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
60 /* pthread_key_create.c overrides this */
61 static const size_t dummy = 0;
62 weak_alias(dummy, __pthread_tsd_size);
64 static FILE *const dummy_file = 0;
65 weak_alias(dummy_file, __stdin_used);
66 weak_alias(dummy_file, __stdout_used);
67 weak_alias(dummy_file, __stderr_used);
69 static void init_file_lock(FILE *f)
71 if (f && f->lock<0) f->lock = 0;
74 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
77 size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
78 size_t guard = DEFAULT_GUARD_SIZE;
79 struct pthread *self = pthread_self(), *new;
80 unsigned char *map, *stack, *tsd;
82 if (!self) return ENOSYS;
84 for (FILE *f=libc.ofl_head; f; f=f->next)
86 init_file_lock(__stdin_used);
87 init_file_lock(__stdout_used);
88 init_file_lock(__stderr_used);
89 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
94 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
95 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
97 size += __pthread_tsd_size;
98 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
99 if (!map) return EAGAIN;
100 if (guard) mprotect(map, guard, PROT_NONE);
102 tsd = map + size - __pthread_tsd_size;
103 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
105 new->map_size = size;
106 new->pid = self->pid;
107 new->errno_ptr = &new->errno_val;
109 new->start_arg = arg;
111 new->tsd = (void *)tsd;
112 if (attr) new->detached = attr->_a_detach;
113 new->unblock_cancel = self->cancel;
114 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
115 new->tlsdesc[1] = (uintptr_t)new;
116 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
120 a_inc(&libc.threads_minus_1);
121 ret = __uniclone(stack, start, new);
126 a_dec(&libc.threads_minus_1);
134 void pthread_exit(void *result)
136 struct pthread *self = pthread_self();
137 struct __ptcb cb = { .__next = self->cancelbuf };
138 self->result = result;
139 __pthread_unwind_next(&cb);