blob: 87bf8166ff85242ad890e0832cdc07c3399b7972 [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 Felkerafc35d52012-02-09 02:33:08 -050046void __do_cleanup_push(struct __ptcb *cb, void (*f)(void *), void *x)
Rich Felker5f37fc12011-08-03 19:57:46 -040047{
48 struct pthread *self = pthread_self();
Rich Felkerafc35d52012-02-09 02:33:08 -050049 cb->__f = f;
50 cb->__x = x;
Rich Felker5f37fc12011-08-03 19:57:46 -040051 cb->__next = self->cancelbuf;
52 self->cancelbuf = cb;
53}
54
Rich Felkerafc35d52012-02-09 02:33:08 -050055void __do_cleanup_pop(struct __ptcb *cb, int run)
Rich Felker5f37fc12011-08-03 19:57:46 -040056{
Rich Felkerafc35d52012-02-09 02:33:08 -050057 __pthread_self()->cancelbuf = cb->__next;
58 if (run) cb->__f(cb->__x);
Rich Felker5f37fc12011-08-03 19:57:46 -040059}
60
Rich Felker3f72cda2011-09-18 10:14:37 -040061static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -050062{
Rich Felker3f72cda2011-09-18 10:14:37 -040063 pthread_t self = p;
Rich Felker99b8a252011-05-07 23:23:58 -040064 if (self->unblock_cancel)
Rich Felker4c4e22d2011-05-07 23:37:10 -040065 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker0b44a032011-02-12 00:22:29 -050066 pthread_exit(self->start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -040067 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -050068}
69
Rich Felker0b44a032011-02-12 00:22:29 -050070#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
71
72/* pthread_key_create.c overrides this */
73static const size_t dummy = 0;
74weak_alias(dummy, __pthread_tsd_size);
75
Rich Felkerdba68bf2011-07-30 08:02:14 -040076static FILE *const dummy_file = 0;
77weak_alias(dummy_file, __stdin_used);
78weak_alias(dummy_file, __stdout_used);
79weak_alias(dummy_file, __stderr_used);
80
81static void init_file_lock(FILE *f)
82{
83 if (f && f->lock<0) f->lock = 0;
84}
85
Rich Felker0b44a032011-02-12 00:22:29 -050086int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
87{
Rich Felker0b44a032011-02-12 00:22:29 -050088 int ret;
Rich Felker11e4b922011-05-07 23:39:48 -040089 size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
90 size_t guard = DEFAULT_GUARD_SIZE;
Rich Felker0b44a032011-02-12 00:22:29 -050091 struct pthread *self = pthread_self(), *new;
92 unsigned char *map, *stack, *tsd;
Rich Felker0b44a032011-02-12 00:22:29 -050093
Rich Felker7fd39952011-04-03 16:15:15 -040094 if (!self) return ENOSYS;
Rich Felker9080cc12011-04-17 16:53:54 -040095 if (!libc.threaded) {
Rich Felkerdba68bf2011-07-30 08:02:14 -040096 for (FILE *f=libc.ofl_head; f; f=f->next)
97 init_file_lock(f);
98 init_file_lock(__stdin_used);
99 init_file_lock(__stdout_used);
100 init_file_lock(__stderr_used);
Rich Felker4c4e22d2011-05-07 23:37:10 -0400101 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker9080cc12011-04-17 16:53:54 -0400102 libc.threaded = 1;
103 }
Rich Felker0b44a032011-02-12 00:22:29 -0500104
Rich Felker11e4b922011-05-07 23:39:48 -0400105 if (attr) {
106 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
107 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
108 }
Rich Felker0b44a032011-02-12 00:22:29 -0500109 size += __pthread_tsd_size;
110 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
Rich Felker3f39c9b2011-09-27 12:18:44 -0400111 if (map == MAP_FAILED) return EAGAIN;
Rich Felker29fae652011-03-16 11:36:21 -0400112 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -0500113
114 tsd = map + size - __pthread_tsd_size;
115 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
116 new->map_base = map;
117 new->map_size = size;
118 new->pid = self->pid;
119 new->errno_ptr = &new->errno_val;
120 new->start = entry;
121 new->start_arg = arg;
122 new->self = new;
123 new->tsd = (void *)tsd;
Rich Felker11e4b922011-05-07 23:39:48 -0400124 if (attr) new->detached = attr->_a_detach;
Rich Felkerbf619d82011-03-29 12:58:22 -0400125 new->unblock_cancel = self->cancel;
Rich Felker3f72cda2011-09-18 10:14:37 -0400126 stack = (void *)new;
Rich Felker0b44a032011-02-12 00:22:29 -0500127
Rich Felkeracb04802011-07-29 22:59:44 -0400128 __synccall_lock();
Rich Felker0b44a032011-02-12 00:22:29 -0500129
130 a_inc(&libc.threads_minus_1);
Rich Felker3f72cda2011-09-18 10:14:37 -0400131 ret = __clone(start, stack, 0x7d8f00, new, &new->tid, new, &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500132
Rich Felkeracb04802011-07-29 22:59:44 -0400133 __synccall_unlock();
Rich Felker0b44a032011-02-12 00:22:29 -0500134
135 if (ret < 0) {
136 a_dec(&libc.threads_minus_1);
137 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500138 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500139 }
140 *res = new;
141 return 0;
142}