blob: ecf93a46617116fb6f7313e888256f16c1b1fde3 [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 Felkerafc35d52012-02-09 02:33:08 -050011void pthread_exit(void *result)
Rich Felker1a9a2ff2011-02-13 19:58:30 -050012{
Rich Felker1ebde9c2011-04-17 17:06:05 -040013 pthread_t self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -040014 int n;
Rich Felker1a9a2ff2011-02-13 19:58:30 -050015
Rich Felkerafc35d52012-02-09 02:33:08 -050016 self->result = result;
17
18 while (self->cancelbuf) {
19 void (*f)(void *) = self->cancelbuf->__f;
20 void *x = self->cancelbuf->__x;
21 self->cancelbuf = self->cancelbuf->__next;
22 f(x);
Rich Felker1ebde9c2011-04-17 17:06:05 -040023 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050024
Rich Felkera6054e32011-04-19 23:09:14 -040025 __pthread_tsd_run_dtors();
Rich Felker1a9a2ff2011-02-13 19:58:30 -050026
Rich Felkerf58c8a02011-06-14 01:25:17 -040027 __lock(&self->exitlock);
28
Rich Felker5fcebcd2011-03-10 18:31:37 -050029 /* Mark this thread dead before decrementing count */
Rich Felker7779dbd2011-06-14 01:35:51 -040030 __lock(&self->killlock);
Rich Felker5fcebcd2011-03-10 18:31:37 -050031 self->dead = 1;
Rich Felker7779dbd2011-06-14 01:35:51 -040032 a_store(&self->killlock, 0);
Rich Felker19eb13b2011-02-19 11:04:36 -050033
Rich Felkerfeee9892011-04-17 11:43:03 -040034 do n = libc.threads_minus_1;
35 while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
36 if (!n) exit(0);
Rich Felkerfb11b6b2011-02-19 10:38:57 -050037
Rich Felker5fcebcd2011-03-10 18:31:37 -050038 if (self->detached && self->map_base) {
Rich Felker99b8a252011-05-07 23:23:58 -040039 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050040 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -050041 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050042
Rich Felker74950b32011-04-06 19:47:50 -040043 __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050044}
Rich Felker0b44a032011-02-12 00:22:29 -050045
Rich Felkercfd892f2012-05-23 14:13:54 -040046void __do_cleanup_push(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -040047{
48 struct pthread *self = pthread_self();
49 cb->__next = self->cancelbuf;
50 self->cancelbuf = cb;
51}
52
Rich Felkercfd892f2012-05-23 14:13:54 -040053void __do_cleanup_pop(struct __ptcb *cb)
Rich Felker5f37fc12011-08-03 19:57:46 -040054{
Rich Felkerafc35d52012-02-09 02:33:08 -050055 __pthread_self()->cancelbuf = cb->__next;
Rich Felker5f37fc12011-08-03 19:57:46 -040056}
57
Rich Felker3f72cda2011-09-18 10:14:37 -040058static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -050059{
Rich Felker3f72cda2011-09-18 10:14:37 -040060 pthread_t self = p;
Rich Felker99b8a252011-05-07 23:23:58 -040061 if (self->unblock_cancel)
Rich Felker4c4e22d2011-05-07 23:37:10 -040062 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker0b44a032011-02-12 00:22:29 -050063 pthread_exit(self->start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -040064 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -050065}
66
Rich Felker0b44a032011-02-12 00:22:29 -050067#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
68
69/* pthread_key_create.c overrides this */
70static const size_t dummy = 0;
71weak_alias(dummy, __pthread_tsd_size);
72
Rich Felkerdba68bf2011-07-30 08:02:14 -040073static FILE *const dummy_file = 0;
74weak_alias(dummy_file, __stdin_used);
75weak_alias(dummy_file, __stdout_used);
76weak_alias(dummy_file, __stderr_used);
77
78static void init_file_lock(FILE *f)
79{
80 if (f && f->lock<0) f->lock = 0;
81}
82
Rich Felker0b44a032011-02-12 00:22:29 -050083int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
84{
Rich Felker0b44a032011-02-12 00:22:29 -050085 int ret;
Rich Felker11e4b922011-05-07 23:39:48 -040086 size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
87 size_t guard = DEFAULT_GUARD_SIZE;
Rich Felker0b44a032011-02-12 00:22:29 -050088 struct pthread *self = pthread_self(), *new;
89 unsigned char *map, *stack, *tsd;
Rich Felker0b44a032011-02-12 00:22:29 -050090
Rich Felker7fd39952011-04-03 16:15:15 -040091 if (!self) return ENOSYS;
Rich Felker9080cc12011-04-17 16:53:54 -040092 if (!libc.threaded) {
Rich Felkerdba68bf2011-07-30 08:02:14 -040093 for (FILE *f=libc.ofl_head; f; f=f->next)
94 init_file_lock(f);
95 init_file_lock(__stdin_used);
96 init_file_lock(__stdout_used);
97 init_file_lock(__stderr_used);
Rich Felker4c4e22d2011-05-07 23:37:10 -040098 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker9080cc12011-04-17 16:53:54 -040099 libc.threaded = 1;
100 }
Rich Felker0b44a032011-02-12 00:22:29 -0500101
Rich Felker11e4b922011-05-07 23:39:48 -0400102 if (attr) {
103 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
104 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
105 }
Rich Felker0b44a032011-02-12 00:22:29 -0500106 size += __pthread_tsd_size;
Rich Felker7e4d7942012-05-04 22:51:59 -0400107 map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker3f39c9b2011-09-27 12:18:44 -0400108 if (map == MAP_FAILED) return EAGAIN;
Rich Felker29fae652011-03-16 11:36:21 -0400109 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -0500110
111 tsd = map + size - __pthread_tsd_size;
112 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
113 new->map_base = map;
114 new->map_size = size;
115 new->pid = self->pid;
116 new->errno_ptr = &new->errno_val;
117 new->start = entry;
118 new->start_arg = arg;
119 new->self = new;
120 new->tsd = (void *)tsd;
Rich Felker11e4b922011-05-07 23:39:48 -0400121 if (attr) new->detached = attr->_a_detach;
Rich Felkerbf619d82011-03-29 12:58:22 -0400122 new->unblock_cancel = self->cancel;
Rich Felker58aa5f42012-05-03 20:42:45 -0400123 new->canary = self->canary ^ (uintptr_t)&new;
Rich Felker3f72cda2011-09-18 10:14:37 -0400124 stack = (void *)new;
Rich Felker0b44a032011-02-12 00:22:29 -0500125
Rich Felkeracb04802011-07-29 22:59:44 -0400126 __synccall_lock();
Rich Felker0b44a032011-02-12 00:22:29 -0500127
128 a_inc(&libc.threads_minus_1);
Rich Felker3f72cda2011-09-18 10:14:37 -0400129 ret = __clone(start, stack, 0x7d8f00, new, &new->tid, new, &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500130
Rich Felkeracb04802011-07-29 22:59:44 -0400131 __synccall_unlock();
Rich Felker0b44a032011-02-12 00:22:29 -0500132
133 if (ret < 0) {
134 a_dec(&libc.threads_minus_1);
135 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500136 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500137 }
138 *res = new;
139 return 0;
140}