blob: c6a23955bdd6dde5b12ace70b74130bd2abf1c6d [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "pthread_impl.h"
2
Rich Felkerb2486a82011-04-06 20:27:07 -04003static void dummy_0()
4{
5}
6weak_alias(dummy_0, __rsyscall_lock);
7weak_alias(dummy_0, __rsyscall_unlock);
8
Rich Felkerfd80cfa2011-04-03 02:33:50 -04009static void dummy_1(pthread_t self)
10{
11}
12weak_alias(dummy_1, __pthread_tsd_run_dtors);
13
Rich Felkerea343362011-03-25 22:13:57 -040014#ifdef __pthread_unwind_next
15#undef __pthread_unwind_next
16#define __pthread_unwind_next __pthread_unwind_next_3
17#endif
18
Rich Felker1a9a2ff2011-02-13 19:58:30 -050019void __pthread_unwind_next(struct __ptcb *cb)
20{
Rich Felker1ebde9c2011-04-17 17:06:05 -040021 pthread_t self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -040022 int n;
Rich Felker1a9a2ff2011-02-13 19:58:30 -050023
Rich Felker1ebde9c2011-04-17 17:06:05 -040024 if (cb->__next) {
25 self->cancelbuf = cb->__next->__next;
26 longjmp((void *)cb->__next->__jb, 1);
27 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050028
Rich Felker1a9a2ff2011-02-13 19:58:30 -050029 LOCK(&self->exitlock);
30
Rich Felkerfd80cfa2011-04-03 02:33:50 -040031 __pthread_tsd_run_dtors(self);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050032
Rich Felker5fcebcd2011-03-10 18:31:37 -050033 /* Mark this thread dead before decrementing count */
34 self->dead = 1;
Rich Felker19eb13b2011-02-19 11:04:36 -050035
Rich Felkerfeee9892011-04-17 11:43:03 -040036 do n = libc.threads_minus_1;
37 while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
38 if (!n) exit(0);
Rich Felkerfb11b6b2011-02-19 10:38:57 -050039
Rich Felker5fcebcd2011-03-10 18:31:37 -050040 if (self->detached && self->map_base) {
Rich Felkerc2cd25b2011-04-06 20:32:53 -040041 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050042 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -050043 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050044
Rich Felker74950b32011-04-06 19:47:50 -040045 __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050046}
Rich Felker0b44a032011-02-12 00:22:29 -050047
Rich Felker0b44a032011-02-12 00:22:29 -050048static int start(void *p)
49{
50 struct pthread *self = p;
Rich Felkerbf619d82011-03-29 12:58:22 -040051 if (self->unblock_cancel) {
52 sigset_t set;
53 sigemptyset(&set);
54 sigaddset(&set, SIGCANCEL);
55 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
56 }
Rich Felker0b44a032011-02-12 00:22:29 -050057 pthread_exit(self->start(self->start_arg));
58 return 0;
59}
60
Rich Felker0b2006c2011-02-15 03:24:58 -050061int __uniclone(void *, int (*)(), void *);
Rich Felker0b44a032011-02-12 00:22:29 -050062
63#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
64
65/* pthread_key_create.c overrides this */
66static const size_t dummy = 0;
67weak_alias(dummy, __pthread_tsd_size);
68
69int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
70{
Rich Felker0b44a032011-02-12 00:22:29 -050071 int ret;
72 size_t size, guard;
73 struct pthread *self = pthread_self(), *new;
74 unsigned char *map, *stack, *tsd;
Rich Felkerb2486a82011-04-06 20:27:07 -040075 const pthread_attr_t default_attr = { 0 };
Rich Felker0b44a032011-02-12 00:22:29 -050076
Rich Felker7fd39952011-04-03 16:15:15 -040077 if (!self) return ENOSYS;
Rich Felker9080cc12011-04-17 16:53:54 -040078 if (!libc.threaded) {
79 sigset_t set;
80 sigemptyset(&set);
81 sigaddset(&set, SIGSYSCALL);
82 sigaddset(&set, SIGCANCEL);
83 __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
84 libc.threaded = 1;
85 }
Rich Felker0b44a032011-02-12 00:22:29 -050086
87 if (!attr) attr = &default_attr;
Rich Felkere8827562011-02-17 17:16:20 -050088 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
89 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
Rich Felker0b44a032011-02-12 00:22:29 -050090 size += __pthread_tsd_size;
91 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
92 if (!map) return EAGAIN;
Rich Felker29fae652011-03-16 11:36:21 -040093 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -050094
95 tsd = map + size - __pthread_tsd_size;
96 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
97 new->map_base = map;
98 new->map_size = size;
99 new->pid = self->pid;
100 new->errno_ptr = &new->errno_val;
101 new->start = entry;
102 new->start_arg = arg;
103 new->self = new;
104 new->tsd = (void *)tsd;
Rich Felkere8827562011-02-17 17:16:20 -0500105 new->detached = attr->_a_detach;
Rich Felker0b44a032011-02-12 00:22:29 -0500106 new->attr = *attr;
Rich Felkerbf619d82011-03-29 12:58:22 -0400107 new->unblock_cancel = self->cancel;
Rich Felker0b44a032011-02-12 00:22:29 -0500108 memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
109 new->tlsdesc[1] = (uintptr_t)new;
110 stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
111
Rich Felkerb2486a82011-04-06 20:27:07 -0400112 __rsyscall_lock();
Rich Felker0b44a032011-02-12 00:22:29 -0500113
114 a_inc(&libc.threads_minus_1);
Rich Felker0b2006c2011-02-15 03:24:58 -0500115 ret = __uniclone(stack, start, new);
Rich Felker0b44a032011-02-12 00:22:29 -0500116
Rich Felkerb2486a82011-04-06 20:27:07 -0400117 __rsyscall_unlock();
Rich Felker0b44a032011-02-12 00:22:29 -0500118
119 if (ret < 0) {
120 a_dec(&libc.threads_minus_1);
121 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500122 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500123 }
124 *res = new;
125 return 0;
126}
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500127
128void pthread_exit(void *result)
129{
130 struct pthread *self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -0400131 struct __ptcb cb = { .__next = self->cancelbuf };
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500132 self->result = result;
Rich Felkerfeee9892011-04-17 11:43:03 -0400133 self->canceldisable = 1;
134 self->cancelasync = 0;
135 __pthread_unwind_next(&cb);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500136}