blob: f7768d8d03dd1dc9e8c5c26c974d9a86a8176571 [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 Felker730bee72011-08-03 19:45:21 -040011void __pthread_do_unwind(struct __ptcb *cb)
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 Felker1ebde9c2011-04-17 17:06:05 -040016 if (cb->__next) {
17 self->cancelbuf = cb->__next->__next;
18 longjmp((void *)cb->__next->__jb, 1);
19 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050020
Rich Felkera6054e32011-04-19 23:09:14 -040021 __pthread_tsd_run_dtors();
Rich Felker1a9a2ff2011-02-13 19:58:30 -050022
Rich Felkerf58c8a02011-06-14 01:25:17 -040023 __lock(&self->exitlock);
24
Rich Felker5fcebcd2011-03-10 18:31:37 -050025 /* Mark this thread dead before decrementing count */
Rich Felker7779dbd2011-06-14 01:35:51 -040026 __lock(&self->killlock);
Rich Felker5fcebcd2011-03-10 18:31:37 -050027 self->dead = 1;
Rich Felker7779dbd2011-06-14 01:35:51 -040028 a_store(&self->killlock, 0);
Rich Felker19eb13b2011-02-19 11:04:36 -050029
Rich Felkerfeee9892011-04-17 11:43:03 -040030 do n = libc.threads_minus_1;
31 while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
32 if (!n) exit(0);
Rich Felkerfb11b6b2011-02-19 10:38:57 -050033
Rich Felker5fcebcd2011-03-10 18:31:37 -050034 if (self->detached && self->map_base) {
Rich Felker99b8a252011-05-07 23:23:58 -040035 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1},0,8);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050036 __unmapself(self->map_base, self->map_size);
Rich Felker5fcebcd2011-03-10 18:31:37 -050037 }
Rich Felker1a9a2ff2011-02-13 19:58:30 -050038
Rich Felker74950b32011-04-06 19:47:50 -040039 __syscall(SYS_exit, 0);
Rich Felker1a9a2ff2011-02-13 19:58:30 -050040}
Rich Felker0b44a032011-02-12 00:22:29 -050041
Rich Felker5f37fc12011-08-03 19:57:46 -040042void __pthread_do_register(struct __ptcb *cb)
43{
44 struct pthread *self = pthread_self();
45 cb->__next = self->cancelbuf;
46 self->cancelbuf = cb;
47}
48
49void __pthread_do_unregister(struct __ptcb *cb)
50{
51 struct pthread *self = __pthread_self();
52 self->cancelbuf = self->cancelbuf->__next;
53}
54
Rich Felker3f72cda2011-09-18 10:14:37 -040055static int start(void *p)
Rich Felker0b44a032011-02-12 00:22:29 -050056{
Rich Felker3f72cda2011-09-18 10:14:37 -040057 pthread_t self = p;
Rich Felker99b8a252011-05-07 23:23:58 -040058 if (self->unblock_cancel)
Rich Felker4c4e22d2011-05-07 23:37:10 -040059 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker0b44a032011-02-12 00:22:29 -050060 pthread_exit(self->start(self->start_arg));
Rich Felker3f72cda2011-09-18 10:14:37 -040061 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -050062}
63
Rich Felker0b44a032011-02-12 00:22:29 -050064#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
65
66/* pthread_key_create.c overrides this */
67static const size_t dummy = 0;
68weak_alias(dummy, __pthread_tsd_size);
69
Rich Felkerdba68bf2011-07-30 08:02:14 -040070static FILE *const dummy_file = 0;
71weak_alias(dummy_file, __stdin_used);
72weak_alias(dummy_file, __stdout_used);
73weak_alias(dummy_file, __stderr_used);
74
75static void init_file_lock(FILE *f)
76{
77 if (f && f->lock<0) f->lock = 0;
78}
79
Rich Felker0b44a032011-02-12 00:22:29 -050080int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
81{
Rich Felker0b44a032011-02-12 00:22:29 -050082 int ret;
Rich Felker11e4b922011-05-07 23:39:48 -040083 size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
84 size_t guard = DEFAULT_GUARD_SIZE;
Rich Felker0b44a032011-02-12 00:22:29 -050085 struct pthread *self = pthread_self(), *new;
86 unsigned char *map, *stack, *tsd;
Rich Felker0b44a032011-02-12 00:22:29 -050087
Rich Felker7fd39952011-04-03 16:15:15 -040088 if (!self) return ENOSYS;
Rich Felker9080cc12011-04-17 16:53:54 -040089 if (!libc.threaded) {
Rich Felkerdba68bf2011-07-30 08:02:14 -040090 for (FILE *f=libc.ofl_head; f; f=f->next)
91 init_file_lock(f);
92 init_file_lock(__stdin_used);
93 init_file_lock(__stdout_used);
94 init_file_lock(__stderr_used);
Rich Felker4c4e22d2011-05-07 23:37:10 -040095 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
Rich Felker9080cc12011-04-17 16:53:54 -040096 libc.threaded = 1;
97 }
Rich Felker0b44a032011-02-12 00:22:29 -050098
Rich Felker11e4b922011-05-07 23:39:48 -040099 if (attr) {
100 guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
101 size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
102 }
Rich Felker0b44a032011-02-12 00:22:29 -0500103 size += __pthread_tsd_size;
104 map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
105 if (!map) return EAGAIN;
Rich Felker29fae652011-03-16 11:36:21 -0400106 if (guard) mprotect(map, guard, PROT_NONE);
Rich Felker0b44a032011-02-12 00:22:29 -0500107
108 tsd = map + size - __pthread_tsd_size;
109 new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
110 new->map_base = map;
111 new->map_size = size;
112 new->pid = self->pid;
113 new->errno_ptr = &new->errno_val;
114 new->start = entry;
115 new->start_arg = arg;
116 new->self = new;
117 new->tsd = (void *)tsd;
Rich Felker11e4b922011-05-07 23:39:48 -0400118 if (attr) new->detached = attr->_a_detach;
Rich Felkerbf619d82011-03-29 12:58:22 -0400119 new->unblock_cancel = self->cancel;
Rich Felker3f72cda2011-09-18 10:14:37 -0400120 stack = (void *)new;
Rich Felker0b44a032011-02-12 00:22:29 -0500121
Rich Felkeracb04802011-07-29 22:59:44 -0400122 __synccall_lock();
Rich Felker0b44a032011-02-12 00:22:29 -0500123
124 a_inc(&libc.threads_minus_1);
Rich Felker3f72cda2011-09-18 10:14:37 -0400125 ret = __clone(start, stack, 0x7d8f00, new, &new->tid, new, &new->tid);
Rich Felker0b44a032011-02-12 00:22:29 -0500126
Rich Felkeracb04802011-07-29 22:59:44 -0400127 __synccall_unlock();
Rich Felker0b44a032011-02-12 00:22:29 -0500128
129 if (ret < 0) {
130 a_dec(&libc.threads_minus_1);
131 munmap(map, size);
Rich Felker59666802011-02-15 02:20:21 -0500132 return EAGAIN;
Rich Felker0b44a032011-02-12 00:22:29 -0500133 }
134 *res = new;
135 return 0;
136}
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500137
138void pthread_exit(void *result)
139{
140 struct pthread *self = pthread_self();
Rich Felkerfeee9892011-04-17 11:43:03 -0400141 struct __ptcb cb = { .__next = self->cancelbuf };
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500142 self->result = result;
Rich Felker56385dd2011-08-03 19:50:35 -0400143 __pthread_do_unwind(&cb);
Rich Felker1a9a2ff2011-02-13 19:58:30 -0500144}