1 #define _GNU_SOURCE
2 #define ANON_STACK_NAME_SIZE 50
3 #include "musl_log.h"
4 #include "pthread_impl.h"
5 #include "stdio_impl.h"
6 #include "libc.h"
7 #include "lock.h"
8 #include <sys/mman.h>
9 #include <sys/prctl.h>
10 #include <string.h>
11 #include <stddef.h>
12 #include <stdarg.h>
13
14 pid_t getpid(void);
15
log_print(const char * info,...)16 void log_print(const char* info, ...)
17 {
18 va_list ap;
19 va_start(ap, info);
20 vfprintf(stdout, info, ap);
21 va_end(ap);
22 }
23
stack_naming(struct pthread * new)24 void stack_naming(struct pthread *new) {
25 size_t size_len;
26 unsigned char *start_addr;
27 char name[ANON_STACK_NAME_SIZE];
28 if (new->guard_size) {
29 snprintf(name, ANON_STACK_NAME_SIZE, "guard:%d", new->tid);
30 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new->map_base, new->guard_size, name);
31 start_addr = new->map_base + new->guard_size;
32 size_len = new->map_size - new->guard_size;
33 memset(name, 0, ANON_STACK_NAME_SIZE);
34 } else {
35 start_addr = new->map_base;
36 size_len = new->map_size;
37 }
38 snprintf(name, ANON_STACK_NAME_SIZE, "stack:%d", new->tid);
39 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start_addr, size_len, name);
40 };
41
42 #ifdef RESERVE_SIGNAL_STACK
43 #if defined (__LP64__)
44 #define RESERVE_SIGNAL_STACK_SIZE (32 * 1024)
45 #else
46 #define RESERVE_SIGNAL_STACK_SIZE (20 * 1024)
47 #endif
__pthread_reserve_signal_stack()48 void __pthread_reserve_signal_stack()
49 {
50 void* stack = mmap(NULL, RESERVE_SIGNAL_STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51 if (stack != MAP_FAILED) {
52 if (mprotect(stack, __default_guardsize, PROT_NONE) == -1) {
53 munmap(stack, RESERVE_SIGNAL_STACK_SIZE);
54 return;
55 }
56 }
57
58 stack_t signal_stack;
59 signal_stack.ss_sp = (uint8_t*)stack + __default_guardsize;
60 signal_stack.ss_size = RESERVE_SIGNAL_STACK_SIZE - __default_guardsize;
61 signal_stack.ss_flags = 0;
62 sigaltstack(&signal_stack, NULL);
63
64 pthread_t self = __pthread_self();
65 self->signal_stack = stack;
66 char name[ANON_STACK_NAME_SIZE];
67 snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid);
68 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name);
69 return;
70 }
71
__pthread_release_signal_stack()72 void __pthread_release_signal_stack()
73 {
74 pthread_t self = __pthread_self();
75 if (self->signal_stack == NULL) {
76 return;
77 }
78
79 stack_t signal_stack, old_stack;
80 memset(&signal_stack, 0, sizeof(signal_stack));
81 signal_stack.ss_flags = SS_DISABLE;
82 sigaltstack(&signal_stack, &old_stack);
83 munmap(self->signal_stack, __default_guardsize);
84 if (old_stack.ss_flags != SS_DISABLE) {
85 munmap(old_stack.ss_sp, old_stack.ss_size);
86 }
87 self->signal_stack = NULL;
88 }
89
90 weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack);
91 weak_alias(__pthread_release_signal_stack, pthread_release_signal_stack);
92 #endif
93
dummy_0()94 static void dummy_0()
95 {
96 }
97 weak_alias(dummy_0, __acquire_ptc);
98 weak_alias(dummy_0, __release_ptc);
99 weak_alias(dummy_0, __pthread_tsd_run_dtors);
100 weak_alias(dummy_0, __do_orphaned_stdio_locks);
101 weak_alias(dummy_0, __dl_thread_cleanup);
102 weak_alias(dummy_0, __membarrier_init);
103
104 #define TID_ERROR_0 (0)
105 #define TID_ERROR_INIT (-1)
106 #define COUNT_ERROR_INIT (-10000)
107
108 static int tl_lock_count;
109 static int tl_lock_waiters;
110 static int tl_lock_tid_fail = TID_ERROR_INIT;
111 static int tl_lock_count_tid = TID_ERROR_INIT;
112 static int tl_lock_count_tid_sub = TID_ERROR_INIT;
113 static int tl_lock_count_fail = COUNT_ERROR_INIT;
114 static int thread_list_lock_after_lock = TID_ERROR_INIT;
115 static int thread_list_lock_pre_unlock = TID_ERROR_INIT;
116 static int thread_list_lock_pthread_exit = TID_ERROR_INIT;
117 static int thread_list_lock_tid_overlimit = TID_ERROR_INIT;
118
119 struct call_tl_lock tl_lock_caller_count = { 0 };
120
get_tl_lock_count(void)121 int get_tl_lock_count(void)
122 {
123 return tl_lock_count;
124 }
125
get_tl_lock_waiters(void)126 int get_tl_lock_waiters(void)
127 {
128 return tl_lock_waiters;
129 }
130
get_tl_lock_tid_fail(void)131 int get_tl_lock_tid_fail(void)
132 {
133 return tl_lock_tid_fail;
134 }
135
get_tl_lock_count_tid(void)136 int get_tl_lock_count_tid(void)
137 {
138 return tl_lock_count_tid;
139 }
140
get_tl_lock_count_tid_sub(void)141 int get_tl_lock_count_tid_sub(void)
142 {
143 return tl_lock_count_tid_sub;
144 }
145
get_tl_lock_count_fail(void)146 int get_tl_lock_count_fail(void)
147 {
148 return tl_lock_count_fail;
149 }
150
get_thread_list_lock_after_lock(void)151 int get_thread_list_lock_after_lock(void)
152 {
153 return thread_list_lock_after_lock;
154 }
155
get_thread_list_lock_pre_unlock(void)156 int get_thread_list_lock_pre_unlock(void)
157 {
158 return thread_list_lock_pre_unlock;
159 }
160
get_thread_list_lock_pthread_exit(void)161 int get_thread_list_lock_pthread_exit(void)
162 {
163 return thread_list_lock_pthread_exit;
164 }
165
get_thread_list_lock_tid_overlimit(void)166 int get_thread_list_lock_tid_overlimit(void)
167 {
168 return thread_list_lock_tid_overlimit;
169 }
170
get_tl_lock_caller_count(void)171 struct call_tl_lock *get_tl_lock_caller_count(void)
172 {
173 return &tl_lock_caller_count;
174 }
175
__tl_lock(void)176 void __tl_lock(void)
177 {
178 int tid = __pthread_self()->tid;
179 if (tid == TID_ERROR_0 || tid == TID_ERROR_INIT) {
180 tl_lock_tid_fail = TID_ERROR_0;
181 tid = __syscall(SYS_gettid);
182 }
183 if ((thread_list_lock_pthread_exit == tid) &&
184 (thread_list_lock_pthread_exit == __thread_list_lock)) {
185 thread_list_lock_tid_overlimit = __thread_list_lock;
186 }
187 int val = __thread_list_lock;
188 if (val == tid) {
189 tl_lock_count++;
190 tl_lock_count_tid = val;
191 return;
192 }
193 while ((val = a_cas(&__thread_list_lock, 0, tid)))
194 __wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
195 thread_list_lock_after_lock = __thread_list_lock;
196 if (get_tl_lock_caller_count()) {
197 get_tl_lock_caller_count()->tl_lock_unlock_count++;
198 }
199 }
200
__tl_unlock(void)201 void __tl_unlock(void)
202 {
203 if (tl_lock_count) {
204 tl_lock_count--;
205 tl_lock_count_tid_sub = __thread_list_lock;
206 return;
207 }
208 thread_list_lock_pre_unlock = __thread_list_lock;
209 if (get_tl_lock_caller_count()) {
210 get_tl_lock_caller_count()->tl_lock_unlock_count--;
211 }
212 a_store(&__thread_list_lock, 0);
213 if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
214 }
215
__tl_sync(pthread_t td)216 void __tl_sync(pthread_t td)
217 {
218 a_barrier();
219 int val = __thread_list_lock;
220 if (!val) return;
221 __wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
222 if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
223 }
224
225 #ifdef CXA_THREAD_USE_TLS
226 extern void __cxa_thread_finalize();
227 #endif
228
229 #ifdef ENABLE_HWASAN
230 weak void __hwasan_thread_enter();
231 weak void __hwasan_thread_exit();
232
233 __attribute__((no_sanitize("hwaddress")))
234 #endif
__pthread_exit(void * result)235 _Noreturn void __pthread_exit(void *result)
236 {
237 #ifdef CXA_THREAD_USE_TLS
238 // Call thread_local dtors.
239 __cxa_thread_finalize();
240 #endif
241 pthread_t self = __pthread_self();
242 sigset_t set;
243
244 #ifdef FEATURE_PTHREAD_CANCEL
245 self->canceldisable = 1;
246 self->cancelasync = 0;
247 #endif
248 self->result = result;
249
250 while (self->cancelbuf) {
251 void (*f)(void *) = self->cancelbuf->__f;
252 void *x = self->cancelbuf->__x;
253 self->cancelbuf = self->cancelbuf->__next;
254 f(x);
255 }
256
257 __pthread_tsd_run_dtors();
258
259 __block_app_sigs(&set);
260
261 /* This atomic potentially competes with a concurrent pthread_detach
262 * call; the loser is responsible for freeing thread resources. */
263 int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
264
265 if (state==DT_DETACHED && self->map_base) {
266 /* Since __unmapself bypasses the normal munmap code path,
267 * explicitly wait for vmlock holders first. This must be
268 * done before any locks are taken, to avoid lock ordering
269 * issues that could lead to deadlock. */
270 __vm_wait();
271 }
272
273 /* Access to target the exiting thread with syscalls that use
274 * its kernel tid is controlled by killlock. For detached threads,
275 * any use past this point would have undefined behavior, but for
276 * joinable threads it's a valid usage that must be handled.
277 * Signals must be blocked since pthread_kill must be AS-safe. */
278 LOCK(self->killlock);
279
280 /* The thread list lock must be AS-safe, and thus depends on
281 * application signals being blocked above. */
282 __tl_lock();
283 if (get_tl_lock_caller_count()) {
284 get_tl_lock_caller_count()->__pthread_exit_tl_lock++;
285 }
286
287 #ifdef RESERVE_SIGNAL_STACK
288 __pthread_release_signal_stack();
289 #endif
290 /* If this is the only thread in the list, don't proceed with
291 * termination of the thread, but restore the previous lock and
292 * signal state to prepare for exit to call atexit handlers. */
293 if (self->next == self) {
294 if (get_tl_lock_caller_count()) {
295 get_tl_lock_caller_count()->__pthread_exit_tl_lock--;
296 }
297 __tl_unlock();
298 UNLOCK(self->killlock);
299 self->detach_state = state;
300 __restore_sigs(&set);
301 #ifdef ENABLE_HWASAN
302 __hwasan_thread_exit();
303 #endif
304 exit(0);
305 }
306
307 /* At this point we are committed to thread termination. */
308
309 /* After the kernel thread exits, its tid may be reused. Clear it
310 * to prevent inadvertent use and inform functions that would use
311 * it that it's no longer available. At this point the killlock
312 * may be released, since functions that use it will consistently
313 * see the thread as having exited. Release it now so that no
314 * remaining locks (except thread list) are held if we end up
315 * resetting need_locks below. */
316 self->tid = 0;
317 UNLOCK(self->killlock);
318
319 /* Process robust list in userspace to handle non-pshared mutexes
320 * and the detached thread case where the robust list head will
321 * be invalid when the kernel would process it. */
322 __vm_lock();
323 volatile void *volatile *rp;
324 while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
325 pthread_mutex_t *m = (void *)((char *)rp
326 - offsetof(pthread_mutex_t, _m_next));
327 int waiters = m->_m_waiters;
328 int priv = (m->_m_type & 128) ^ 128;
329 self->robust_list.pending = rp;
330 self->robust_list.head = *rp;
331 int cont = a_swap(&m->_m_lock, 0x40000000);
332 self->robust_list.pending = 0;
333 if (cont < 0 || waiters)
334 __wake(&m->_m_lock, 1, priv);
335 }
336 __vm_unlock();
337
338 __do_orphaned_stdio_locks();
339 __dl_thread_cleanup();
340
341 /* Last, unlink thread from the list. This change will not be visible
342 * until the lock is released, which only happens after SYS_exit
343 * has been called, via the exit futex address pointing at the lock.
344 * This needs to happen after any possible calls to LOCK() that might
345 * skip locking if process appears single-threaded. */
346 if (!--libc.threads_minus_1) libc.need_locks = -1;
347 self->next->prev = self->prev;
348 self->prev->next = self->next;
349 self->prev = self->next = self;
350
351 if (state==DT_DETACHED && self->map_base) {
352 /* Detached threads must block even implementation-internal
353 * signals, since they will not have a stack in their last
354 * moments of existence. */
355 __block_all_sigs(&set);
356
357 /* Robust list will no longer be valid, and was already
358 * processed above, so unregister it with the kernel. */
359 if (self->robust_list.off)
360 __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
361
362 /* The following call unmaps the thread's stack mapping
363 * and then exits without touching the stack. */
364 if(tl_lock_count != 0) {
365 tl_lock_count_fail = tl_lock_count;
366 tl_lock_count = 0;
367 }
368 thread_list_lock_pthread_exit = __thread_list_lock;
369 if (get_tl_lock_caller_count()) {
370 get_tl_lock_caller_count()->__pthread_exit_tl_lock--;
371 get_tl_lock_caller_count()->tl_lock_unlock_count--;
372 }
373 __unmapself(self->map_base, self->map_size);
374 }
375
376 /* Wake any joiner. */
377 a_store(&self->detach_state, DT_EXITED);
378 __wake(&self->detach_state, 1, 1);
379
380 #ifdef ENABLE_HWASAN
381 __hwasan_thread_exit();
382 #endif
383
384 // If a thread call __tl_lock and call __pthread_exit without
385 // call __tl_unlock, the value of tl_lock_count will appear
386 // non-zero value, here set it to zero.
387 if(tl_lock_count != 0) {
388 tl_lock_count_fail = tl_lock_count;
389 tl_lock_count = 0;
390 }
391 thread_list_lock_pthread_exit = __thread_list_lock;
392 if (get_tl_lock_caller_count()) {
393 get_tl_lock_caller_count()->__pthread_exit_tl_lock--;
394 get_tl_lock_caller_count()->tl_lock_unlock_count--;
395 }
396
397 for (;;) __syscall(SYS_exit, 0);
398 }
399
__do_cleanup_push(struct __ptcb * cb)400 void __do_cleanup_push(struct __ptcb *cb)
401 {
402 struct pthread *self = __pthread_self();
403 cb->__next = self->cancelbuf;
404 self->cancelbuf = cb;
405 }
406
__do_cleanup_pop(struct __ptcb * cb)407 void __do_cleanup_pop(struct __ptcb *cb)
408 {
409 __pthread_self()->cancelbuf = cb->__next;
410 }
411
412 struct start_args {
413 void *(*start_func)(void *);
414 void *start_arg;
415 volatile int control;
416 unsigned long sig_mask[_NSIG/8/sizeof(long)];
417 };
418
419 #ifdef ENABLE_HWASAN
420 __attribute__((no_sanitize("hwaddress")))
421 #endif
start(void * p)422 static int start(void *p)
423 {
424 #ifdef ENABLE_HWASAN
425 __hwasan_thread_enter();
426 #endif
427 struct start_args *args = p;
428 int state = args->control;
429 if (state) {
430 if (a_cas(&args->control, 1, 2) == 1)
431 __wait(&args->control, 0, 2, 1);
432 if (args->control) {
433 __syscall(SYS_set_tid_address, &args->control);
434 for (;;) __syscall(SYS_exit, 0);
435 }
436 }
437 __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &args->sig_mask, 0, _NSIG/8);
438 #ifdef RESERVE_SIGNAL_STACK
439 __pthread_reserve_signal_stack();
440 #endif
441 __pthread_exit(args->start_func(args->start_arg));
442 return 0;
443 }
444
445 #ifdef ENABLE_HWASAN
446 __attribute__((no_sanitize("hwaddress")))
447 #endif
start_c11(void * p)448 static int start_c11(void *p)
449 {
450 #ifdef RESERVE_SIGNAL_STACK
451 __pthread_reserve_signal_stack();
452 #endif
453 #ifdef ENABLE_HWASAN
454 __hwasan_thread_enter();
455 #endif
456 struct start_args *args = p;
457 int (*start)(void*) = (int(*)(void*)) args->start_func;
458 __pthread_exit((void *)(uintptr_t)start(args->start_arg));
459 return 0;
460 }
461
462 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
463
464 /* pthread_key_create.c overrides this */
465 static volatile size_t dummy = 0;
466 weak_alias(dummy, __pthread_tsd_size);
467 static void *dummy_tsd[1] = { 0 };
468 weak_alias(dummy_tsd, __pthread_tsd_main);
469
470 static FILE *volatile dummy_file = 0;
471 weak_alias(dummy_file, __stdin_used);
472 weak_alias(dummy_file, __stdout_used);
473 weak_alias(dummy_file, __stderr_used);
474
init_file_lock(FILE * f)475 static void init_file_lock(FILE *f)
476 {
477 if (f && f->lock<0) f->lock = 0;
478 }
479
480 #ifdef ENABLE_HWASAN
481 __attribute__((no_sanitize("hwaddress")))
482 #endif
__pthread_create(pthread_t * restrict res,const pthread_attr_t * restrict attrp,void * (* entry)(void *),void * restrict arg)483 int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
484 {
485 int ret, c11 = (attrp == __ATTRP_C11_THREAD);
486 size_t size, guard;
487 struct pthread *self, *new;
488 unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
489 unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
490 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
491 | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
492 pthread_attr_t attr = { 0 };
493 sigset_t set;
494
495 if (!libc.can_do_threads) {
496 MUSL_LOGE("pthread_create: can't do threads, err: %{public}s", strerror(errno));
497 return ENOSYS;
498 }
499 self = __pthread_self();
500 if (!libc.threaded) {
501 for (FILE *f = *__ofl_lock(); f; f = f->next)
502 init_file_lock(f);
503 __ofl_unlock();
504 init_file_lock(__stdin_used);
505 init_file_lock(__stdout_used);
506 init_file_lock(__stderr_used);
507 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
508 self->tsd = (void **)__pthread_tsd_main;
509 __membarrier_init();
510 libc.threaded = 1;
511 }
512 if (attrp && !c11) attr = *attrp;
513
514 __acquire_ptc();
515 if (!attrp || c11) {
516 attr._a_stacksize = __default_stacksize;
517 attr._a_guardsize = __default_guardsize;
518 }
519
520 if (attr._a_stackaddr) {
521 size_t need = libc.tls_size + __pthread_tsd_size;
522 size = attr._a_stacksize;
523 stack = (void *)(attr._a_stackaddr & -16);
524 stack_limit = (void *)(attr._a_stackaddr - size);
525 /* Use application-provided stack for TLS only when
526 * it does not take more than ~12% or 2k of the
527 * application's stack space. */
528 if (need < size / 8 && need < 2048) {
529 tsd = stack - __pthread_tsd_size;
530 stack = tsd - libc.tls_size;
531 memset(stack, 0, need);
532 } else {
533 size = ROUND(need);
534 }
535 guard = 0;
536 } else {
537 guard = ROUND(attr._a_guardsize);
538 size = guard + ROUND(attr._a_stacksize
539 + libc.tls_size + __pthread_tsd_size);
540 }
541
542 if (!tsd) {
543 if (guard) {
544 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
545 if (map == MAP_FAILED) {
546 MUSL_LOGE("pthread_create: mmap PROT_NONE failed, err:%{public}s", strerror(errno));
547 goto fail;
548 }
549 if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
550 && errno != ENOSYS) {
551 MUSL_LOGE("pthread_create: mprotect failed, err:%{public}s", strerror(errno));
552 __munmap(map, size);
553 goto fail;
554 }
555 } else {
556 map = __mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
557 if (map == MAP_FAILED) {
558 MUSL_LOGE("pthread_create: mmap PROT_READ|PROT_WRITE failed, err:%{public}s", strerror(errno));
559 goto fail;
560 }
561 }
562 tsd = map + size - __pthread_tsd_size;
563 if (!stack) {
564 stack = tsd - libc.tls_size;
565 stack_limit = map + guard;
566 }
567 }
568
569 new = __copy_tls(tsd - libc.tls_size);
570 new->map_base = map;
571 new->map_size = size;
572 new->stack = stack;
573 new->stack_size = stack - stack_limit;
574 new->guard_size = guard;
575 new->self = new;
576 new->pid = getpid();
577 new->proc_tid = -1;
578 new->tsd = (void *)tsd;
579 new->locale = &libc.global_locale;
580 if (attr._a_detach) {
581 new->detach_state = DT_DETACHED;
582 } else {
583 new->detach_state = DT_JOINABLE;
584 }
585 new->robust_list.head = &new->robust_list.head;
586 new->canary = self->canary;
587 new->sysinfo = self->sysinfo;
588
589 /* Setup argument structure for the new thread on its stack.
590 * It's safe to access from the caller only until the thread
591 * list is unlocked. */
592 stack -= (uintptr_t)stack % sizeof(uintptr_t);
593 stack -= sizeof(struct start_args);
594 struct start_args *args = (void *)stack;
595 args->start_func = entry;
596 args->start_arg = arg;
597 args->control = attr._a_sched ? 1 : 0;
598
599 /* Application signals (but not the synccall signal) must be
600 * blocked before the thread list lock can be taken, to ensure
601 * that the lock is AS-safe. */
602 __block_app_sigs(&set);
603
604 /* Ensure SIGCANCEL is unblocked in new thread. This requires
605 * working with a copy of the set so we can restore the
606 * original mask in the calling thread. */
607 memcpy(&args->sig_mask, &set, sizeof args->sig_mask);
608 args->sig_mask[(SIGCANCEL-1)/8/sizeof(long)] &=
609 ~(1UL<<((SIGCANCEL-1)%(8*sizeof(long))));
610
611 __tl_lock();
612 if (get_tl_lock_caller_count()) {
613 get_tl_lock_caller_count()->__pthread_create_tl_lock++;
614 }
615 if (!libc.threads_minus_1++) libc.need_locks = 1;
616 ret = __clone((c11 ? start_c11 : start), stack, flags, args, &new->tid, TP_ADJ(new), &__thread_list_lock);
617
618 /* All clone failures translate to EAGAIN. If explicit scheduling
619 * was requested, attempt it before unlocking the thread list so
620 * that the failed thread is never exposed and so that we can
621 * clean up all transient resource usage before returning. */
622 if (ret < 0) {
623 ret = -EAGAIN;
624 } else if (attr._a_sched) {
625 ret = __syscall(SYS_sched_setscheduler,
626 new->tid, attr._a_policy, &attr._a_prio);
627 if (a_swap(&args->control, ret ? 3 : 0) == 2)
628 __wake(&args->control, 1, 1);
629 if (ret)
630 __wait(&args->control, 0, 3, 0);
631 }
632
633 if (ret >= 0) {
634 stack_naming(new);
635
636 new->next = self->next;
637 new->prev = self;
638 new->next->prev = new;
639 new->prev->next = new;
640 } else {
641 if (!--libc.threads_minus_1) libc.need_locks = 0;
642 }
643 if (get_tl_lock_caller_count()) {
644 get_tl_lock_caller_count()->__pthread_create_tl_lock--;
645 }
646 __tl_unlock();
647 __restore_sigs(&set);
648 __release_ptc();
649
650 if (ret < 0) {
651 if (map) __munmap(map, size);
652 MUSL_LOGE("pthread_create: ret:%{public}d, err:%{public}s", ret, strerror(errno));
653 return -ret;
654 }
655
656 *res = new;
657 return 0;
658 fail:
659 __release_ptc();
660 return EAGAIN;
661 }
662
663 weak_alias(__pthread_exit, pthread_exit);
664 weak_alias(__pthread_create, pthread_create);
665
__pthread_list_find(pthread_t thread_id,const char * info)666 struct pthread* __pthread_list_find(pthread_t thread_id, const char* info)
667 {
668 struct pthread *thread = (struct pthread *)thread_id;
669 if (NULL == thread) {
670 log_print("invalid pthread_t (0) passed to %s\n", info);
671 return NULL;
672 }
673
674 struct pthread *self = __pthread_self();
675 if (thread == self) {
676 return thread;
677 }
678 struct pthread *t = self;
679 t = t->next ;
680 while (t != self) {
681 if (t == thread) return thread;
682 t = t->next ;
683 }
684 log_print("invalid pthread_t %p passed to %s\n", thread, info);
685 return NULL;
686 }
687
__pthread_gettid_np(pthread_t t)688 pid_t __pthread_gettid_np(pthread_t t)
689 {
690 sigset_t set;
691 __block_app_sigs(&set);
692 __tl_lock();
693 if (get_tl_lock_caller_count()) {
694 get_tl_lock_caller_count()->__pthread_gettid_np_tl_lock++;
695 }
696 struct pthread* thread = __pthread_list_find(t, "pthread_gettid_np");
697 if (get_tl_lock_caller_count()) {
698 get_tl_lock_caller_count()->__pthread_gettid_np_tl_lock--;
699 }
700 __tl_unlock();
701 __restore_sigs(&set);
702 return thread ? thread->tid : -1;
703 }
704 weak_alias(__pthread_gettid_np, pthread_gettid_np);
705