1 #include "pthread_impl.h"
6 weak_alias(dummy_0, __rsyscall_lock);
7 weak_alias(dummy_0, __rsyscall_unlock);
9 static void dummy_1(pthread_t self)
12 weak_alias(dummy_1, __pthread_tsd_run_dtors);
14 #ifdef __pthread_unwind_next
15 #undef __pthread_unwind_next
16 #define __pthread_unwind_next __pthread_unwind_next_3
19 void __pthread_unwind_next(struct __ptcb *cb)
23 if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
25 self = pthread_self();
27 LOCK(&self->exitlock);
29 __pthread_tsd_run_dtors(self);
31 /* Mark this thread dead before decrementing count */
34 if (!a_fetch_add(&libc.threads_minus_1, -1))
37 if (self->detached && self->map_base) {
38 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
39 __unmapself(self->map_base, self->map_size);
42 __syscall(SYS_exit, 0);
45 static void docancel(struct pthread *self)
47 struct __ptcb cb = { .__next = self->cancelbuf };
48 self->canceldisable = 1;
49 self->cancelasync = 0;
50 __pthread_unwind_next(&cb);
53 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
55 struct pthread *self = __pthread_self();
56 if (self->cancel && !self->canceldisable &&
57 (self->cancelasync || (self->cancelpoint==1 && PC_AT_SYS(ctx))))
61 static void cancelpt(int x)
63 struct pthread *self = __pthread_self();
64 if ((self->cancelpoint+=x)==1 && self->cancel
65 && x<2U && !self->canceldisable) docancel(self);
68 static void init_threads()
70 struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
72 libc.lockfile = __lockfile;
73 libc.cancelpt = cancelpt;
75 sigemptyset(&sa.sa_mask);
76 sa.sa_sigaction = cancel_handler;
77 __libc_sigaction(SIGCANCEL, &sa, 0);
79 sigaddset(&sa.sa_mask, SIGSYSCALL);
80 sigaddset(&sa.sa_mask, SIGCANCEL);
81 __libc_sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
84 static int start(void *p)
86 struct pthread *self = p;
87 if (self->unblock_cancel) {
90 sigaddset(&set, SIGCANCEL);
91 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
93 pthread_exit(self->start(self->start_arg));
97 int __uniclone(void *, int (*)(), void *);
99 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
101 /* pthread_key_create.c overrides this */
102 static const size_t dummy = 0;
103 weak_alias(dummy, __pthread_tsd_size);
105 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
110 struct pthread *self = pthread_self(), *new;
111 unsigned char *map, *stack, *tsd;
112 const pthread_attr_t default_attr = { 0 };
114 if (!self) return ENOSYS;
115 if (!init && ++init) init_threads();
117 if (!attr) attr = &default_attr;
118 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
119 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
120 size += __pthread_tsd_size;
121 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
122 if (!map) return EAGAIN;
123 if (guard) mprotect(map, guard, PROT_NONE);
125 tsd = map + size - __pthread_tsd_size;
126 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
128 new->map_size = size;
129 new->pid = self->pid;
130 new->errno_ptr = &new->errno_val;
132 new->start_arg = arg;
134 new->tsd = (void *)tsd;
135 new->detached = attr->_a_detach;
137 new->unblock_cancel = self->cancel;
138 new->result = PTHREAD_CANCELED;
139 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
140 new->tlsdesc[1] = (uintptr_t)new;
141 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
145 a_inc(&libc.threads_minus_1);
146 ret = __uniclone(stack, start, new);
151 a_dec(&libc.threads_minus_1);
159 void pthread_exit(void *result)
161 struct pthread *self = pthread_self();
162 self->result = result;