use atomic decrement rather than cas in pthread_exit thread count
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2 #include "stdio_impl.h"
3 #include <sys/mman.h>
4
5 static void dummy_0()
6 {
7 }
8 weak_alias(dummy_0, __acquire_ptc);
9 weak_alias(dummy_0, __release_ptc);
10 weak_alias(dummy_0, __pthread_tsd_run_dtors);
11
12 _Noreturn void pthread_exit(void *result)
13 {
14         pthread_t self = pthread_self();
15
16         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);
23         }
24
25         __pthread_tsd_run_dtors();
26
27         __lock(self->exitlock);
28
29         /* Mark this thread dead before decrementing count */
30         __lock(self->killlock);
31         self->dead = 1;
32         __unlock(self->killlock);
33
34         /* Block all signals before decrementing the live thread count.
35          * This is important to ensure that dynamically allocated TLS
36          * is not under-allocated/over-committed, and possibly for other
37          * reasons as well. */
38         __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGALL_SET, 0, _NSIG/8);
39
40         if (a_fetch_add(&libc.threads_minus_1, -1)==0) exit(0);
41
42         if (self->detached && self->map_base) {
43                 /* Detached threads must avoid the kernel clear_child_tid
44                  * feature, since the virtual address will have been
45                  * unmapped and possibly already reused by a new mapping
46                  * at the time the kernel would perform the write. In
47                  * the case of threads that started out detached, the
48                  * initial clone flags are correct, but if the thread was
49                  * detached later (== 2), we need to clear it here. */
50                 if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
51
52                 /* The following call unmaps the thread's stack mapping
53                  * and then exits without touching the stack. */
54                 __unmapself(self->map_base, self->map_size);
55         }
56
57         for (;;) __syscall(SYS_exit, 0);
58 }
59
60 void __do_cleanup_push(struct __ptcb *cb)
61 {
62         struct pthread *self = pthread_self();
63         cb->__next = self->cancelbuf;
64         self->cancelbuf = cb;
65 }
66
67 void __do_cleanup_pop(struct __ptcb *cb)
68 {
69         __pthread_self()->cancelbuf = cb->__next;
70 }
71
72 static int start(void *p)
73 {
74         pthread_t self = p;
75         if (self->startlock[0]) {
76                 __wait(self->startlock, 0, 1, 1);
77                 if (self->startlock[0]) {
78                         self->detached = 2;
79                         pthread_exit(0);
80                 }
81                 __syscall(SYS_rt_sigprocmask, SIG_SETMASK,
82                         self->sigmask, 0, _NSIG/8);
83         }
84         if (self->unblock_cancel)
85                 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
86                         SIGPT_SET, 0, _NSIG/8);
87         pthread_exit(self->start(self->start_arg));
88         return 0;
89 }
90
91 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
92
93 /* pthread_key_create.c overrides this */
94 static const size_t dummy = 0;
95 weak_alias(dummy, __pthread_tsd_size);
96
97 static FILE *const dummy_file = 0;
98 weak_alias(dummy_file, __stdin_used);
99 weak_alias(dummy_file, __stdout_used);
100 weak_alias(dummy_file, __stderr_used);
101
102 static void init_file_lock(FILE *f)
103 {
104         if (f && f->lock<0) f->lock = 0;
105 }
106
107 void *__copy_tls(unsigned char *);
108
109 int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
110 {
111         int ret;
112         size_t size, guard;
113         struct pthread *self = pthread_self(), *new;
114         unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
115         unsigned flags = 0x7d8f00;
116         int do_sched = 0;
117         pthread_attr_t attr = {0};
118
119         if (!self) return ENOSYS;
120         if (!libc.threaded) {
121                 for (FILE *f=libc.ofl_head; f; f=f->next)
122                         init_file_lock(f);
123                 init_file_lock(__stdin_used);
124                 init_file_lock(__stdout_used);
125                 init_file_lock(__stderr_used);
126                 libc.threaded = 1;
127         }
128         if (attrp) attr = *attrp;
129
130         __acquire_ptc();
131
132         if (attr._a_stackaddr) {
133                 size_t need = libc.tls_size + __pthread_tsd_size;
134                 size = attr._a_stacksize + DEFAULT_STACK_SIZE;
135                 stack = (void *)(attr._a_stackaddr & -16);
136                 stack_limit = (void *)(attr._a_stackaddr - size);
137                 /* Use application-provided stack for TLS only when
138                  * it does not take more than ~12% or 2k of the
139                  * application's stack space. */
140                 if (need < size/8 && need < 2048) {
141                         tsd = stack - __pthread_tsd_size;
142                         stack = tsd - libc.tls_size;
143                 } else {
144                         size = ROUND(need);
145                         guard = 0;
146                 }
147         } else {
148                 guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
149                 size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
150                         + libc.tls_size +  __pthread_tsd_size);
151         }
152
153         if (!tsd) {
154                 if (guard) {
155                         map = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
156                         if (map == MAP_FAILED) goto fail;
157                         if (mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
158                                 munmap(map, size);
159                                 goto fail;
160                         }
161                 } else {
162                         map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
163                         if (map == MAP_FAILED) goto fail;
164                 }
165                 tsd = map + size - __pthread_tsd_size;
166                 if (!stack) {
167                         stack = tsd - libc.tls_size;
168                         stack_limit = map + guard;
169                 }
170         }
171
172         new = __copy_tls(tsd - libc.tls_size);
173         new->map_base = map;
174         new->map_size = size;
175         new->stack = stack;
176         new->stack_size = stack - stack_limit;
177         new->pid = self->pid;
178         new->errno_ptr = &new->errno_val;
179         new->start = entry;
180         new->start_arg = arg;
181         new->self = new;
182         new->tsd = (void *)tsd;
183         if (attr._a_detach) {
184                 new->detached = 1;
185                 flags -= 0x200000;
186         }
187         if (attr._a_sched) {
188                 do_sched = new->startlock[0] = 1;
189                 __syscall(SYS_rt_sigprocmask, SIG_BLOCK,
190                         SIGALL_SET, self->sigmask, _NSIG/8);
191         }
192         new->unblock_cancel = self->cancel;
193         new->canary = self->canary;
194
195         a_inc(&libc.threads_minus_1);
196         ret = __clone(start, stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
197
198         __release_ptc();
199
200         if (do_sched) {
201                 __syscall(SYS_rt_sigprocmask, SIG_SETMASK,
202                         new->sigmask, 0, _NSIG/8);
203         }
204
205         if (ret < 0) {
206                 a_dec(&libc.threads_minus_1);
207                 if (map) munmap(map, size);
208                 return EAGAIN;
209         }
210
211         if (do_sched) {
212                 ret = __syscall(SYS_sched_setscheduler, new->tid,
213                         attr._a_policy, &attr._a_prio);
214                 a_store(new->startlock, ret<0 ? 2 : 0);
215                 __wake(new->startlock, 1, 1);
216                 if (ret < 0) return -ret;
217         }
218
219         *res = new;
220         return 0;
221 fail:
222         __release_ptc();
223         return EAGAIN;
224 }