blob: adef510c5298c810d09b14e8666cd0359ae8c2d3 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "pthread_impl.h"
Rich Felkerdba68bf2011-07-30 08:02:14 -04002#include "stdio_impl.h"
Rich Felker0b44a032011-02-12 00:22:29 -05003
Rich Felkerb2486a82011-04-06 20:27:07 -04004static void dummy_0()
5{
6}
Rich Felkeracb04802011-07-29 22:59:44 -04007weak_alias(dummy_0, __synccall_lock);
8weak_alias(dummy_0, __synccall_unlock);
Rich Felkera6054e32011-04-19 23:09:14 -04009weak_alias(dummy_0, __pthread_tsd_run_dtors);
Rich Felkerfd80cfa2011-04-03 02:33:50 -040010
Rich Felkerea343362011-03-25 22:13:57 -040011#ifdef __pthread_unwind_next
12#undef __pthread_unwind_next
13#define __pthread_unwind_next __pthread_unwind_next_3
14#endif
15
Rich Felker1a9a2ff2011-02-13 19:58:30 -050016void __pthread_unwind_next(struct __ptcb *cb)
17{
Rich Felker1ebde9c2011-04-17 17:06:05 -040018 pthread_t self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -040019 int n;
Rich Felker1a9a2ff2011-02-13 19:58:30 -050020
Rich Felker1ebde9c2011-04-17 17:06:05 -040021 if (cb->__next) {
22 self->cancelbuf = cb->__next->__next;
23 longjmp((void *)cb->__next->__jb, 1);
24 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050025
Rich Felkera6054e32011-04-19 23:09:14 -040026 __pthread_tsd_run_dtors();
Rich Felker1a9a2ff2011-02-13 19:58:30 -050027
Rich Felkerf58c8a02011-06-14 01:25:17 -040028 __lock(&self->exitlock);
29
Rich Felker5fcebcd2011-03-10 18:31:37 -050030 /* Mark this thread dead before decrementing count */
Rich Felker7779dbd2011-06-14 01:35:51 -040031 __lock(&self->killlock);
Rich Felker5fcebcd2011-03-10 18:31:37 -050032 self->dead = 1;
Rich Felker7779dbd2011-06-14 01:35:51 -040033 a_store(&self->killlock, 0);
Rich Felker19eb13b2011-02-19 11:04:36 -050034
Rich Felkerfeee9892011-04-17 11:43:03 -040035 do n = libc.threads_minus_1;
36 while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
37 if (!n) exit(0);
Rich Felkerfb11b6b2011-02-19 10:38:57 -050038
Rich Felker5fcebcd2011-03-10 18:31:37 -050039 if (self->detached && self->map_base) {
Rich Felker99b8a252011-05-07 23:23:58 -040040 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050041 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -050042 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050043
Rich Felker74950b32011-04-06 19:47:50 -040044 __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050045}
Rich Felker0b44a032011-02-12 00:22:29 -050046
Rich Felker0b44a032011-02-12 00:22:29 -050047static int start(void *p)
48{
49 struct pthread *self = p;
Rich Felker99b8a252011-05-07 23:23:58 -040050 if (self->unblock_cancel)
Rich Felker4c4e22d2011-05-07 23:37:10 -040051 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker0b44a032011-02-12 00:22:29 -050052 pthread_exit(self->start(self->start_arg));
53 return 0;
54}
55
Rich Felker0b2006c2011-02-15 03:24:58 -050056int __uniclone(void *, int (*)(), void *);
Rich Felker0b44a032011-02-12 00:22:29 -050057
58#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
59
60/* pthread_key_create.c overrides this */
61static const size_t dummy = 0;
62weak_alias(dummy, __pthread_tsd_size);
63
Rich Felkerdba68bf2011-07-30 08:02:14 -040064static FILE *const dummy_file = 0;
65weak_alias(dummy_file, __stdin_used);
66weak_alias(dummy_file, __stdout_used);
67weak_alias(dummy_file, __stderr_used);
68
69static void init_file_lock(FILE *f)
70{
71 if (f && f->lock<0) f->lock = 0;
72}
73
Rich Felker0b44a032011-02-12 00:22:29 -050074int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
75{
Rich Felker0b44a032011-02-12 00:22:29 -050076 int ret;
Rich Felker11e4b922011-05-07 23:39:48 -040077 size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
78 size_t guard = DEFAULT_GUARD_SIZE;
Rich Felker0b44a032011-02-12 00:22:29 -050079 struct pthread *self = pthread_self(), *new;
80 unsigned char *map, *stack, *tsd;
Rich Felker0b44a032011-02-12 00:22:29 -050081
Rich Felker7fd39952011-04-03 16:15:15 -040082 if (!self) return ENOSYS;
Rich Felker9080cc12011-04-17 16:53:54 -040083 if (!libc.threaded) {
Rich Felkerdba68bf2011-07-30 08:02:14 -040084 for (FILE *f=libc.ofl_head; f; f=f->next)
85 init_file_lock(f);
86 init_file_lock(__stdin_used);
87 init_file_lock(__stdout_used);
88 init_file_lock(__stderr_used);
Rich Felker4c4e22d2011-05-07 23:37:10 -040089 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker9080cc12011-04-17 16:53:54 -040090 libc.threaded = 1;
91 }
Rich Felker0b44a032011-02-12 00:22:29 -050092
Rich Felker11e4b922011-05-07 23:39:48 -040093 if (attr) {
94 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
95 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
96 }
Rich Felker0b44a032011-02-12 00:22:29 -050097 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;
Rich Felker29fae652011-03-16 11:36:21 -0400100 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -0500101
102 tsd = map + size - __pthread_tsd_size;
103 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
104 new->map_base = map;
105 new->map_size = size;
106 new->pid = self->pid;
107 new->errno_ptr = &new->errno_val;
108 new->start = entry;
109 new->start_arg = arg;
110 new->self = new;
111 new->tsd = (void *)tsd;
Rich Felker11e4b922011-05-07 23:39:48 -0400112 if (attr) new->detached = attr->_a_detach;
Rich Felkerbf619d82011-03-29 12:58:22 -0400113 new->unblock_cancel = self->cancel;
Rich Felker0b44a032011-02-12 00:22:29 -0500114 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);
117
Rich Felkeracb04802011-07-29 22:59:44 -0400118 __synccall_lock();
Rich Felker0b44a032011-02-12 00:22:29 -0500119
120 a_inc(&libc.threads_minus_1);
Rich Felker0b2006c2011-02-15 03:24:58 -0500121 ret = __uniclone(stack, start, new);
Rich Felker0b44a032011-02-12 00:22:29 -0500122
Rich Felkeracb04802011-07-29 22:59:44 -0400123 __synccall_unlock();
Rich Felker0b44a032011-02-12 00:22:29 -0500124
125 if (ret < 0) {
126 a_dec(&libc.threads_minus_1);
127 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500128 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500129 }
130 *res = new;
131 return 0;
132}
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500133
134void pthread_exit(void *result)
135{
136 struct pthread *self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -0400137 struct __ptcb cb = { .__next = self->cancelbuf };
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500138 self->result = result;
Rich Felkerfeee9892011-04-17 11:43:03 -0400139 __pthread_unwind_next(&cb);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500140}