- struct pthread *self = pthread_self(), *new;
- unsigned char *map, *stack, *tsd;
- const pthread_attr_t default_attr = { 0 };
-
- if (!self) return ENOSYS;
- if (!init && ++init) init_threads();
-
- if (!attr) attr = &default_attr;
- guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
- size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
- size += __pthread_tsd_size;
- map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
- if (!map) return EAGAIN;
- if (guard) mprotect(map, guard, PROT_NONE);
-
- tsd = map + size - __pthread_tsd_size;
- new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
+ struct pthread *self, *new;
+ unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
+ unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
+ | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
+ | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
+ int do_sched = 0;
+ pthread_attr_t attr = {0};
+
+ if (!libc.can_do_threads) return ENOSYS;
+ self = __pthread_self();
+ if (!libc.threaded) {
+ for (FILE *f=*__ofl_lock(); f; f=f->next)
+ init_file_lock(f);
+ __ofl_unlock();
+ init_file_lock(__stdin_used);
+ init_file_lock(__stdout_used);
+ init_file_lock(__stderr_used);
+ __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
+ self->tsd = (void **)__pthread_tsd_main;
+ libc.threaded = 1;
+ }
+ if (attrp && !c11) attr = *attrp;
+
+ __acquire_ptc();
+ if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
+
+ if (attr._a_stackaddr) {
+ size_t need = libc.tls_size + __pthread_tsd_size;
+ size = attr._a_stacksize + DEFAULT_STACK_SIZE;
+ stack = (void *)(attr._a_stackaddr & -16);
+ stack_limit = (void *)(attr._a_stackaddr - size);
+ /* Use application-provided stack for TLS only when
+ * it does not take more than ~12% or 2k of the
+ * application's stack space. */
+ if (need < size/8 && need < 2048) {
+ tsd = stack - __pthread_tsd_size;
+ stack = tsd - libc.tls_size;
+ memset(stack, 0, need);
+ } else {
+ size = ROUND(need);
+ guard = 0;
+ }
+ } else {
+ guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
+ size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
+ + libc.tls_size + __pthread_tsd_size);
+ }
+
+ if (!tsd) {
+ if (guard) {
+ map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
+ if (map == MAP_FAILED) goto fail;
+ if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
+ __munmap(map, size);
+ goto fail;
+ }
+ } else {
+ map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
+ if (map == MAP_FAILED) goto fail;
+ }
+ tsd = map + size - __pthread_tsd_size;
+ if (!stack) {
+ stack = tsd - libc.tls_size;
+ stack_limit = map + guard;
+ }
+ }
+
+ new = __copy_tls(tsd - libc.tls_size);