From 155b378a3962e4d291f6af577eafa1f7267b6198 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 10 Sep 2022 02:56:25 -0700 Subject: [PATCH] Tidy up the threading implementation The organization of the source files is now much more rational. Old experiments that didn't work out are now deleted. Naming of things like files is now more intuitive. --- examples/greenbean.c | 4 +- examples/thread.c | 2 +- libc/calls/clock_gettime-mono.c | 2 +- libc/calls/fstatat64.S | 7 +- libc/calls/ftruncate64.S | 7 +- libc/calls/g_sighandrvas.c | 2 +- libc/calls/getloadavg-nt.c | 2 +- libc/calls/interrupts-nt.c | 2 +- libc/calls/lstat64.S | 7 +- libc/calls/raise.c | 2 +- libc/calls/samplepids.c | 4 +- libc/calls/sched_setscheduler.c | 2 +- libc/calls/siglock.c | 2 +- libc/calls/stat64.S | 4 +- libc/calls/state.internal.h | 4 +- libc/calls/symlinkat-nt.c | 2 +- libc/calls/unveil.c | 2 +- libc/dns/gethoststxt.c | 2 +- libc/dns/getresolvconf.c | 2 +- libc/intrin/asan.c | 6 +- libc/intrin/cxaatexit.internal.h | 2 +- libc/intrin/cxalock.c | 2 +- libc/intrin/fds_lock.c | 2 +- libc/intrin/futex_wait.c | 2 +- libc/intrin/futex_wake.c | 2 +- libc/intrin/g_fds.c | 2 +- libc/intrin/gettid.c | 21 +- libc/intrin/intrin.mk | 5 - libc/intrin/kprintf.greg.c | 6 +- libc/intrin/mmi.init.S | 2 +- libc/intrin/mmi_lock.c | 2 +- libc/intrin/mmi_lock_obj.c | 2 +- libc/intrin/phtread_attr_setschedpolicy.c | 2 +- libc/intrin/pthread.h | 205 ----------------- libc/intrin/pthread_mutex_init.c | 6 +- libc/intrin/pthread_mutex_lock.c | 4 +- libc/intrin/pthread_mutex_trylock.c | 2 +- libc/intrin/pthread_mutex_unlock.c | 2 +- libc/intrin/pthread_once.c | 2 +- libc/intrin/pthread_yield.c | 2 +- libc/intrin/rand64.c | 4 +- libc/intrin/tlsisrequired.c | 4 +- libc/intrin/wait0.c | 2 +- libc/isystem/limits.h | 2 +- libc/isystem/pthread.h | 4 +- libc/log/backtrace2.c | 6 +- libc/log/backtrace3.c | 6 +- libc/log/oncrash.c | 2 +- libc/log/printgarbage.c | 6 +- libc/mem/gc.c | 20 +- libc/nexgen32e/gc.S | 2 +- libc/nexgen32e/gclongjmp.S | 2 +- libc/nexgen32e/threaded.c | 2 +- libc/nexgen32e/threaded.h | 16 -- libc/runtime/construct.S | 34 --- libc/runtime/enable_threads.c | 2 +- libc/runtime/enable_tls.c | 21 +- libc/runtime/fork-nt.c | 2 +- libc/runtime/fork.c | 5 +- libc/runtime/ftracer.c | 31 ++- libc/runtime/getsymboltable.c | 2 +- libc/runtime/memtrack.internal.h | 2 +- libc/runtime/mmap.c | 2 +- libc/stdio/dirstream.c | 2 +- libc/stdio/fdopen.c | 2 +- libc/stdio/fflush.internal.h | 2 +- libc/stdio/fflush_unlocked.c | 2 +- libc/stdio/flushlbf.c | 2 +- libc/stdio/fmemopen.c | 2 +- libc/stdio/iconv.c | 7 +- libc/stdio/lock.h | 2 +- libc/stdio/readdir64.S | 7 +- libc/stdio/stderr-init.S | 2 +- libc/stdio/stdin-init.S | 2 +- libc/stdio/stdio.h | 2 +- libc/stdio/stdout-init.S | 2 +- libc/str/langinfo.c | 5 +- libc/sysv/errno_location.greg.c | 8 +- libc/testlib/testrunner.c | 2 +- libc/{runtime => thread}/clone-linux.S | 0 .../winthreadlaunch.S => thread/clone-nt.S} | 0 libc/{runtime => thread}/clone-openbsd.S | 0 libc/{runtime => thread}/clone.c | 19 +- libc/thread/internal.h | 2 +- libc/thread/mktls.c | 18 +- libc/thread/posixthread.internal.h | 9 +- .../{intrin => thread}/pthread_attr_destroy.c | 2 +- .../pthread_attr_getdetachstate.c | 2 +- .../pthread_attr_getguardsize.c | 2 +- .../pthread_attr_getinheritsched.c | 2 +- .../pthread_attr_getschedparam.c | 2 +- .../pthread_attr_getschedpolicy.c | 2 +- .../pthread_attr_getscope.c | 2 +- .../pthread_attr_getstack.c | 2 +- .../pthread_attr_getstacksize.c | 2 +- libc/{intrin => thread}/pthread_attr_init.c | 2 +- .../pthread_attr_setdetachstate.c | 2 +- .../pthread_attr_setguardsize.c | 2 +- .../pthread_attr_setinheritsched.c | 2 +- .../pthread_attr_setschedparam.c | 2 +- .../pthread_attr_setscope.c | 2 +- .../pthread_attr_setstack.c | 2 +- .../pthread_attr_setstacksize.c | 2 +- .../pthread_barrier_destroy.c | 2 +- .../{intrin => thread}/pthread_barrier_init.c | 7 +- .../{intrin => thread}/pthread_barrier_wait.c | 2 +- .../pthread_barrierattr_destroy.c | 2 +- .../pthread_barrierattr_getpshared.c | 2 +- .../pthread_barrierattr_init.c | 4 +- .../pthread_barrierattr_setpshared.c | 2 +- .../pthread.c => thread/pthread_cancel.c} | 8 +- .../pthread_cond_broadcast.c | 2 +- .../{intrin => thread}/pthread_cond_destroy.c | 2 +- libc/{intrin => thread}/pthread_cond_init.c | 4 +- libc/thread/pthread_cond_timedwait.c | 4 +- libc/thread/pthread_cond_wait.c | 2 +- .../pthread_condattr_destroy.c | 2 +- .../pthread_condattr_getpshared.c | 2 +- .../pthread_condattr_init.c | 4 +- .../pthread_condattr_setpshared.c | 2 +- libc/thread/pthread_create.c | 10 +- libc/thread/pthread_detach.c | 2 +- libc/thread/pthread_equal.c | 2 +- libc/thread/pthread_exit.c | 5 +- libc/thread/pthread_getaffinity_np.c | 1 - libc/thread/pthread_getattr_np.c | 1 - libc/thread/pthread_getname_np.c | 1 - libc/thread/pthread_getschedparam.c | 2 +- libc/thread/pthread_getspecific.c | 3 +- libc/thread/pthread_getthreadid_np.c | 2 +- libc/thread/pthread_getunique_np.c | 1 - libc/thread/pthread_join.c | 2 +- libc/thread/pthread_key_create.c | 3 +- libc/thread/pthread_key_delete.c | 1 - libc/thread/pthread_key_destruct.c | 1 - .../pthread_mutex_destroy.c | 2 +- .../pthread_mutexattr_destroy.c | 2 +- .../pthread_mutexattr_getpshared.c | 2 +- .../pthread_mutexattr_gettype.c | 2 +- .../pthread_mutexattr_init.c | 7 +- .../pthread_mutexattr_setpshared.c | 2 +- .../pthread_mutexattr_settype.c | 2 +- libc/thread/pthread_reschedule.c | 1 - .../pthread_rwlock_destroy.c | 2 +- libc/{intrin => thread}/pthread_rwlock_init.c | 4 +- .../pthread_rwlock_rdlock.c | 2 +- .../pthread_rwlock_unlock.c | 2 +- .../pthread_rwlock_wrlock.c | 2 +- .../pthread_rwlockattr_destroy.c | 2 +- .../pthread_rwlockattr_getpshared.c | 2 +- .../pthread_rwlockattr_init.c | 4 +- .../pthread_rwlockattr_setpshared.c | 2 +- libc/thread/pthread_self.c | 5 +- libc/thread/pthread_setaffinity_np.c | 1 - libc/thread/pthread_setname_np.c | 1 - libc/thread/pthread_setschedparam.c | 2 +- libc/thread/pthread_setschedparam_freebsd.c | 1 - libc/thread/pthread_setspecific.c | 3 +- libc/thread/pthread_sigmask.c | 1 - .../{intrin => thread}/pthread_spin_destroy.c | 2 +- libc/{intrin => thread}/pthread_spin_init.c | 2 +- libc/{intrin => thread}/pthread_spin_lock.c | 2 +- .../{intrin => thread}/pthread_spin_trylock.c | 2 +- libc/{intrin => thread}/pthread_spin_unlock.c | 2 +- libc/thread/{zombie.c => pthread_zombies.c} | 2 +- libc/thread/spawn.c | 12 +- libc/thread/thread.h | 212 ++++++++++++++++-- libc/thread/thread.mk | 3 +- libc/{intrin/pthread2.h => thread/thread2.h} | 2 +- libc/thread/tls.h | 45 ++++ libc/{nexgen32e/gettls.h => thread/tls2.h} | 25 +-- libc/thread/ungarbage.c | 7 +- libc/time/localtime.c | 4 +- libc/zipos/get.c | 2 +- libc/zipos/lock.c | 2 +- libc/zipos/zipos.internal.h | 2 +- test/libc/calls/reservefd_test.c | 2 +- test/libc/intrin/pthread_mutex_lock2_test.c | 2 +- test/libc/intrin/pthread_mutex_lock_test.c | 5 +- test/libc/intrin/pthread_once_test.c | 2 +- test/libc/intrin/rand64_test.c | 1 - test/libc/runtime/arch_prctl_test.c | 2 +- test/libc/{runtime => thread}/clone_test.c | 24 +- .../pthread_barrier_wait_test.c | 2 +- .../pthread_cond_broadcast_test.c | 4 +- test/libc/thread/pthread_create_test.c | 3 +- test/libc/thread/pthread_key_create_test.c | 2 - .../pthread_rwlock_rdlock_test.c | 6 +- test/libc/thread/pthread_setname_np_test.c | 1 - test/libc/thread/spawn_test.c | 1 - test/libc/x/makedirs_test.c | 2 +- third_party/dlmalloc/locks.inc | 4 +- third_party/gdtoa/lock.c | 2 +- third_party/gdtoa/lock.h | 2 +- third_party/libcxx/__threading_support | 4 +- third_party/lua/lrepl.c | 2 +- tool/build/mkdeps.c | 4 +- tool/net/redbean.c | 4 +- tool/plinko/plinko.c | 2 +- 199 files changed, 526 insertions(+), 685 deletions(-) delete mode 100644 libc/intrin/pthread.h delete mode 100644 libc/nexgen32e/threaded.h delete mode 100644 libc/runtime/construct.S rename libc/{runtime => thread}/clone-linux.S (100%) rename libc/{runtime/winthreadlaunch.S => thread/clone-nt.S} (100%) rename libc/{runtime => thread}/clone-openbsd.S (100%) rename libc/{runtime => thread}/clone.c (97%) rename libc/{intrin => thread}/pthread_attr_destroy.c (98%) rename libc/{intrin => thread}/pthread_attr_getdetachstate.c (98%) rename libc/{intrin => thread}/pthread_attr_getguardsize.c (98%) rename libc/{intrin => thread}/pthread_attr_getinheritsched.c (98%) rename libc/{intrin => thread}/pthread_attr_getschedparam.c (98%) rename libc/{intrin => thread}/pthread_attr_getschedpolicy.c (98%) rename libc/{intrin => thread}/pthread_attr_getscope.c (98%) rename libc/{intrin => thread}/pthread_attr_getstack.c (98%) rename libc/{intrin => thread}/pthread_attr_getstacksize.c (98%) rename libc/{intrin => thread}/pthread_attr_init.c (98%) rename libc/{intrin => thread}/pthread_attr_setdetachstate.c (98%) rename libc/{intrin => thread}/pthread_attr_setguardsize.c (98%) rename libc/{intrin => thread}/pthread_attr_setinheritsched.c (98%) rename libc/{intrin => thread}/pthread_attr_setschedparam.c (98%) rename libc/{intrin => thread}/pthread_attr_setscope.c (98%) rename libc/{intrin => thread}/pthread_attr_setstack.c (99%) rename libc/{intrin => thread}/pthread_attr_setstacksize.c (98%) rename libc/{intrin => thread}/pthread_barrier_destroy.c (98%) rename libc/{intrin => thread}/pthread_barrier_init.c (94%) rename libc/{intrin => thread}/pthread_barrier_wait.c (98%) rename libc/{intrin => thread}/pthread_barrierattr_destroy.c (98%) rename libc/{intrin => thread}/pthread_barrierattr_getpshared.c (98%) rename libc/{intrin => thread}/pthread_barrierattr_init.c (96%) rename libc/{intrin => thread}/pthread_barrierattr_setpshared.c (98%) rename libc/{intrin/pthread.c => thread/pthread_cancel.c} (92%) rename libc/{intrin => thread}/pthread_cond_broadcast.c (99%) rename libc/{intrin => thread}/pthread_cond_destroy.c (98%) rename libc/{intrin => thread}/pthread_cond_init.c (95%) rename libc/{intrin => thread}/pthread_condattr_destroy.c (98%) rename libc/{intrin => thread}/pthread_condattr_getpshared.c (98%) rename libc/{intrin => thread}/pthread_condattr_init.c (96%) rename libc/{intrin => thread}/pthread_condattr_setpshared.c (98%) rename libc/{intrin => thread}/pthread_mutex_destroy.c (98%) rename libc/{intrin => thread}/pthread_mutexattr_destroy.c (98%) rename libc/{intrin => thread}/pthread_mutexattr_getpshared.c (98%) rename libc/{intrin => thread}/pthread_mutexattr_gettype.c (98%) rename libc/{intrin => thread}/pthread_mutexattr_init.c (93%) rename libc/{intrin => thread}/pthread_mutexattr_setpshared.c (98%) rename libc/{intrin => thread}/pthread_mutexattr_settype.c (98%) rename libc/{intrin => thread}/pthread_rwlock_destroy.c (98%) rename libc/{intrin => thread}/pthread_rwlock_init.c (95%) rename libc/{intrin => thread}/pthread_rwlock_rdlock.c (98%) rename libc/{intrin => thread}/pthread_rwlock_unlock.c (98%) rename libc/{intrin => thread}/pthread_rwlock_wrlock.c (98%) rename libc/{intrin => thread}/pthread_rwlockattr_destroy.c (98%) rename libc/{intrin => thread}/pthread_rwlockattr_getpshared.c (98%) rename libc/{intrin => thread}/pthread_rwlockattr_init.c (96%) rename libc/{intrin => thread}/pthread_rwlockattr_setpshared.c (98%) rename libc/{intrin => thread}/pthread_spin_destroy.c (98%) rename libc/{intrin => thread}/pthread_spin_init.c (98%) rename libc/{intrin => thread}/pthread_spin_lock.c (98%) rename libc/{intrin => thread}/pthread_spin_trylock.c (98%) rename libc/{intrin => thread}/pthread_spin_unlock.c (98%) rename libc/thread/{zombie.c => pthread_zombies.c} (98%) rename libc/{intrin/pthread2.h => thread/thread2.h} (96%) create mode 100644 libc/thread/tls.h rename libc/{nexgen32e/gettls.h => thread/tls2.h} (56%) rename test/libc/{runtime => thread}/clone_test.c (89%) rename test/libc/{intrin => thread}/pthread_barrier_wait_test.c (99%) rename test/libc/{intrin => thread}/pthread_cond_broadcast_test.c (98%) rename test/libc/{intrin => thread}/pthread_rwlock_rdlock_test.c (95%) diff --git a/examples/greenbean.c b/examples/greenbean.c index 7169a6180..379884ee3 100644 --- a/examples/greenbean.c +++ b/examples/greenbean.c @@ -18,14 +18,14 @@ #include "libc/fmt/itoa.h" #include "libc/intrin/atomic.h" #include "libc/intrin/kprintf.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/wait0.internal.h" #include "libc/limits.h" #include "libc/log/check.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" diff --git a/examples/thread.c b/examples/thread.c index f019d7107..d59a0eb4a 100644 --- a/examples/thread.c +++ b/examples/thread.c @@ -7,7 +7,7 @@ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/stdio/stdio.h" /** diff --git a/libc/calls/clock_gettime-mono.c b/libc/calls/clock_gettime-mono.c index 42a88162e..550af8dcd 100644 --- a/libc/calls/clock_gettime-mono.c +++ b/libc/calls/clock_gettime-mono.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/struct/timespec.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/nexgen32e/rdtsc.h" #include "libc/nexgen32e/x86feature.h" #include "libc/sysv/consts/clock.h" diff --git a/libc/calls/fstatat64.S b/libc/calls/fstatat64.S index 09086d2dd..8b0c3e5f8 100644 --- a/libc/calls/fstatat64.S +++ b/libc/calls/fstatat64.S @@ -1,5 +1,5 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ This is free and unencumbered software released into the public domain. │ │ │ @@ -26,5 +26,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -fstatat64: jmp fstatat +fstatat64: + jmp fstatat .endfn fstatat64,globl diff --git a/libc/calls/ftruncate64.S b/libc/calls/ftruncate64.S index fbb6c5aa2..a4a2a5080 100644 --- a/libc/calls/ftruncate64.S +++ b/libc/calls/ftruncate64.S @@ -1,5 +1,5 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ This is free and unencumbered software released into the public domain. │ │ │ @@ -26,5 +26,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -ftruncate64: jmp ftruncate +ftruncate64: + jmp ftruncate .endfn ftruncate64,globl diff --git a/libc/calls/g_sighandrvas.c b/libc/calls/g_sighandrvas.c index df628bc0e..3e0780d59 100644 --- a/libc/calls/g_sighandrvas.c +++ b/libc/calls/g_sighandrvas.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/state.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" // TODO(jart): These should be _Thread_local but doing that currently // causes a regression with runitd.com on Windows. diff --git a/libc/calls/getloadavg-nt.c b/libc/calls/getloadavg-nt.c index 1eba10b65..6a82d5bbc 100644 --- a/libc/calls/getloadavg-nt.c +++ b/libc/calls/getloadavg-nt.c @@ -20,7 +20,7 @@ #include "libc/calls/syscall_support-nt.internal.h" #include "libc/dce.h" #include "libc/fmt/conv.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/nt/accounting.h" #include "libc/runtime/sysconf.h" diff --git a/libc/calls/interrupts-nt.c b/libc/calls/interrupts-nt.c index 82ccd8e6e..749d0a192 100644 --- a/libc/calls/interrupts-nt.c +++ b/libc/calls/interrupts-nt.c @@ -27,7 +27,7 @@ #include "libc/dce.h" #include "libc/intrin/lockcmpxchgp.h" #include "libc/intrin/weaken.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" textwindows bool _check_interrupts(bool restartable, struct Fd *fd) { bool res; diff --git a/libc/calls/lstat64.S b/libc/calls/lstat64.S index 24d9d81e0..7cfe4b329 100644 --- a/libc/calls/lstat64.S +++ b/libc/calls/lstat64.S @@ -1,5 +1,5 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ This is free and unencumbered software released into the public domain. │ │ │ @@ -26,5 +26,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -lstat64: jmp lstat +lstat64: + jmp lstat .endfn lstat64,globl diff --git a/libc/calls/raise.c b/libc/calls/raise.c index d2fb4927b..b94c49e55 100644 --- a/libc/calls/raise.c +++ b/libc/calls/raise.c @@ -21,7 +21,7 @@ #include "libc/calls/strace.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/sysv/consts/sicode.h" #include "libc/sysv/consts/sig.h" diff --git a/libc/calls/samplepids.c b/libc/calls/samplepids.c index 035f43356..b1a6c8016 100644 --- a/libc/calls/samplepids.c +++ b/libc/calls/samplepids.c @@ -19,8 +19,8 @@ #include "libc/assert.h" #include "libc/calls/internal.h" #include "libc/dce.h" -#include "libc/intrin/pthread.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #include "libc/stdio/lcg.internal.h" /** diff --git a/libc/calls/sched_setscheduler.c b/libc/calls/sched_setscheduler.c index 211f14326..e5fb93996 100644 --- a/libc/calls/sched_setscheduler.c +++ b/libc/calls/sched_setscheduler.c @@ -22,7 +22,7 @@ #include "libc/calls/struct/sched_param.internal.h" #include "libc/dce.h" #include "libc/intrin/describeflags.internal.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/str/str.h" #include "libc/sysv/errfuns.h" diff --git a/libc/calls/siglock.c b/libc/calls/siglock.c index c9feb3ba2..84cf89582 100644 --- a/libc/calls/siglock.c +++ b/libc/calls/siglock.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/state.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" static pthread_mutex_t __sig_lock_obj; diff --git a/libc/calls/stat64.S b/libc/calls/stat64.S index 7b6738b40..e61b7bb04 100644 --- a/libc/calls/stat64.S +++ b/libc/calls/stat64.S @@ -1,5 +1,5 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ This is free and unencumbered software released into the public domain. │ │ │ diff --git a/libc/calls/state.internal.h b/libc/calls/state.internal.h index b5eacf1ba..09743e4fa 100644 --- a/libc/calls/state.internal.h +++ b/libc/calls/state.internal.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_CALLS_STATE_INTERNAL_H_ #define COSMOPOLITAN_LIBC_CALLS_STATE_INTERNAL_H_ #include "libc/intrin/nopl.h" -#include "libc/intrin/pthread.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/calls/symlinkat-nt.c b/libc/calls/symlinkat-nt.c index dd36786bd..cdb7acc49 100644 --- a/libc/calls/symlinkat-nt.c +++ b/libc/calls/symlinkat-nt.c @@ -19,7 +19,7 @@ #include "libc/calls/calls.h" #include "libc/calls/syscall_support-nt.internal.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/nt/enum/accessmask.h" #include "libc/nt/enum/fileflagandattributes.h" #include "libc/nt/enum/symboliclink.h" diff --git a/libc/calls/unveil.c b/libc/calls/unveil.c index 759ad3db8..b439553b5 100644 --- a/libc/calls/unveil.c +++ b/libc/calls/unveil.c @@ -30,7 +30,7 @@ #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" diff --git a/libc/dns/gethoststxt.c b/libc/dns/gethoststxt.c index 32ff8dfce..55fb566a7 100644 --- a/libc/dns/gethoststxt.c +++ b/libc/dns/gethoststxt.c @@ -23,7 +23,7 @@ #include "libc/dce.h" #include "libc/dns/hoststxt.h" #include "libc/fmt/fmt.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/nt/systeminfo.h" #include "libc/runtime/runtime.h" diff --git a/libc/dns/getresolvconf.c b/libc/dns/getresolvconf.c index e226ac13f..1546ee9c6 100644 --- a/libc/dns/getresolvconf.c +++ b/libc/dns/getresolvconf.c @@ -20,7 +20,7 @@ #include "libc/dce.h" #include "libc/dns/resolvconf.h" #include "libc/fmt/fmt.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" diff --git a/libc/intrin/asan.c b/libc/intrin/asan.c index 5fb061e14..9c3887746 100644 --- a/libc/intrin/asan.c +++ b/libc/intrin/asan.c @@ -28,7 +28,6 @@ #include "libc/intrin/likely.h" #include "libc/intrin/lockcmpxchg.h" #include "libc/intrin/nomultics.internal.h" -#include "libc/intrin/pthread.h" #include "libc/intrin/weaken.h" #include "libc/log/backtrace.internal.h" #include "libc/log/internal.h" @@ -39,9 +38,7 @@ #include "libc/mem/mem.h" #include "libc/mem/reverse.internal.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/stackframe.h" -#include "libc/nexgen32e/threaded.h" #include "libc/nt/enum/version.h" #include "libc/nt/runtime.h" #include "libc/runtime/directmap.internal.h" @@ -58,6 +55,7 @@ #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" #include "libc/thread/thread.h" +#include "libc/thread/tls.h" #include "third_party/dlmalloc/dlmalloc.h" STATIC_YOINK("_init_asan"); @@ -938,7 +936,7 @@ static void __asan_trace(struct AsanTrace *bt, const struct StackFrame *bp) { size_t i, gi; intptr_t addr; struct Garbages *garbage; - garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0; + garbage = __tls_enabled ? __get_tls()->tib_garbages : 0; gi = garbage ? garbage->i : 0; for (f1 = -1, i = 0; bp && i < ARRAYLEN(bt->p); ++i, bp = bp->next) { if (f1 != (f2 = ((intptr_t)bp >> 16))) { diff --git a/libc/intrin/cxaatexit.internal.h b/libc/intrin/cxaatexit.internal.h index 99e1e9036..46c3aacaf 100644 --- a/libc/intrin/cxaatexit.internal.h +++ b/libc/intrin/cxaatexit.internal.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_RUNTIME_CXAATEXIT_H_ #define COSMOPOLITAN_LIBC_RUNTIME_CXAATEXIT_H_ #include "libc/intrin/nopl.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/stdio/stdio.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/intrin/cxalock.c b/libc/intrin/cxalock.c index 49b2d96b6..1f7b8d652 100644 --- a/libc/intrin/cxalock.c +++ b/libc/intrin/cxalock.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/cxaatexit.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" static pthread_mutex_t __cxa_lock_obj; diff --git a/libc/intrin/fds_lock.c b/libc/intrin/fds_lock.c index b01bccb9e..6097b4a5a 100644 --- a/libc/intrin/fds_lock.c +++ b/libc/intrin/fds_lock.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/state.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" void(__fds_lock)(void) { pthread_mutex_lock(&__fds_lock_obj); diff --git a/libc/intrin/futex_wait.c b/libc/intrin/futex_wait.c index bb6a6c5c6..0f7fbb904 100644 --- a/libc/intrin/futex_wait.c +++ b/libc/intrin/futex_wait.c @@ -23,7 +23,7 @@ #include "libc/errno.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/sysv/consts/futex.h" int _futex(void *, int, int, struct timespec *) hidden; diff --git a/libc/intrin/futex_wake.c b/libc/intrin/futex_wake.c index d53330a57..46e3cdd08 100644 --- a/libc/intrin/futex_wake.c +++ b/libc/intrin/futex_wake.c @@ -21,7 +21,7 @@ #include "libc/errno.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/sysv/consts/futex.h" int _futex(void *, int, int) hidden; diff --git a/libc/intrin/g_fds.c b/libc/intrin/g_fds.c index 171fb3801..c0e187cbc 100644 --- a/libc/intrin/g_fds.c +++ b/libc/intrin/g_fds.c @@ -19,7 +19,7 @@ #include "libc/calls/extend.internal.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/pushpop.h" #include "libc/intrin/weaken.h" #include "libc/nt/runtime.h" diff --git a/libc/intrin/gettid.c b/libc/intrin/gettid.c index 77ac11678..209298da9 100644 --- a/libc/intrin/gettid.c +++ b/libc/intrin/gettid.c @@ -21,8 +21,7 @@ #include "libc/calls/state.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" /** * Returns current thread id. @@ -31,22 +30,6 @@ * if this is the main thread. On NetBSD, gettid() for the main thread * is always 1. * - * This function issues a system call. That stops being the case as soon - * as __install_tls() is called. That'll happen automatically, when you - * call clone() and provide the TLS parameter. We assume that when a TLS - * block exists, then - * - * *(int *)(__get_tls() + 0x38) - * - * will contain the thread id. Therefore when issuing clone() calls, the - * `CLONE_CHILD_SETTID` and `CLONE_CHILD_CLEARTID` flags should use that - * index as its `ctid` memory. - * - * gettid (single threaded) l: 126𝑐 41𝑛𝑠 - * gettid (tls enabled) l: 2𝑐 1𝑛𝑠 - * - * The TLS convention is important for reentrant lock performance. - * * @return thread id greater than zero or -1 w/ errno * @asyncsignalsafe * @threadsafe @@ -55,7 +38,7 @@ int gettid(void) { int tid; if (__tls_enabled && !__vforked) { - tid = *(int *)(__get_tls() + 0x38); + tid = __get_tls()->tib_tid; if (tid > 0) { return tid; } diff --git a/libc/intrin/intrin.mk b/libc/intrin/intrin.mk index 7d32d0143..ddc5615e2 100644 --- a/libc/intrin/intrin.mk +++ b/libc/intrin/intrin.mk @@ -93,11 +93,6 @@ o/$(MODE)/libc/intrin/futex_wait.o \ o/$(MODE)/libc/intrin/futex_wake.o \ o/$(MODE)/libc/intrin/gettid.greg.o \ o/$(MODE)/libc/intrin/sys_gettid.greg.o \ -o/$(MODE)/libc/intrin/pthread_mutex_lock.o \ -o/$(MODE)/libc/intrin/pthread_mutex_wait.o \ -o/$(MODE)/libc/intrin/pthread_mutex_wake.o \ -o/$(MODE)/libc/intrin/pthread_mutex_unlock.o \ -o/$(MODE)/libc/intrin/pthread_mutex_trylock.o \ o/$(MODE)/libc/intrin/_trylock_debug_4.o \ o/$(MODE)/libc/intrin/_spinlock_debug_4.o: private \ OVERRIDE_CFLAGS += \ diff --git a/libc/intrin/kprintf.greg.c b/libc/intrin/kprintf.greg.c index 6d7bbf1ef..472166246 100644 --- a/libc/intrin/kprintf.greg.c +++ b/libc/intrin/kprintf.greg.c @@ -35,9 +35,7 @@ #include "libc/limits.h" #include "libc/log/internal.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/rdtsc.h" -#include "libc/nexgen32e/threaded.h" #include "libc/nexgen32e/uart.internal.h" #include "libc/nt/process.h" #include "libc/nt/runtime.h" @@ -52,6 +50,8 @@ #include "libc/str/utf16.h" #include "libc/sysv/consts/nr.h" #include "libc/sysv/consts/prot.h" +#include "libc/thread/tls.h" +#include "libc/thread/tls2.h" extern hidden struct SymbolTable *__symtab; @@ -312,7 +312,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, if (!__tls_enabled) { x = __pid; } else { - x = *(int *)(__get_tls_privileged() + 0x38); + x = __get_tls_privileged()->tib_tid; } } else { x = 666; diff --git a/libc/intrin/mmi.init.S b/libc/intrin/mmi.init.S index 54842d9c8..1f0a77bc8 100644 --- a/libc/intrin/mmi.init.S +++ b/libc/intrin/mmi.init.S @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" .init.start 200,_init__mmi diff --git a/libc/intrin/mmi_lock.c b/libc/intrin/mmi_lock.c index 7dc44fd97..3c4e4c672 100644 --- a/libc/intrin/mmi_lock.c +++ b/libc/intrin/mmi_lock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/runtime/memtrack.internal.h" extern pthread_mutex_t __mmi_lock_obj; diff --git a/libc/intrin/mmi_lock_obj.c b/libc/intrin/mmi_lock_obj.c index c34d116bc..e33861b9d 100644 --- a/libc/intrin/mmi_lock_obj.c +++ b/libc/intrin/mmi_lock_obj.c @@ -16,6 +16,6 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" pthread_mutex_t __mmi_lock_obj; // recursive :'( diff --git a/libc/intrin/phtread_attr_setschedpolicy.c b/libc/intrin/phtread_attr_setschedpolicy.c index 4de984b9d..dafa1f46c 100644 --- a/libc/intrin/phtread_attr_setschedpolicy.c +++ b/libc/intrin/phtread_attr_setschedpolicy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets thread scheduler policy attribute, e.g. diff --git a/libc/intrin/pthread.h b/libc/intrin/pthread.h deleted file mode 100644 index 576beb427..000000000 --- a/libc/intrin/pthread.h +++ /dev/null @@ -1,205 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_RUNTIME_PTHREAD_H_ -#define COSMOPOLITAN_LIBC_RUNTIME_PTHREAD_H_ - -#define PTHREAD_KEYS_MAX 64 -#define PTHREAD_STACK_MIN FRAMESIZE -#define PTHREAD_DESTRUCTOR_ITERATIONS 4 - -#define PTHREAD_BARRIER_SERIAL_THREAD 31337 - -#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL -#define PTHREAD_MUTEX_NORMAL 0 -#define PTHREAD_MUTEX_RECURSIVE 1 -#define PTHREAD_MUTEX_ERRORCHECK 2 -#define PTHREAD_MUTEX_STALLED 0 -#define PTHREAD_MUTEX_ROBUST 1 - -#define PTHREAD_PROCESS_DEFAULT PTHREAD_PROCESS_PRIVATE -#define PTHREAD_PROCESS_PRIVATE 0 -#define PTHREAD_PROCESS_SHARED 1 - -#define PTHREAD_CREATE_JOINABLE 0 -#define PTHREAD_CREATE_DETACHED 1 - -#define PTHREAD_INHERIT_SCHED 0 -#define PTHREAD_EXPLICIT_SCHED 1 - -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -/* clang-format off */ -#define PTHREAD_ONCE_INIT {0} -#define PTHREAD_COND_INITIALIZER {PTHREAD_PROCESS_DEFAULT} -#define PTHREAD_BARRIER_INITIALIZER {PTHREAD_PROCESS_DEFAULT} -#define PTHREAD_RWLOCK_INITIALIZER {PTHREAD_PROCESS_DEFAULT} -#define PTHREAD_MUTEX_INITIALIZER {PTHREAD_MUTEX_DEFAULT, \ - PTHREAD_PROCESS_DEFAULT} -/* clang-format on */ - -typedef uintptr_t pthread_t; -typedef int pthread_id_np_t; -typedef char pthread_condattr_t; -typedef char pthread_rwlockattr_t; -typedef char pthread_barrierattr_t; -typedef unsigned pthread_key_t; -typedef void (*pthread_key_dtor)(void *); - -typedef struct pthread_once_s { - _Atomic(char) lock; -} pthread_once_t; - -typedef struct pthread_spinlock_s { - _Atomic(char) lock; -} pthread_spinlock_t; - -typedef struct pthread_mutex_s { - char type; - char pshared; - int reent; - _Atomic(int) lock; - _Atomic(int) waits; -} pthread_mutex_t; - -typedef struct pthread_mutexattr_s { - char type; - char pshared; -} pthread_mutexattr_t; - -typedef struct pthread_cond_s { - char pshared; - _Atomic(int) waits; - _Atomic(unsigned) seq; -} pthread_cond_t; - -typedef struct pthread_barrier_s { - char pshared; - int count; - _Atomic(int) waits; - _Atomic(int) popped; -} pthread_barrier_t; - -typedef struct pthread_rwlock_s { - char pshared; - _Atomic(int) lock; - _Atomic(int) waits; -} pthread_rwlock_t; - -typedef struct pthread_attr_s { - char detachstate; - char inheritsched; - int schedparam; - int schedpolicy; - int scope; - unsigned guardsize; - unsigned stacksize; - char *stackaddr; -} pthread_attr_t; - -int pthread_yield(void); -void pthread_exit(void *) wontreturn; -pthread_t pthread_self(void) pureconst; -pthread_id_np_t pthread_getthreadid_np(void); -int64_t pthread_getunique_np(pthread_t); -int pthread_setname_np(pthread_t, const char *); -int pthread_getname_np(pthread_t, char *, size_t); -int pthread_getattr_np(pthread_t, pthread_attr_t *); -int pthread_attr_init(pthread_attr_t *); -int pthread_attr_destroy(pthread_attr_t *); -int pthread_attr_getdetachstate(const pthread_attr_t *, int *); -int pthread_attr_setdetachstate(pthread_attr_t *, int); -int pthread_attr_getguardsize(const pthread_attr_t *, size_t *); -int pthread_attr_setguardsize(pthread_attr_t *, size_t); -int pthread_attr_getinheritsched(const pthread_attr_t *, int *); -int pthread_attr_setinheritsched(pthread_attr_t *, int); -int pthread_attr_getschedpolicy(const pthread_attr_t *, int *); -int pthread_attr_setschedpolicy(pthread_attr_t *, int); -int pthread_attr_getscope(const pthread_attr_t *, int *); -int pthread_attr_setscope(pthread_attr_t *, int); -int pthread_attr_getstack(const pthread_attr_t *, void **, size_t *); -int pthread_attr_setstack(pthread_attr_t *, void *, size_t); -int pthread_attr_getstacksize(const pthread_attr_t *, size_t *); -int pthread_attr_setstacksize(pthread_attr_t *, size_t); -int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), - void *); -int pthread_detach(pthread_t); -int pthread_cancel(pthread_t); -int pthread_join(pthread_t, void **); -int pthread_equal(pthread_t, pthread_t); -int pthread_once(pthread_once_t *, void (*)(void)); -int pthread_spin_init(pthread_spinlock_t *, int); -int pthread_spin_destroy(pthread_spinlock_t *); -int pthread_spin_lock(pthread_spinlock_t *); -int pthread_spin_unlock(pthread_spinlock_t *); -int pthread_spin_trylock(pthread_spinlock_t *); -int pthread_mutexattr_init(pthread_mutexattr_t *); -int pthread_mutexattr_destroy(pthread_mutexattr_t *); -int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *); -int pthread_mutexattr_settype(pthread_mutexattr_t *, int); -int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); -int pthread_mutexattr_getpshared(const pthread_mutexattr_t *, int *); -int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); -int pthread_mutex_lock(pthread_mutex_t *); -int pthread_mutex_unlock(pthread_mutex_t *); -int pthread_mutex_trylock(pthread_mutex_t *); -int pthread_mutex_destroy(pthread_mutex_t *); -int pthread_mutex_consistent(pthread_mutex_t *); -int pthread_condattr_init(pthread_condattr_t *); -int pthread_condattr_destroy(pthread_condattr_t *); -int pthread_condattr_setpshared(pthread_condattr_t *, int); -int pthread_condattr_getpshared(const pthread_condattr_t *, int *); -int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); -int pthread_cond_destroy(pthread_cond_t *); -int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); -int pthread_cond_broadcast(pthread_cond_t *); -int pthread_cond_signal(pthread_cond_t *); -int pthread_rwlockattr_init(pthread_rwlockattr_t *); -int pthread_rwlockattr_destroy(pthread_rwlockattr_t *); -int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int); -int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *, int *); -int pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *); -int pthread_rwlock_destroy(pthread_rwlock_t *); -int pthread_rwlock_rdlock(pthread_rwlock_t *); -int pthread_rwlock_tryrdlock(pthread_rwlock_t *); -int pthread_rwlock_wrlock(pthread_rwlock_t *); -int pthread_rwlock_trywrlock(pthread_rwlock_t *); -int pthread_rwlock_unlock(pthread_rwlock_t *); -int pthread_key_create(pthread_key_t *, pthread_key_dtor); -int pthread_key_delete(pthread_key_t); -int pthread_setspecific(pthread_key_t, void *); -void *pthread_getspecific(pthread_key_t); -int pthread_barrierattr_init(pthread_barrierattr_t *); -int pthread_barrierattr_destroy(pthread_barrierattr_t *); -int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *); -int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); -int pthread_barrier_wait(pthread_barrier_t *); -int pthread_barrier_destroy(pthread_barrier_t *); -int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, - unsigned); - -#define pthread_spin_init(pSpin, multiprocess) ((pSpin)->lock = 0, 0) -#define pthread_spin_destroy(pSpin) ((pSpin)->lock = -1, 0) -#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 && \ - !defined(__STRICT_ANSI__) -extern const errno_t EBUSY; -#define pthread_spin_lock(pSpin) \ - ({ \ - pthread_spinlock_t *_s = pSpin; \ - while (__atomic_test_and_set(&_s->lock, __ATOMIC_SEQ_CST)) donothing; \ - 0; \ - }) -#define pthread_spin_unlock(pSpin) \ - ({ \ - pthread_spinlock_t *_s = pSpin; \ - __atomic_store_n(&_s->lock, 0, __ATOMIC_RELAXED); \ - 0; \ - }) -#define pthread_spin_trylock(pSpin) \ - ({ \ - pthread_spinlock_t *_s = pSpin; \ - __atomic_test_and_set(&_s->lock, __ATOMIC_SEQ_CST) ? EBUSY : 0; \ - }) -#endif /* GCC 4.7+ */ - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_LIBC_RUNTIME_PTHREAD_H_ */ diff --git a/libc/intrin/pthread_mutex_init.c b/libc/intrin/pthread_mutex_init.c index 0645c6a0e..242aff95e 100644 --- a/libc/intrin/pthread_mutex_init.c +++ b/libc/intrin/pthread_mutex_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes mutex. @@ -27,8 +27,8 @@ int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { *mutex = (pthread_mutex_t){ - attr ? attr->type : PTHREAD_MUTEX_DEFAULT, - attr ? attr->pshared : PTHREAD_PROCESS_DEFAULT, + attr ? attr->type : 0, + attr ? attr->pshared : 0, }; return 0; } diff --git a/libc/intrin/pthread_mutex_lock.c b/libc/intrin/pthread_mutex_lock.c index 374f97a2d..88542275d 100644 --- a/libc/intrin/pthread_mutex_lock.c +++ b/libc/intrin/pthread_mutex_lock.c @@ -22,9 +22,9 @@ #include "libc/intrin/asmflag.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/linux/futex.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/sysv/consts/futex.h" #include "libc/sysv/consts/nr.h" diff --git a/libc/intrin/pthread_mutex_trylock.c b/libc/intrin/pthread_mutex_trylock.c index 6d1a20151..6c15e6c2c 100644 --- a/libc/intrin/pthread_mutex_trylock.c +++ b/libc/intrin/pthread_mutex_trylock.c @@ -20,7 +20,7 @@ #include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Locks mutex if it isn't locked already. diff --git a/libc/intrin/pthread_mutex_unlock.c b/libc/intrin/pthread_mutex_unlock.c index 430b9c8c6..3ddd820a3 100644 --- a/libc/intrin/pthread_mutex_unlock.c +++ b/libc/intrin/pthread_mutex_unlock.c @@ -22,7 +22,7 @@ #include "libc/errno.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Releases mutex. diff --git a/libc/intrin/pthread_once.c b/libc/intrin/pthread_once.c index 1d13fd863..7ca91dbae 100644 --- a/libc/intrin/pthread_once.c +++ b/libc/intrin/pthread_once.c @@ -20,7 +20,7 @@ #include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #define INIT 0 #define CALLING 1 diff --git a/libc/intrin/pthread_yield.c b/libc/intrin/pthread_yield.c index 322e0799c..273eabde4 100644 --- a/libc/intrin/pthread_yield.c +++ b/libc/intrin/pthread_yield.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/strace.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Yields current thread's remaining timeslice to operating system. diff --git a/libc/intrin/rand64.c b/libc/intrin/rand64.c index 6b915c2a1..3d48c6b94 100644 --- a/libc/intrin/rand64.c +++ b/libc/intrin/rand64.c @@ -17,9 +17,9 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/_getauxval.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/nexgen32e/rdtsc.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" diff --git a/libc/intrin/tlsisrequired.c b/libc/intrin/tlsisrequired.c index a428d3144..08fb4d41a 100644 --- a/libc/intrin/tlsisrequired.c +++ b/libc/intrin/tlsisrequired.c @@ -16,9 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" -void TlsIsRequired(void) { +void __require_tls(void) { if (!__tls_enabled) { notpossible; } diff --git a/libc/intrin/wait0.c b/libc/intrin/wait0.c index 0bfa2bf12..4a0a1493d 100644 --- a/libc/intrin/wait0.c +++ b/libc/intrin/wait0.c @@ -20,7 +20,7 @@ #include "libc/dce.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/wait0.internal.h" #include "libc/linux/futex.h" diff --git a/libc/isystem/limits.h b/libc/isystem/limits.h index a6abd1c8b..6eb8b5ff6 100644 --- a/libc/isystem/limits.h +++ b/libc/isystem/limits.h @@ -1,6 +1,6 @@ #ifndef LIBC_ISYSTEM_LIMITS_H_ #define LIBC_ISYSTEM_LIMITS_H_ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/limits.h" #include "libc/sysv/consts/_posix.h" #include "libc/sysv/consts/iov.h" diff --git a/libc/isystem/pthread.h b/libc/isystem/pthread.h index 9e5e99b40..77ba04720 100644 --- a/libc/isystem/pthread.h +++ b/libc/isystem/pthread.h @@ -1,5 +1,5 @@ #ifndef LIBC_ISYSTEM_PTHREAD_H_ #define LIBC_ISYSTEM_PTHREAD_H_ -#include "libc/intrin/pthread.h" -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread.h" +#include "libc/thread/thread2.h" #endif /* LIBC_ISYSTEM_PTHREAD_H_ */ diff --git a/libc/log/backtrace2.c b/libc/log/backtrace2.c index 65b00e55e..2459941a4 100644 --- a/libc/log/backtrace2.c +++ b/libc/log/backtrace2.c @@ -37,8 +37,6 @@ #include "libc/mem/alg.h" #include "libc/mem/bisectcarleft.internal.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/gc.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" @@ -49,7 +47,7 @@ #include "libc/sysv/consts/fileno.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/sig.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #include "libc/x/x.h" #define kBacktraceMaxFrames 128 @@ -109,7 +107,7 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) { argv[i++] = "-a"; /* filter out w/ shell script wrapper for old versions */ argv[i++] = "-pCife"; argv[i++] = debugbin; - garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0; + garbage = __tls_enabled ? __get_tls()->tib_garbages : 0; gi = garbage ? garbage->i : 0; for (frame = bp; frame && i < kBacktraceMaxFrames - 1; frame = frame->next) { addr = frame->addr; diff --git a/libc/log/backtrace3.c b/libc/log/backtrace3.c index eb07cc176..2e5b3c82d 100644 --- a/libc/log/backtrace3.c +++ b/libc/log/backtrace3.c @@ -26,14 +26,12 @@ #include "libc/macros.internal.h" #include "libc/mem/bisectcarleft.internal.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/stackframe.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/memtrack.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/str/str.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #define LIMIT 100 @@ -57,7 +55,7 @@ noinstrument noasan int PrintBacktraceUsingSymbols(int fd, struct Garbages *garbage; const struct StackFrame *frame; if (!bp) bp = __builtin_frame_address(0); - garbage = __tls_enabled ? ((cthread_t)__get_tls())->garbages : 0; + garbage = __tls_enabled ? __get_tls()->tib_garbages : 0; gi = garbage ? garbage->i : 0; for (i = 0, frame = bp; frame; frame = frame->next) { if (++i == LIMIT) { diff --git a/libc/log/oncrash.c b/libc/log/oncrash.c index 8fffe7269..5f722b23d 100644 --- a/libc/log/oncrash.c +++ b/libc/log/oncrash.c @@ -35,7 +35,7 @@ #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/stackframe.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/runtime/pc.internal.h" #include "libc/runtime/runtime.h" diff --git a/libc/log/printgarbage.c b/libc/log/printgarbage.c index 911a1d986..c14d51f51 100644 --- a/libc/log/printgarbage.c +++ b/libc/log/printgarbage.c @@ -20,10 +20,8 @@ #include "libc/intrin/kprintf.h" #include "libc/log/log.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" #include "libc/stdio/stdio.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" // clang-format off /** @@ -38,7 +36,7 @@ void PrintGarbage(void) { kprintf(" SHADOW STACK @ %p\n", __builtin_frame_address(0)); kprintf("garbage ent. parent frame original ret callback arg \n"); kprintf("------------ ------------ ------------------ ------------------ ------------------\n"); - if ((g = __tls_enabled ? ((cthread_t)__get_tls())->garbages:0) && g->i) { + if ((g = __tls_enabled ? __get_tls()->tib_garbages:0) && g->i) { for (i = g->i; i--;) { symbol = __get_symbol_by_addr(g->p[i].ret); if (symbol) { diff --git a/libc/mem/gc.c b/libc/mem/gc.c index 3e0dfde84..96e493c53 100644 --- a/libc/mem/gc.c +++ b/libc/mem/gc.c @@ -20,12 +20,10 @@ #include "libc/intrin/likely.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/stackframe.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" static inline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame, struct StackFrame *parent, @@ -36,11 +34,11 @@ static inline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame, static void TeardownGc(void) { int i; - cthread_t tls; struct Garbages *g; + struct CosmoTib *t; if (__tls_enabled) { - tls = (cthread_t)__get_tls(); - if ((g = tls->garbages)) { + t = __get_tls(); + if ((g = t->tib_garbages)) { // exit() currently doesn't use _gclongjmp() like pthread_exit() // so we need to run the deferred functions manually. while (g->i) { @@ -61,18 +59,18 @@ __attribute__((__constructor__)) static void InitializeGc(void) { // then rewrite caller's return address on stack. static void DeferFunction(struct StackFrame *frame, void *fn, void *arg) { int n2; - cthread_t tls; struct Garbage *p2; struct Garbages *g; - TlsIsRequired(); - tls = (cthread_t)__get_tls(); - g = tls->garbages; + struct CosmoTib *t; + __require_tls(); + t = __get_tls(); + g = t->tib_garbages; if (UNLIKELY(!g)) { if (!(g = malloc(sizeof(struct Garbages)))) notpossible; g->i = 0; g->n = 4; if (!(g->p = malloc(g->n * sizeof(struct Garbage)))) notpossible; - tls->garbages = g; + t->tib_garbages = g; } else if (UNLIKELY(g->i == g->n)) { p2 = g->p; n2 = g->n + (g->n >> 1); diff --git a/libc/nexgen32e/gc.S b/libc/nexgen32e/gc.S index 83143e336..d9a41adff 100644 --- a/libc/nexgen32e/gc.S +++ b/libc/nexgen32e/gc.S @@ -34,7 +34,7 @@ // @see test/libc/runtime/gc_test.c // @threadsafe __gc: mov %fs:0,%rcx # __get_tls() - mov 0x18(%rcx),%rcx # cthread_t::garbages + mov 0x18(%rcx),%rcx # tls::garbages decl (%rcx) # ++g->i mov (%rcx),%r8d # r8 = g->i mov 8(%rcx),%r9 # r9 = g->p diff --git a/libc/nexgen32e/gclongjmp.S b/libc/nexgen32e/gclongjmp.S index 4bfdd3755..fc98ca623 100644 --- a/libc/nexgen32e/gclongjmp.S +++ b/libc/nexgen32e/gclongjmp.S @@ -35,7 +35,7 @@ _gclongjmp: mov %rsp,%rbp .profilable mov %fs:0,%r12 # __get_tls() - mov 0x18(%r12),%r12 # cthread_t::garbages + mov 0x18(%r12),%r12 # Tls::garbages test %r12,%r12 jz 0f movl (%r12),%r13d # garbages.i diff --git a/libc/nexgen32e/threaded.c b/libc/nexgen32e/threaded.c index 83c56b1a9..6226e1bff 100644 --- a/libc/nexgen32e/threaded.c +++ b/libc/nexgen32e/threaded.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" /** * Contains TID of main thread or 0 if threading isn't enabled. diff --git a/libc/nexgen32e/threaded.h b/libc/nexgen32e/threaded.h deleted file mode 100644 index 7878eb841..000000000 --- a/libc/nexgen32e/threaded.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_NEXGEN32E_THREADED_H_ -#define COSMOPOLITAN_LIBC_NEXGEN32E_THREADED_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -extern int __threaded; -extern bool __tls_enabled; -extern unsigned __tls_index; - -void TlsIsRequired(void); -void *__initialize_tls(char[64]); -void __install_tls(char[64]); - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_THREADED_H_ */ diff --git a/libc/runtime/construct.S b/libc/runtime/construct.S deleted file mode 100644 index 953e8b4d1..000000000 --- a/libc/runtime/construct.S +++ /dev/null @@ -1,34 +0,0 @@ -/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ -│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/runtime/internal.h" -#include "libc/macros.internal.h" -.text.startup - -// Calls global initialization functions. -// -// @param r12 is argc -// @param r13 is argv -// @param r14 is environ -// @param r15 is auxv -_construct: - push %rbp - mov %rsp,%rbp - pop %rbp - ret - .endfn _construct,globl diff --git a/libc/runtime/enable_threads.c b/libc/runtime/enable_threads.c index 08ededed8..47a17ee3c 100644 --- a/libc/runtime/enable_threads.c +++ b/libc/runtime/enable_threads.c @@ -19,7 +19,7 @@ #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/strace.internal.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/runtime.h" extern int __threadcalls_end[]; diff --git a/libc/runtime/enable_tls.c b/libc/runtime/enable_tls.c index a6c4f9bb3..0d4ba2ccf 100644 --- a/libc/runtime/enable_tls.c +++ b/libc/runtime/enable_tls.c @@ -26,14 +26,13 @@ #include "libc/log/libfatal.internal.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/msr.h" -#include "libc/nexgen32e/threaded.h" #include "libc/nt/thread.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/stdalign.internal.h" #include "libc/str/str.h" #include "libc/sysv/consts/nrlinux.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #include "third_party/xed/x86.h" #define __NR_sysarch 0x000000a5 // freebsd+netbsd @@ -48,7 +47,7 @@ #define _TLSZ ((intptr_t)_tls_size) #define _TLDZ ((intptr_t)_tdata_size) -#define _TIBZ sizeof(struct cthread_descriptor_t) +#define _TIBZ sizeof(struct CosmoTib) int sys_enable_tls(); @@ -97,14 +96,14 @@ _Alignas(long) static char __static_tls[5008]; */ privileged void __enable_tls(void) { size_t siz; - cthread_t tib; + struct CosmoTib *tib; char *mem, *tls; siz = ROUNDUP(_TLSZ + _TIBZ, alignof(__static_tls)); if (siz <= sizeof(__static_tls)) { // if tls requirement is small then use the static tls block // which helps avoid a system call for appes with little tls // this is crucial to keeping life.com 16 kilobytes in size! - _Static_assert(alignof(__static_tls) >= alignof(cthread_t)); + _Static_assert(alignof(__static_tls) >= alignof(struct CosmoTib)); mem = __static_tls; } else { // if this binary needs a hefty tls block then we'll bank on @@ -116,17 +115,17 @@ privileged void __enable_tls(void) { mem = weaken(_mapanon)(siz); assert(mem); } - tib = (cthread_t)(mem + siz - _TIBZ); + tib = (struct CosmoTib *)(mem + siz - _TIBZ); tls = mem + siz - _TIBZ - _TLSZ; - tib->self = tib; - tib->self2 = tib; - tib->err = __errno; + tib->tib_self = tib; + tib->tib_self2 = tib; + tib->tib_errno = __errno; if (IsLinux()) { // gnu/systemd guarantees pid==tid for the main thread so we can // avoid issuing a superfluous system call at startup in program - tib->tid = __pid; + tib->tib_tid = __pid; } else { - tib->tid = sys_gettid(); + tib->tib_tid = sys_gettid(); } __repmovsb(tls, _tdata_start, _TLDZ); diff --git a/libc/runtime/fork-nt.c b/libc/runtime/fork-nt.c index 93401e7a1..73ebcb2e6 100644 --- a/libc/runtime/fork-nt.c +++ b/libc/runtime/fork-nt.c @@ -29,7 +29,7 @@ #include "libc/mem/alloca.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/nt2sysv.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/nt/console.h" #include "libc/nt/createfile.h" #include "libc/nt/enum/accessmask.h" diff --git a/libc/runtime/fork.c b/libc/runtime/fork.c index e717e421e..1d50b3fc1 100644 --- a/libc/runtime/fork.c +++ b/libc/runtime/fork.c @@ -22,11 +22,10 @@ #include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/dce.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" #include "libc/nt/process.h" #include "libc/runtime/internal.h" #include "libc/sysv/consts/sig.h" +#include "libc/thread/tls.h" /** * Creates new process. @@ -61,7 +60,7 @@ int fork(void) { parent = __pid; __pid = dx; if (__tls_enabled) { - *(int *)(__get_tls() + 0x38) = IsLinux() ? dx : sys_gettid(); + __get_tls()->tib_tid = IsLinux() ? dx : sys_gettid(); } STRACE("fork() → 0 (child of %d)", parent); sigprocmask(SIG_SETMASK, &old, 0); diff --git a/libc/runtime/ftracer.c b/libc/runtime/ftracer.c index 44d7a611d..4bdbd82fd 100644 --- a/libc/runtime/ftracer.c +++ b/libc/runtime/ftracer.c @@ -23,13 +23,12 @@ #include "libc/intrin/kprintf.h" #include "libc/intrin/nopl.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/stackframe.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/internal.h" #include "libc/runtime/stack.h" #include "libc/runtime/symbols.internal.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" +#include "libc/thread/tls2.h" #define MAX_NESTING 512 @@ -44,7 +43,7 @@ void ftrace_hook(void); static int g_stackdigs; -static struct Ftrace g_ftrace; +static struct CosmoFtrace g_ftrace; static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) { int nesting = -2; @@ -55,12 +54,12 @@ static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) { return MAX(0, nesting); } -static privileged inline int GetNestingLevel(struct Ftrace *ft, +static privileged inline int GetNestingLevel(struct CosmoFtrace *ft, struct StackFrame *sf) { int nesting; nesting = GetNestingLevelImpl(sf); - if (nesting < ft->skew) ft->skew = nesting; - nesting -= ft->skew; + if (nesting < ft->ft_skew) ft->ft_skew = nesting; + nesting -= ft->ft_skew; return MIN(MAX_NESTING, nesting); } @@ -73,27 +72,27 @@ static privileged inline int GetNestingLevel(struct Ftrace *ft, */ privileged void ftracer(void) { long stackuse; - struct Ftrace *ft; + struct CosmoFtrace *ft; struct StackFrame *sf; if (__tls_enabled) { - ft = (struct Ftrace *)(__get_tls_privileged() + 0x08); + ft = &__get_tls_privileged()->tib_ftrace; } else { ft = &g_ftrace; } - if (_cmpxchg(&ft->once, false, true)) { - ft->lastaddr = -1; - ft->skew = GetNestingLevelImpl(__builtin_frame_address(0)); + if (_cmpxchg(&ft->ft_once, false, true)) { + ft->ft_lastaddr = -1; + ft->ft_skew = GetNestingLevelImpl(__builtin_frame_address(0)); } - if (_cmpxchg(&ft->noreentry, false, true)) { + if (_cmpxchg(&ft->ft_noreentry, false, true)) { sf = __builtin_frame_address(0); sf = sf->next; - if (sf->addr != ft->lastaddr) { + if (sf->addr != ft->ft_lastaddr) { stackuse = GetStackAddr() + GetStackSize() - (intptr_t)sf; kprintf("%rFUN %6P %'13T %'*ld %*s%t\n", g_stackdigs, stackuse, GetNestingLevel(ft, sf) * 2, "", sf->addr); - ft->lastaddr = sf->addr; + ft->ft_lastaddr = sf->addr; } - ft->noreentry = false; + ft->ft_noreentry = false; } } diff --git a/libc/runtime/getsymboltable.c b/libc/runtime/getsymboltable.c index 742504416..44c897b14 100644 --- a/libc/runtime/getsymboltable.c +++ b/libc/runtime/getsymboltable.c @@ -20,7 +20,7 @@ #include "libc/calls/strace.internal.h" #include "libc/intrin/bits.h" #include "libc/intrin/promises.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/runtime/internal.h" diff --git a/libc/runtime/memtrack.internal.h b/libc/runtime/memtrack.internal.h index 746602693..0e1c10af5 100644 --- a/libc/runtime/memtrack.internal.h +++ b/libc/runtime/memtrack.internal.h @@ -5,7 +5,7 @@ #include "libc/dce.h" #include "libc/intrin/nopl.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/nt/version.h" #include "libc/runtime/stack.h" #include "libc/sysv/consts/ss.h" diff --git a/libc/runtime/mmap.c b/libc/runtime/mmap.c index 4bdfeaaf9..71efc97da 100644 --- a/libc/runtime/mmap.c +++ b/libc/runtime/mmap.c @@ -28,7 +28,7 @@ #include "libc/intrin/bits.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/likely.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/weaken.h" #include "libc/limits.h" diff --git a/libc/stdio/dirstream.c b/libc/stdio/dirstream.c index 6a62df49c..60531888e 100644 --- a/libc/stdio/dirstream.c +++ b/libc/stdio/dirstream.c @@ -26,7 +26,7 @@ #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/nopl.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/weaken.h" #include "libc/mem/mem.h" #include "libc/nt/enum/fileflagandattributes.h" diff --git a/libc/stdio/fdopen.c b/libc/stdio/fdopen.c index 4a25238cd..bcbb82033 100644 --- a/libc/stdio/fdopen.c +++ b/libc/stdio/fdopen.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" diff --git a/libc/stdio/fflush.internal.h b/libc/stdio/fflush.internal.h index a00e504fb..6eb92461e 100644 --- a/libc/stdio/fflush.internal.h +++ b/libc/stdio/fflush.internal.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_ #define COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_ #include "libc/intrin/nopl.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/stdio/stdio.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/stdio/fflush_unlocked.c b/libc/stdio/fflush_unlocked.c index 0746536be..a00ebe969 100644 --- a/libc/stdio/fflush_unlocked.c +++ b/libc/stdio/fflush_unlocked.c @@ -19,7 +19,7 @@ #include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/intrin/bits.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/pushpop.h" #include "libc/macros.internal.h" #include "libc/mem/arraylist.internal.h" diff --git a/libc/stdio/flushlbf.c b/libc/stdio/flushlbf.c index 69bd5e4c9..ff4abcb5d 100644 --- a/libc/stdio/flushlbf.c +++ b/libc/stdio/flushlbf.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/stdio/fflush.internal.h" #include "libc/stdio/lock.h" #include "libc/stdio/stdio.h" diff --git a/libc/stdio/fmemopen.c b/libc/stdio/fmemopen.c index 28c12cd74..148850be3 100644 --- a/libc/stdio/fmemopen.c +++ b/libc/stdio/fmemopen.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" diff --git a/libc/stdio/iconv.c b/libc/stdio/iconv.c index b71ed9160..8a09fda96 100644 --- a/libc/stdio/iconv.c +++ b/libc/stdio/iconv.c @@ -27,11 +27,10 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/gettls.h" #include "libc/stdio/iconv.h" -#include "libc/str/str.h" -#include "libc/thread/thread.h" #include "libc/str/locale.h" +#include "libc/str/str.h" +#include "libc/thread/tls.h" // clang-format off asm(".ident\t\"\\n\\n\ @@ -283,7 +282,7 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri int err; unsigned char type = map[-1]; unsigned char totype = tomap[-1]; - locale_t *ploc = &((cthread_t)__get_tls())->locale; + locale_t *ploc = (locale_t *)&__get_tls()->tib_locale; locale_t loc = *ploc; if (!in || !*in || !*inb) return 0; diff --git a/libc/stdio/lock.h b/libc/stdio/lock.h index fe38e179d..8b33439b6 100644 --- a/libc/stdio/lock.h +++ b/libc/stdio/lock.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_STDIO_LOCK_H_ #define COSMOPOLITAN_LIBC_STDIO_LOCK_H_ #include "libc/intrin/nopl.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/stdio/stdio.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/stdio/readdir64.S b/libc/stdio/readdir64.S index 471f01bdb..ea6608a79 100644 --- a/libc/stdio/readdir64.S +++ b/libc/stdio/readdir64.S @@ -1,5 +1,5 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ This is free and unencumbered software released into the public domain. │ │ │ @@ -26,5 +26,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" -readdir64: jmp readdir +readdir64: + jmp readdir .endfn readdir64,globl diff --git a/libc/stdio/stderr-init.S b/libc/stdio/stderr-init.S index b5be107f5..7dc295db4 100644 --- a/libc/stdio/stderr-init.S +++ b/libc/stdio/stderr-init.S @@ -20,7 +20,7 @@ #include "libc/dce.h" #include "libc/calls/calls.h" #include "libc/sysv/consts/fileno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" .init.start 400,_init_stderr diff --git a/libc/stdio/stdin-init.S b/libc/stdio/stdin-init.S index 2f6d03d0d..fa7eda46f 100644 --- a/libc/stdio/stdin-init.S +++ b/libc/stdio/stdin-init.S @@ -20,7 +20,7 @@ #include "libc/dce.h" #include "libc/calls/calls.h" #include "libc/sysv/consts/fileno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" .init.start 400,_init_stdin diff --git a/libc/stdio/stdio.h b/libc/stdio/stdio.h index 7180a1457..c7c03d81c 100644 --- a/libc/stdio/stdio.h +++ b/libc/stdio/stdio.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_STDIO_STDIO_H_ #define COSMOPOLITAN_LIBC_STDIO_STDIO_H_ #include "libc/fmt/pflink.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #define _STDIO_H #define L_ctermid 20 diff --git a/libc/stdio/stdout-init.S b/libc/stdio/stdout-init.S index 19c549e10..f43249810 100644 --- a/libc/stdio/stdout-init.S +++ b/libc/stdio/stdout-init.S @@ -20,7 +20,7 @@ #include "libc/dce.h" #include "libc/calls/calls.h" #include "libc/sysv/consts/fileno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" .init.start 400,_init_stdout diff --git a/libc/str/langinfo.c b/libc/str/langinfo.c index 9451b444d..41aad1030 100644 --- a/libc/str/langinfo.c +++ b/libc/str/langinfo.c @@ -25,11 +25,10 @@ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/nexgen32e/gettls.h" -#include "libc/thread/thread.h" #include "libc/str/langinfo.h" #include "libc/str/locale.h" #include "libc/str/nltypes.h" +#include "libc/thread/tls.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ @@ -101,5 +100,5 @@ char *nl_langinfo_l(nl_item item, locale_t loc) char *nl_langinfo(nl_item item) { - return nl_langinfo_l(item, ((cthread_t)__get_tls())->locale); + return nl_langinfo_l(item, (locale_t)__get_tls()->tib_locale); } diff --git a/libc/sysv/errno_location.greg.c b/libc/sysv/errno_location.greg.c index 425939f73..fe1fc4cd6 100644 --- a/libc/sysv/errno_location.greg.c +++ b/libc/sysv/errno_location.greg.c @@ -17,16 +17,12 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls2.h" /** * Returns address of errno variable. - * - * @see __initialize_tls() - * @see __install_tls() */ privileged nocallersavedregisters errno_t *(__errno_location)(void) { if (!__tls_enabled) return &__errno; - return (errno_t *)(__get_tls_privileged() + 0x3c); + return &__get_tls_privileged()->tib_errno; } diff --git a/libc/testlib/testrunner.c b/libc/testlib/testrunner.c index bd67be048..ea8627a26 100644 --- a/libc/testlib/testrunner.c +++ b/libc/testlib/testrunner.c @@ -28,7 +28,7 @@ #include "libc/fmt/fmt.h" #include "libc/fmt/itoa.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/weaken.h" #include "libc/log/check.h" #include "libc/log/internal.h" diff --git a/libc/runtime/clone-linux.S b/libc/thread/clone-linux.S similarity index 100% rename from libc/runtime/clone-linux.S rename to libc/thread/clone-linux.S diff --git a/libc/runtime/winthreadlaunch.S b/libc/thread/clone-nt.S similarity index 100% rename from libc/runtime/winthreadlaunch.S rename to libc/thread/clone-nt.S diff --git a/libc/runtime/clone-openbsd.S b/libc/thread/clone-openbsd.S similarity index 100% rename from libc/runtime/clone-openbsd.S rename to libc/thread/clone-openbsd.S diff --git a/libc/runtime/clone.c b/libc/thread/clone.c similarity index 97% rename from libc/runtime/clone.c rename to libc/thread/clone.c index e869c4f13..866eb4429 100644 --- a/libc/runtime/clone.c +++ b/libc/thread/clone.c @@ -23,11 +23,8 @@ #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" -#include "libc/intrin/pthread.h" #include "libc/limits.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" #include "libc/nt/runtime.h" #include "libc/nt/thread.h" #include "libc/nt/thunk/msabi.h" @@ -42,6 +39,8 @@ #include "libc/sysv/errfuns.h" #include "libc/thread/freebsd.internal.h" #include "libc/thread/openbsd.internal.h" +#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #include "libc/thread/xnu.internal.h" #define __NR_thr_new 455 @@ -495,20 +494,6 @@ static int CloneLinux(int (*func)(void *arg, int tid), char *stk, size_t stksz, * _Exit1() which terminates the thread. Even though the callback says * it supports a return code, that'll only work on Linux and Windows. * - * The `tls` parameter is for thread-local storage. If you specify this - * then clone() will implicitly rewire libc (e.g. errno) to use TLS: - * - * static char tib[64]; - * __initialize_tls(tib); - * __install_tls(tib); - * - * If you want a main process TLS size that's larger call it manually. - * Once you've done the above and/or started creating your own threads - * you'll be able to access your `tls` thread information block, using - * - * char *p = __get_tls(); - * printf("errno is %d\n", *(int *)(p + 0x3c)); - * * This function follows the same ABI convention as the Linux userspace * libraries, with a few small changes. The varargs has been removed to * help prevent broken code, and the stack size and tls size parameters diff --git a/libc/thread/internal.h b/libc/thread/internal.h index 331e7899b..c1d9f7ea4 100644 --- a/libc/thread/internal.h +++ b/libc/thread/internal.h @@ -1,6 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_THREAD_INTERNAL_H_ #define COSMOPOLITAN_LIBC_THREAD_INTERNAL_H_ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/thread/mktls.c b/libc/thread/mktls.c index 911c2a18e..8b9fb1a3b 100644 --- a/libc/thread/mktls.c +++ b/libc/thread/mktls.c @@ -22,12 +22,12 @@ #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/thread/spawn.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" #define _TLSZ ((intptr_t)_tls_size) #define _TLDZ ((intptr_t)_tdata_size) -#define _TIBZ sizeof(struct cthread_descriptor_t) -#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, _Alignof(struct cthread_descriptor_t)) +#define _TIBZ sizeof(struct CosmoTib) +#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, _Alignof(struct CosmoTib)) /** * Allocates thread-local storage memory for new thread. @@ -35,18 +35,18 @@ */ char *_mktls(char **out_tib) { char *tls; - cthread_t tib; + struct CosmoTib *tib; // Allocate enough TLS memory for all the GNU Linuker (_tls_size) // organized _Thread_local data, as well as Cosmpolitan Libc (64) if (!(tls = calloc(1, _MEMZ))) return 0; // set up thread information block - tib = (cthread_t)(tls + _MEMZ - _TIBZ); - tib->self = tib; - tib->self2 = tib; - tib->err = 0; - tib->tid = -1; + tib = (struct CosmoTib *)(tls + _MEMZ - _TIBZ); + tib->tib_self = tib; + tib->tib_self2 = tib; + tib->tib_errno = 0; + tib->tib_tid = -1; memmove(tls, _tdata_start, _TLDZ); if (out_tib) { diff --git a/libc/thread/posixthread.internal.h b/libc/thread/posixthread.internal.h index 7a51695dc..84373c951 100644 --- a/libc/thread/posixthread.internal.h +++ b/libc/thread/posixthread.internal.h @@ -1,8 +1,8 @@ #ifndef COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_ #define COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_ #include "libc/calls/struct/sched_param.h" -#include "libc/intrin/pthread.h" #include "libc/runtime/runtime.h" +#include "libc/thread/thread.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ @@ -67,13 +67,14 @@ struct PosixThread { pthread_attr_t attr; }; -void _pthread_free(struct PosixThread *) hidden; -void _pthread_wait(struct PosixThread *) hidden; int _pthread_reschedule(struct PosixThread *) hidden; +int _pthread_setschedparam_freebsd(int, int, const struct sched_param *) hidden; +void _pthread_free(struct PosixThread *) hidden; +void _pthread_ungarbage(void) hidden; +void _pthread_wait(struct PosixThread *) hidden; void _pthread_zombies_add(struct PosixThread *) hidden; void _pthread_zombies_decimate(void) hidden; void _pthread_zombies_harvest(void) hidden; -int _pthread_setschedparam_freebsd(int, int, const struct sched_param *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/intrin/pthread_attr_destroy.c b/libc/thread/pthread_attr_destroy.c similarity index 98% rename from libc/intrin/pthread_attr_destroy.c rename to libc/thread/pthread_attr_destroy.c index b8690c31f..d93ffa7d2 100644 --- a/libc/intrin/pthread_attr_destroy.c +++ b/libc/thread/pthread_attr_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_attr_getdetachstate.c b/libc/thread/pthread_attr_getdetachstate.c similarity index 98% rename from libc/intrin/pthread_attr_getdetachstate.c rename to libc/thread/pthread_attr_getdetachstate.c index ada3daf78..742d26b00 100644 --- a/libc/intrin/pthread_attr_getdetachstate.c +++ b/libc/thread/pthread_attr_getdetachstate.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets thread detachable attribute. diff --git a/libc/intrin/pthread_attr_getguardsize.c b/libc/thread/pthread_attr_getguardsize.c similarity index 98% rename from libc/intrin/pthread_attr_getguardsize.c rename to libc/thread/pthread_attr_getguardsize.c index b91416e39..6b82d638f 100644 --- a/libc/intrin/pthread_attr_getguardsize.c +++ b/libc/thread/pthread_attr_getguardsize.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Returns size of unmapped pages at bottom of stack. diff --git a/libc/intrin/pthread_attr_getinheritsched.c b/libc/thread/pthread_attr_getinheritsched.c similarity index 98% rename from libc/intrin/pthread_attr_getinheritsched.c rename to libc/thread/pthread_attr_getinheritsched.c index 1ec8dfaa4..f8b07caa1 100644 --- a/libc/intrin/pthread_attr_getinheritsched.c +++ b/libc/thread/pthread_attr_getinheritsched.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Returns thread inherit schedule attribute. diff --git a/libc/intrin/pthread_attr_getschedparam.c b/libc/thread/pthread_attr_getschedparam.c similarity index 98% rename from libc/intrin/pthread_attr_getschedparam.c rename to libc/thread/pthread_attr_getschedparam.c index b6170a6da..25e8634b6 100644 --- a/libc/intrin/pthread_attr_getschedparam.c +++ b/libc/thread/pthread_attr_getschedparam.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread2.h" /** * Gets thread scheduler parameter attribute. diff --git a/libc/intrin/pthread_attr_getschedpolicy.c b/libc/thread/pthread_attr_getschedpolicy.c similarity index 98% rename from libc/intrin/pthread_attr_getschedpolicy.c rename to libc/thread/pthread_attr_getschedpolicy.c index a19325f93..88f8e277a 100644 --- a/libc/intrin/pthread_attr_getschedpolicy.c +++ b/libc/thread/pthread_attr_getschedpolicy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets thread scheduler policy attribute diff --git a/libc/intrin/pthread_attr_getscope.c b/libc/thread/pthread_attr_getscope.c similarity index 98% rename from libc/intrin/pthread_attr_getscope.c rename to libc/thread/pthread_attr_getscope.c index fa24f5432..3759ae815 100644 --- a/libc/intrin/pthread_attr_getscope.c +++ b/libc/thread/pthread_attr_getscope.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" int pthread_attr_getscope(const pthread_attr_t *a, int *x) { *x = a->scope; diff --git a/libc/intrin/pthread_attr_getstack.c b/libc/thread/pthread_attr_getstack.c similarity index 98% rename from libc/intrin/pthread_attr_getstack.c rename to libc/thread/pthread_attr_getstack.c index bed6b5c46..c569c7d65 100644 --- a/libc/intrin/pthread_attr_getstack.c +++ b/libc/thread/pthread_attr_getstack.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/runtime/stack.h" /** diff --git a/libc/intrin/pthread_attr_getstacksize.c b/libc/thread/pthread_attr_getstacksize.c similarity index 98% rename from libc/intrin/pthread_attr_getstacksize.c rename to libc/thread/pthread_attr_getstacksize.c index d4c5489b1..746ed4f7a 100644 --- a/libc/intrin/pthread_attr_getstacksize.c +++ b/libc/thread/pthread_attr_getstacksize.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/runtime/stack.h" /** diff --git a/libc/intrin/pthread_attr_init.c b/libc/thread/pthread_attr_init.c similarity index 98% rename from libc/intrin/pthread_attr_init.c rename to libc/thread/pthread_attr_init.c index ac6f31166..b12dfea81 100644 --- a/libc/intrin/pthread_attr_init.c +++ b/libc/thread/pthread_attr_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/runtime/stack.h" /** diff --git a/libc/intrin/pthread_attr_setdetachstate.c b/libc/thread/pthread_attr_setdetachstate.c similarity index 98% rename from libc/intrin/pthread_attr_setdetachstate.c rename to libc/thread/pthread_attr_setdetachstate.c index 5d37579a5..22e03d62c 100644 --- a/libc/intrin/pthread_attr_setdetachstate.c +++ b/libc/thread/pthread_attr_setdetachstate.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets thread detachable attribute, e.g. diff --git a/libc/intrin/pthread_attr_setguardsize.c b/libc/thread/pthread_attr_setguardsize.c similarity index 98% rename from libc/intrin/pthread_attr_setguardsize.c rename to libc/thread/pthread_attr_setguardsize.c index 27f5077e5..40b6a77b8 100644 --- a/libc/intrin/pthread_attr_setguardsize.c +++ b/libc/thread/pthread_attr_setguardsize.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets size of unmapped pages at bottom of stack. diff --git a/libc/intrin/pthread_attr_setinheritsched.c b/libc/thread/pthread_attr_setinheritsched.c similarity index 98% rename from libc/intrin/pthread_attr_setinheritsched.c rename to libc/thread/pthread_attr_setinheritsched.c index 74d6fc352..eb2737720 100644 --- a/libc/intrin/pthread_attr_setinheritsched.c +++ b/libc/thread/pthread_attr_setinheritsched.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets thread scheduler inheritance attribute, e.g. diff --git a/libc/intrin/pthread_attr_setschedparam.c b/libc/thread/pthread_attr_setschedparam.c similarity index 98% rename from libc/intrin/pthread_attr_setschedparam.c rename to libc/thread/pthread_attr_setschedparam.c index 70a348d58..afcf4e381 100644 --- a/libc/intrin/pthread_attr_setschedparam.c +++ b/libc/thread/pthread_attr_setschedparam.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/struct/sched_param.h" #include "libc/errno.h" -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread2.h" /** * Sets thread scheduler parameter attribute, e.g. diff --git a/libc/intrin/pthread_attr_setscope.c b/libc/thread/pthread_attr_setscope.c similarity index 98% rename from libc/intrin/pthread_attr_setscope.c rename to libc/thread/pthread_attr_setscope.c index 365a40120..89ad4eaa8 100644 --- a/libc/intrin/pthread_attr_setscope.c +++ b/libc/thread/pthread_attr_setscope.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" int pthread_attr_setscope(pthread_attr_t *a, int x) { a->scope = x; diff --git a/libc/intrin/pthread_attr_setstack.c b/libc/thread/pthread_attr_setstack.c similarity index 99% rename from libc/intrin/pthread_attr_setstack.c rename to libc/thread/pthread_attr_setstack.c index 6994b97d8..93aec84a0 100644 --- a/libc/intrin/pthread_attr_setstack.c +++ b/libc/thread/pthread_attr_setstack.c @@ -19,7 +19,7 @@ #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Configures custom allocated stack for thread, e.g. diff --git a/libc/intrin/pthread_attr_setstacksize.c b/libc/thread/pthread_attr_setstacksize.c similarity index 98% rename from libc/intrin/pthread_attr_setstacksize.c rename to libc/thread/pthread_attr_setstacksize.c index 893f68714..353d708ab 100644 --- a/libc/intrin/pthread_attr_setstacksize.c +++ b/libc/thread/pthread_attr_setstacksize.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Defines minimum stack size for thread. diff --git a/libc/intrin/pthread_barrier_destroy.c b/libc/thread/pthread_barrier_destroy.c similarity index 98% rename from libc/intrin/pthread_barrier_destroy.c rename to libc/thread/pthread_barrier_destroy.c index 9dca7a62a..bad0f54d2 100644 --- a/libc/intrin/pthread_barrier_destroy.c +++ b/libc/thread/pthread_barrier_destroy.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_barrier_init.c b/libc/thread/pthread_barrier_init.c similarity index 94% rename from libc/intrin/pthread_barrier_init.c rename to libc/thread/pthread_barrier_init.c index f6ea185b8..c5030e60f 100644 --- a/libc/intrin/pthread_barrier_init.c +++ b/libc/thread/pthread_barrier_init.c @@ -18,8 +18,8 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" #include "libc/limits.h" +#include "libc/thread/thread.h" /** * Initializes barrier. @@ -33,10 +33,7 @@ int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count) { if (count && count < INT_MAX / 2) { - *barrier = (pthread_barrier_t){ - attr ? *attr : PTHREAD_PROCESS_DEFAULT, - count, - }; + *barrier = (pthread_barrier_t){attr ? *attr : 0, count}; return 0; } else { assert(!"bad count"); diff --git a/libc/intrin/pthread_barrier_wait.c b/libc/thread/pthread_barrier_wait.c similarity index 98% rename from libc/intrin/pthread_barrier_wait.c rename to libc/thread/pthread_barrier_wait.c index d1931b4e9..328c20a98 100644 --- a/libc/intrin/pthread_barrier_wait.c +++ b/libc/thread/pthread_barrier_wait.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/limits.h" /** diff --git a/libc/intrin/pthread_barrierattr_destroy.c b/libc/thread/pthread_barrierattr_destroy.c similarity index 98% rename from libc/intrin/pthread_barrierattr_destroy.c rename to libc/thread/pthread_barrierattr_destroy.c index 9a51e9f7f..3777f2b3f 100644 --- a/libc/intrin/pthread_barrierattr_destroy.c +++ b/libc/thread/pthread_barrierattr_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_barrierattr_getpshared.c b/libc/thread/pthread_barrierattr_getpshared.c similarity index 98% rename from libc/intrin/pthread_barrierattr_getpshared.c rename to libc/thread/pthread_barrierattr_getpshared.c index 63e9228a8..aeae63780 100644 --- a/libc/intrin/pthread_barrierattr_getpshared.c +++ b/libc/thread/pthread_barrierattr_getpshared.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets barrier process sharing. diff --git a/libc/intrin/pthread_barrierattr_init.c b/libc/thread/pthread_barrierattr_init.c similarity index 96% rename from libc/intrin/pthread_barrierattr_init.c rename to libc/thread/pthread_barrierattr_init.c index 9a79d1460..b009911e2 100644 --- a/libc/intrin/pthread_barrierattr_init.c +++ b/libc/thread/pthread_barrierattr_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes barrier attributes. @@ -24,6 +24,6 @@ * @return 0 on success, or error on failure */ int pthread_barrierattr_init(pthread_barrierattr_t *attr) { - *attr = PTHREAD_PROCESS_DEFAULT; + *attr = 0; return 0; } diff --git a/libc/intrin/pthread_barrierattr_setpshared.c b/libc/thread/pthread_barrierattr_setpshared.c similarity index 98% rename from libc/intrin/pthread_barrierattr_setpshared.c rename to libc/thread/pthread_barrierattr_setpshared.c index d10ee6aac..64979b4d9 100644 --- a/libc/intrin/pthread_barrierattr_setpshared.c +++ b/libc/thread/pthread_barrierattr_setpshared.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets barrier process sharing. diff --git a/libc/intrin/pthread.c b/libc/thread/pthread_cancel.c similarity index 92% rename from libc/intrin/pthread.c rename to libc/thread/pthread_cancel.c index 0eed78739..876589fe1 100644 --- a/libc/intrin/pthread.c +++ b/libc/thread/pthread_cancel.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2021 Justine Alexandra Roberts Tunney │ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -17,12 +17,8 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" int pthread_cancel(pthread_t thread) { return ESRCH; } - -void *__tls_get_addr(size_t v[2]) { - return NULL; -} diff --git a/libc/intrin/pthread_cond_broadcast.c b/libc/thread/pthread_cond_broadcast.c similarity index 99% rename from libc/intrin/pthread_cond_broadcast.c rename to libc/thread/pthread_cond_broadcast.c index 5c1cb8264..6b9779c55 100644 --- a/libc/intrin/pthread_cond_broadcast.c +++ b/libc/thread/pthread_cond_broadcast.c @@ -19,7 +19,7 @@ #include "libc/dce.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/limits.h" static dontinline int pthread_cond_signal_impl(pthread_cond_t *cond, int n) { diff --git a/libc/intrin/pthread_cond_destroy.c b/libc/thread/pthread_cond_destroy.c similarity index 98% rename from libc/intrin/pthread_cond_destroy.c rename to libc/thread/pthread_cond_destroy.c index 0fc53ba88..8dfe435bb 100644 --- a/libc/intrin/pthread_cond_destroy.c +++ b/libc/thread/pthread_cond_destroy.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_cond_init.c b/libc/thread/pthread_cond_init.c similarity index 95% rename from libc/intrin/pthread_cond_init.c rename to libc/thread/pthread_cond_init.c index f683b6350..0bbf67cf7 100644 --- a/libc/intrin/pthread_cond_init.c +++ b/libc/thread/pthread_cond_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes condition. @@ -25,6 +25,6 @@ * @return 0 on success, or error number on failure */ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { - *cond = (pthread_cond_t){attr ? *attr : PTHREAD_PROCESS_DEFAULT}; + *cond = (pthread_cond_t){attr ? *attr : 0}; return 0; } diff --git a/libc/thread/pthread_cond_timedwait.c b/libc/thread/pthread_cond_timedwait.c index 1245fb77c..475c30fff 100644 --- a/libc/thread/pthread_cond_timedwait.c +++ b/libc/thread/pthread_cond_timedwait.c @@ -22,8 +22,8 @@ #include "libc/errno.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread.h" +#include "libc/thread/thread2.h" /** * Waits for condition with optional time limit, e.g. diff --git a/libc/thread/pthread_cond_wait.c b/libc/thread/pthread_cond_wait.c index 1f48fba32..9955868bd 100644 --- a/libc/thread/pthread_cond_wait.c +++ b/libc/thread/pthread_cond_wait.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread2.h" /** * Waits for condition, e.g. diff --git a/libc/intrin/pthread_condattr_destroy.c b/libc/thread/pthread_condattr_destroy.c similarity index 98% rename from libc/intrin/pthread_condattr_destroy.c rename to libc/thread/pthread_condattr_destroy.c index 7ea243c59..abf437dd5 100644 --- a/libc/intrin/pthread_condattr_destroy.c +++ b/libc/thread/pthread_condattr_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_condattr_getpshared.c b/libc/thread/pthread_condattr_getpshared.c similarity index 98% rename from libc/intrin/pthread_condattr_getpshared.c rename to libc/thread/pthread_condattr_getpshared.c index b340781ff..77903194f 100644 --- a/libc/intrin/pthread_condattr_getpshared.c +++ b/libc/thread/pthread_condattr_getpshared.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets condition process sharing. diff --git a/libc/intrin/pthread_condattr_init.c b/libc/thread/pthread_condattr_init.c similarity index 96% rename from libc/intrin/pthread_condattr_init.c rename to libc/thread/pthread_condattr_init.c index 40a35f179..7e61998fd 100644 --- a/libc/intrin/pthread_condattr_init.c +++ b/libc/thread/pthread_condattr_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes condition attributes. @@ -24,6 +24,6 @@ * @return 0 on success, or error on failure */ int pthread_condattr_init(pthread_condattr_t *attr) { - *attr = PTHREAD_PROCESS_DEFAULT; + *attr = 0; return 0; } diff --git a/libc/intrin/pthread_condattr_setpshared.c b/libc/thread/pthread_condattr_setpshared.c similarity index 98% rename from libc/intrin/pthread_condattr_setpshared.c rename to libc/thread/pthread_condattr_setpshared.c index 9c8929a27..9ec958f8e 100644 --- a/libc/intrin/pthread_condattr_setpshared.c +++ b/libc/thread/pthread_condattr_setpshared.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets condition process sharing. diff --git a/libc/thread/pthread_create.c b/libc/thread/pthread_create.c index a08471e7e..6f371e8b6 100644 --- a/libc/thread/pthread_create.c +++ b/libc/thread/pthread_create.c @@ -25,14 +25,11 @@ #include "libc/intrin/asan.internal.h" #include "libc/intrin/atomic.h" #include "libc/intrin/bits.h" -#include "libc/intrin/pthread.h" #include "libc/intrin/wait0.internal.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/sysv/consts/clone.h" @@ -43,6 +40,7 @@ #include "libc/thread/posixthread.internal.h" #include "libc/thread/spawn.h" #include "libc/thread/thread.h" +#include "libc/thread/tls.h" #define MAP_ANON_OPENBSD 0x1000 #define MAP_STACK_OPENBSD 0x4000 @@ -68,13 +66,13 @@ static int PosixThread(void *arg, int tid) { _pthread_reschedule(pt); } if (!setjmp(pt->exiter)) { - ((cthread_t)__get_tls())->pthread = (pthread_t)pt; + __get_tls()->tib_pthread = (pthread_t)pt; pt->rc = pt->start_routine(pt->arg); } if (weaken(_pthread_key_destruct)) { weaken(_pthread_key_destruct)(0); } - cthread_ungarbage(); + _pthread_ungarbage(); if (atomic_load_explicit(&pt->status, memory_order_acquire) == kPosixThreadDetached) { atomic_store_explicit(&pt->status, kPosixThreadZombie, @@ -169,7 +167,7 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { int rc, e = errno; struct PosixThread *pt; - TlsIsRequired(); + __require_tls(); _pthread_zombies_decimate(); // create posix thread object diff --git a/libc/thread/pthread_detach.c b/libc/thread/pthread_detach.c index c3aa460dd..9fbd94bce 100644 --- a/libc/thread/pthread_detach.c +++ b/libc/thread/pthread_detach.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/thread/posixthread.internal.h" diff --git a/libc/thread/pthread_equal.c b/libc/thread/pthread_equal.c index c7e3afcce..6941eb9d3 100644 --- a/libc/thread/pthread_equal.c +++ b/libc/thread/pthread_equal.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/thread/posixthread.internal.h" /** diff --git a/libc/thread/pthread_exit.c b/libc/thread/pthread_exit.c index 842dc7e7f..de4d06fd0 100644 --- a/libc/thread/pthread_exit.c +++ b/libc/thread/pthread_exit.c @@ -16,12 +16,11 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" -#include "libc/nexgen32e/gettls.h" #include "libc/runtime/gc.h" #include "libc/thread/posixthread.internal.h" #include "libc/thread/spawn.h" #include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * Terminates current POSIX thread. @@ -37,7 +36,7 @@ */ wontreturn void pthread_exit(void *rc) { struct PosixThread *pt; - if ((pt = (struct PosixThread *)((cthread_t)__get_tls())->pthread)) { + if ((pt = (struct PosixThread *)__get_tls()->tib_pthread)) { pt->rc = rc; _gclongjmp(pt->exiter, 1); } else { diff --git a/libc/thread/pthread_getaffinity_np.c b/libc/thread/pthread_getaffinity_np.c index 35064bbf5..0133fe20c 100644 --- a/libc/thread/pthread_getaffinity_np.c +++ b/libc/thread/pthread_getaffinity_np.c @@ -19,7 +19,6 @@ #include "libc/calls/struct/cpuset.h" #include "libc/errno.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" /** * Gets CPU affinity for thread. diff --git a/libc/thread/pthread_getattr_np.c b/libc/thread/pthread_getattr_np.c index 878d7ecd9..479f4371c 100644 --- a/libc/thread/pthread_getattr_np.c +++ b/libc/thread/pthread_getattr_np.c @@ -16,7 +16,6 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" #include "libc/str/str.h" #include "libc/thread/posixthread.internal.h" #include "libc/thread/thread.h" diff --git a/libc/thread/pthread_getname_np.c b/libc/thread/pthread_getname_np.c index a306fcd79..413cb4c0a 100644 --- a/libc/thread/pthread_getname_np.c +++ b/libc/thread/pthread_getname_np.c @@ -27,7 +27,6 @@ #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/pr.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" /** * Gets name of thread registered with system, e.g. diff --git a/libc/thread/pthread_getschedparam.c b/libc/thread/pthread_getschedparam.c index b98ef8873..f382a59dc 100644 --- a/libc/thread/pthread_getschedparam.c +++ b/libc/thread/pthread_getschedparam.c @@ -16,8 +16,8 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread2.h" #include "libc/thread/posixthread.internal.h" +#include "libc/thread/thread2.h" /** * Gets most recently set scheduling of thread. diff --git a/libc/thread/pthread_getspecific.c b/libc/thread/pthread_getspecific.c index 0aa0771af..7b49397ec 100644 --- a/libc/thread/pthread_getspecific.c +++ b/libc/thread/pthread_getspecific.c @@ -17,9 +17,8 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/nexgen32e/gettls.h" #include "libc/thread/internal.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * Gets value of TLS slot for current thread. diff --git a/libc/thread/pthread_getthreadid_np.c b/libc/thread/pthread_getthreadid_np.c index e3ceefcdc..9ba68cb87 100644 --- a/libc/thread/pthread_getthreadid_np.c +++ b/libc/thread/pthread_getthreadid_np.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Returns thread id of current POSIX thread. diff --git a/libc/thread/pthread_getunique_np.c b/libc/thread/pthread_getunique_np.c index 8e8b58637..d3bf75032 100644 --- a/libc/thread/pthread_getunique_np.c +++ b/libc/thread/pthread_getunique_np.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" /** * Returns thread id of POSIX thread. diff --git a/libc/thread/pthread_join.c b/libc/thread/pthread_join.c index 8ae82d909..ea9535bfd 100644 --- a/libc/thread/pthread_join.c +++ b/libc/thread/pthread_join.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/thread/posixthread.internal.h" diff --git a/libc/thread/pthread_key_create.c b/libc/thread/pthread_key_create.c index 6d0eb1a45..aaa9fb0ba 100644 --- a/libc/thread/pthread_key_create.c +++ b/libc/thread/pthread_key_create.c @@ -18,10 +18,9 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "libc/nexgen32e/bsr.h" -#include "libc/nexgen32e/gettls.h" #include "libc/runtime/runtime.h" #include "libc/thread/internal.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * Allocates TLS slot. diff --git a/libc/thread/pthread_key_delete.c b/libc/thread/pthread_key_delete.c index 998e78ac0..beb5bf32e 100644 --- a/libc/thread/pthread_key_delete.c +++ b/libc/thread/pthread_key_delete.c @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" #include "libc/thread/internal.h" -#include "libc/thread/thread.h" /** * Deletes TLS slot. diff --git a/libc/thread/pthread_key_destruct.c b/libc/thread/pthread_key_destruct.c index f9eb6cd0b..3414957a3 100644 --- a/libc/thread/pthread_key_destruct.c +++ b/libc/thread/pthread_key_destruct.c @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/nexgen32e/bsr.h" #include "libc/thread/internal.h" -#include "libc/thread/thread.h" void _pthread_key_destruct(void *key[PTHREAD_KEYS_MAX]) { int i, j; diff --git a/libc/intrin/pthread_mutex_destroy.c b/libc/thread/pthread_mutex_destroy.c similarity index 98% rename from libc/intrin/pthread_mutex_destroy.c rename to libc/thread/pthread_mutex_destroy.c index 5a5312910..5350f0e8f 100644 --- a/libc/intrin/pthread_mutex_destroy.c +++ b/libc/thread/pthread_mutex_destroy.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_mutexattr_destroy.c b/libc/thread/pthread_mutexattr_destroy.c similarity index 98% rename from libc/intrin/pthread_mutexattr_destroy.c rename to libc/thread/pthread_mutexattr_destroy.c index a6fc17e3e..49df496c1 100644 --- a/libc/intrin/pthread_mutexattr_destroy.c +++ b/libc/thread/pthread_mutexattr_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_mutexattr_getpshared.c b/libc/thread/pthread_mutexattr_getpshared.c similarity index 98% rename from libc/intrin/pthread_mutexattr_getpshared.c rename to libc/thread/pthread_mutexattr_getpshared.c index 8d76df79b..39560cd2e 100644 --- a/libc/intrin/pthread_mutexattr_getpshared.c +++ b/libc/thread/pthread_mutexattr_getpshared.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets mutex process sharing. diff --git a/libc/intrin/pthread_mutexattr_gettype.c b/libc/thread/pthread_mutexattr_gettype.c similarity index 98% rename from libc/intrin/pthread_mutexattr_gettype.c rename to libc/thread/pthread_mutexattr_gettype.c index 2c7ef3e0a..ecdb6fb11 100644 --- a/libc/intrin/pthread_mutexattr_gettype.c +++ b/libc/thread/pthread_mutexattr_gettype.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets mutex type. diff --git a/libc/intrin/pthread_mutexattr_init.c b/libc/thread/pthread_mutexattr_init.c similarity index 93% rename from libc/intrin/pthread_mutexattr_init.c rename to libc/thread/pthread_mutexattr_init.c index 5bb326829..a12bec7d8 100644 --- a/libc/intrin/pthread_mutexattr_init.c +++ b/libc/thread/pthread_mutexattr_init.c @@ -16,16 +16,13 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes mutex attr. * @return 0 on success, or error number on failure */ int pthread_mutexattr_init(pthread_mutexattr_t *attr) { - *attr = (pthread_mutexattr_t){ - PTHREAD_MUTEX_DEFAULT, - PTHREAD_PROCESS_DEFAULT, - }; + *attr = (pthread_mutexattr_t){0}; return 0; } diff --git a/libc/intrin/pthread_mutexattr_setpshared.c b/libc/thread/pthread_mutexattr_setpshared.c similarity index 98% rename from libc/intrin/pthread_mutexattr_setpshared.c rename to libc/thread/pthread_mutexattr_setpshared.c index 8152edce2..9c600b77e 100644 --- a/libc/intrin/pthread_mutexattr_setpshared.c +++ b/libc/thread/pthread_mutexattr_setpshared.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets mutex process sharing. diff --git a/libc/intrin/pthread_mutexattr_settype.c b/libc/thread/pthread_mutexattr_settype.c similarity index 98% rename from libc/intrin/pthread_mutexattr_settype.c rename to libc/thread/pthread_mutexattr_settype.c index 546459156..9d70a4d02 100644 --- a/libc/intrin/pthread_mutexattr_settype.c +++ b/libc/thread/pthread_mutexattr_settype.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets mutex type. diff --git a/libc/thread/pthread_reschedule.c b/libc/thread/pthread_reschedule.c index 2b7cc3664..c3ad753aa 100644 --- a/libc/thread/pthread_reschedule.c +++ b/libc/thread/pthread_reschedule.c @@ -22,7 +22,6 @@ #include "libc/errno.h" #include "libc/sysv/errfuns.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" int _pthread_reschedule(struct PosixThread *pt) { int rc, e = errno; diff --git a/libc/intrin/pthread_rwlock_destroy.c b/libc/thread/pthread_rwlock_destroy.c similarity index 98% rename from libc/intrin/pthread_rwlock_destroy.c rename to libc/thread/pthread_rwlock_destroy.c index eb0b3c906..649b98f30 100644 --- a/libc/intrin/pthread_rwlock_destroy.c +++ b/libc/thread/pthread_rwlock_destroy.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_rwlock_init.c b/libc/thread/pthread_rwlock_init.c similarity index 95% rename from libc/intrin/pthread_rwlock_init.c rename to libc/thread/pthread_rwlock_init.c index 392dde1c0..ac10d5651 100644 --- a/libc/intrin/pthread_rwlock_init.c +++ b/libc/thread/pthread_rwlock_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes read-write lock. @@ -26,6 +26,6 @@ */ int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { - *rwlock = (pthread_rwlock_t){attr ? *attr : PTHREAD_PROCESS_DEFAULT}; + *rwlock = (pthread_rwlock_t){attr ? *attr : 0}; return 0; } diff --git a/libc/intrin/pthread_rwlock_rdlock.c b/libc/thread/pthread_rwlock_rdlock.c similarity index 98% rename from libc/intrin/pthread_rwlock_rdlock.c rename to libc/thread/pthread_rwlock_rdlock.c index e0502ab7e..3bbe79e26 100644 --- a/libc/intrin/pthread_rwlock_rdlock.c +++ b/libc/thread/pthread_rwlock_rdlock.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" static int pthread_rwlock_rdlock_spin(pthread_rwlock_t *rwlock, int expect, int tries) { diff --git a/libc/intrin/pthread_rwlock_unlock.c b/libc/thread/pthread_rwlock_unlock.c similarity index 98% rename from libc/intrin/pthread_rwlock_unlock.c rename to libc/thread/pthread_rwlock_unlock.c index c90f9ecbd..849a58fef 100644 --- a/libc/intrin/pthread_rwlock_unlock.c +++ b/libc/thread/pthread_rwlock_unlock.c @@ -21,7 +21,7 @@ #include "libc/errno.h" #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Unlocks read-write lock. diff --git a/libc/intrin/pthread_rwlock_wrlock.c b/libc/thread/pthread_rwlock_wrlock.c similarity index 98% rename from libc/intrin/pthread_rwlock_wrlock.c rename to libc/thread/pthread_rwlock_wrlock.c index f50ebc966..d66fb25ce 100644 --- a/libc/intrin/pthread_rwlock_wrlock.c +++ b/libc/thread/pthread_rwlock_wrlock.c @@ -18,7 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" static int pthread_rwlock_wrlock_spin(pthread_rwlock_t *rwlock, int expect, int tries) { diff --git a/libc/intrin/pthread_rwlockattr_destroy.c b/libc/thread/pthread_rwlockattr_destroy.c similarity index 98% rename from libc/intrin/pthread_rwlockattr_destroy.c rename to libc/thread/pthread_rwlockattr_destroy.c index b190001ce..624b5fb5d 100644 --- a/libc/intrin/pthread_rwlockattr_destroy.c +++ b/libc/thread/pthread_rwlockattr_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/str/str.h" /** diff --git a/libc/intrin/pthread_rwlockattr_getpshared.c b/libc/thread/pthread_rwlockattr_getpshared.c similarity index 98% rename from libc/intrin/pthread_rwlockattr_getpshared.c rename to libc/thread/pthread_rwlockattr_getpshared.c index aafde2afe..e087a71d0 100644 --- a/libc/intrin/pthread_rwlockattr_getpshared.c +++ b/libc/thread/pthread_rwlockattr_getpshared.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Gets read-write lock process sharing. diff --git a/libc/intrin/pthread_rwlockattr_init.c b/libc/thread/pthread_rwlockattr_init.c similarity index 96% rename from libc/intrin/pthread_rwlockattr_init.c rename to libc/thread/pthread_rwlockattr_init.c index f64ded572..dee6fb305 100644 --- a/libc/intrin/pthread_rwlockattr_init.c +++ b/libc/thread/pthread_rwlockattr_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes read-write lock attributes. @@ -24,6 +24,6 @@ * @return 0 on success, or error on failure */ int pthread_rwlockattr_init(pthread_rwlockattr_t *attr) { - *attr = PTHREAD_PROCESS_DEFAULT; + *attr = 0; return 0; } diff --git a/libc/intrin/pthread_rwlockattr_setpshared.c b/libc/thread/pthread_rwlockattr_setpshared.c similarity index 98% rename from libc/intrin/pthread_rwlockattr_setpshared.c rename to libc/thread/pthread_rwlockattr_setpshared.c index 2139bfd72..a40ae0f94 100644 --- a/libc/intrin/pthread_rwlockattr_setpshared.c +++ b/libc/thread/pthread_rwlockattr_setpshared.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Sets read-write lock process sharing. diff --git a/libc/thread/pthread_self.c b/libc/thread/pthread_self.c index 0212ecb83..358679b61 100644 --- a/libc/thread/pthread_self.c +++ b/libc/thread/pthread_self.c @@ -16,13 +16,12 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" -#include "libc/nexgen32e/gettls.h" #include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * Returns current POSIX thread. */ pthread_t pthread_self(void) { - return ((cthread_t)__get_tls())->pthread; + return __get_tls()->tib_pthread; } diff --git a/libc/thread/pthread_setaffinity_np.c b/libc/thread/pthread_setaffinity_np.c index 28a67ceab..9605d056e 100644 --- a/libc/thread/pthread_setaffinity_np.c +++ b/libc/thread/pthread_setaffinity_np.c @@ -19,7 +19,6 @@ #include "libc/calls/struct/cpuset.h" #include "libc/errno.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" /** * Asks kernel to only schedule thread on particular CPUs. diff --git a/libc/thread/pthread_setname_np.c b/libc/thread/pthread_setname_np.c index 0663b05db..5452299ae 100644 --- a/libc/thread/pthread_setname_np.c +++ b/libc/thread/pthread_setname_np.c @@ -27,7 +27,6 @@ #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/pr.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" /** * Registers custom name of thread with system, e.g. diff --git a/libc/thread/pthread_setschedparam.c b/libc/thread/pthread_setschedparam.c index 4d44b2093..e0d2fce57 100644 --- a/libc/thread/pthread_setschedparam.c +++ b/libc/thread/pthread_setschedparam.c @@ -17,8 +17,8 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/intrin/pthread2.h" #include "libc/thread/posixthread.internal.h" +#include "libc/thread/thread2.h" /** * Changes scheduling of thread, e.g. diff --git a/libc/thread/pthread_setschedparam_freebsd.c b/libc/thread/pthread_setschedparam_freebsd.c index 2a54cceb4..4dfb10fe8 100644 --- a/libc/thread/pthread_setschedparam_freebsd.c +++ b/libc/thread/pthread_setschedparam_freebsd.c @@ -20,7 +20,6 @@ #include "libc/sysv/consts/sched.h" #include "libc/thread/freebsd.internal.h" #include "libc/thread/posixthread.internal.h" -#include "libc/thread/thread.h" #define RTP_SET_FREEBSD 1 #define PRI_REALTIME_FREEBSD 2 diff --git a/libc/thread/pthread_setspecific.c b/libc/thread/pthread_setspecific.c index 8d5b699d8..52cd8f32a 100644 --- a/libc/thread/pthread_setspecific.c +++ b/libc/thread/pthread_setspecific.c @@ -17,9 +17,8 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/errno.h" -#include "libc/nexgen32e/gettls.h" #include "libc/thread/internal.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * Sets value of TLS slot for current thread. diff --git a/libc/thread/pthread_sigmask.c b/libc/thread/pthread_sigmask.c index 20d87e802..8c0340e65 100644 --- a/libc/thread/pthread_sigmask.c +++ b/libc/thread/pthread_sigmask.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/struct/sigset.h" -#include "libc/thread/thread.h" /** * Examines and/or changes blocked signals on current thread. diff --git a/libc/intrin/pthread_spin_destroy.c b/libc/thread/pthread_spin_destroy.c similarity index 98% rename from libc/intrin/pthread_spin_destroy.c rename to libc/thread/pthread_spin_destroy.c index 309071433..18fa1bfeb 100644 --- a/libc/intrin/pthread_spin_destroy.c +++ b/libc/thread/pthread_spin_destroy.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Destroys spin lock. diff --git a/libc/intrin/pthread_spin_init.c b/libc/thread/pthread_spin_init.c similarity index 98% rename from libc/intrin/pthread_spin_init.c rename to libc/thread/pthread_spin_init.c index 753d4e956..d8ca27cc1 100644 --- a/libc/intrin/pthread_spin_init.c +++ b/libc/thread/pthread_spin_init.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Initializes spin lock. diff --git a/libc/intrin/pthread_spin_lock.c b/libc/thread/pthread_spin_lock.c similarity index 98% rename from libc/intrin/pthread_spin_lock.c rename to libc/thread/pthread_spin_lock.c index dca24fb7b..36ced2007 100644 --- a/libc/intrin/pthread_spin_lock.c +++ b/libc/thread/pthread_spin_lock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Acquires spin lock. diff --git a/libc/intrin/pthread_spin_trylock.c b/libc/thread/pthread_spin_trylock.c similarity index 98% rename from libc/intrin/pthread_spin_trylock.c rename to libc/thread/pthread_spin_trylock.c index 7936b2e8b..36bcb55c9 100644 --- a/libc/intrin/pthread_spin_trylock.c +++ b/libc/thread/pthread_spin_trylock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Acquires spin lock if available. diff --git a/libc/intrin/pthread_spin_unlock.c b/libc/thread/pthread_spin_unlock.c similarity index 98% rename from libc/intrin/pthread_spin_unlock.c rename to libc/thread/pthread_spin_unlock.c index ba3318c6f..54fafbd18 100644 --- a/libc/intrin/pthread_spin_unlock.c +++ b/libc/thread/pthread_spin_unlock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" /** * Releases spin lock. diff --git a/libc/thread/zombie.c b/libc/thread/pthread_zombies.c similarity index 98% rename from libc/thread/zombie.c rename to libc/thread/pthread_zombies.c index b93f9d67e..a8d64a130 100644 --- a/libc/thread/zombie.c +++ b/libc/thread/pthread_zombies.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/thread/posixthread.internal.h" diff --git a/libc/thread/spawn.c b/libc/thread/spawn.c index e58f76a59..cd54d7630 100644 --- a/libc/thread/spawn.c +++ b/libc/thread/spawn.c @@ -21,7 +21,6 @@ #include "libc/intrin/wait0.internal.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" @@ -31,8 +30,9 @@ #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" #include "libc/sysv/errfuns.h" +#include "libc/thread/posixthread.internal.h" #include "libc/thread/spawn.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" /** * @fileoverview Simple threading API @@ -52,8 +52,8 @@ #define _TLSZ ((intptr_t)_tls_size) #define _TLDZ ((intptr_t)_tdata_size) -#define _TIBZ sizeof(struct cthread_descriptor_t) -#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, alignof(struct cthread_descriptor_t)) +#define _TIBZ sizeof(struct CosmoTib) +#define _MEMZ ROUNDUP(_TLSZ + _TIBZ, alignof(struct CosmoTib)) struct spawner { int (*fun)(void *, int); @@ -64,7 +64,7 @@ static int Spawner(void *arg, int tid) { int rc; struct spawner *spawner = arg; rc = spawner->fun(spawner->arg, tid); - cthread_ungarbage(); + _pthread_ungarbage(); free(spawner); return 0; } @@ -94,7 +94,7 @@ static int Spawner(void *arg, int tid) { int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) { struct spawn *th, ths; struct spawner *spawner; - TlsIsRequired(); + __require_tls(); if (!fun) return einval(); // we need to to clobber the output memory before calling clone, since diff --git a/libc/thread/thread.h b/libc/thread/thread.h index 1d5b5cc0f..ddb4e35a9 100644 --- a/libc/thread/thread.h +++ b/libc/thread/thread.h @@ -1,32 +1,202 @@ #ifndef COSMOPOLITAN_LIBC_THREAD_THREAD_H_ #define COSMOPOLITAN_LIBC_THREAD_THREAD_H_ -#include "libc/intrin/pthread.h" -#include "libc/runtime/runtime.h" -#include "libc/str/locale.h" + +#define PTHREAD_KEYS_MAX 64 +#define PTHREAD_STACK_MIN FRAMESIZE +#define PTHREAD_DESTRUCTOR_ITERATIONS 4 + +#define PTHREAD_BARRIER_SERIAL_THREAD 31337 + +#define PTHREAD_MUTEX_DEFAULT 0 +#define PTHREAD_MUTEX_NORMAL 0 +#define PTHREAD_MUTEX_RECURSIVE 1 +#define PTHREAD_MUTEX_ERRORCHECK 2 +#define PTHREAD_MUTEX_STALLED 0 +#define PTHREAD_MUTEX_ROBUST 1 + +#define PTHREAD_PROCESS_PRIVATE 0 +#define PTHREAD_PROCESS_SHARED 1 + +#define PTHREAD_CREATE_JOINABLE 0 +#define PTHREAD_CREATE_DETACHED 1 + +#define PTHREAD_INHERIT_SCHED 0 +#define PTHREAD_EXPLICIT_SCHED 1 + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -struct Ftrace { /* 16 */ - bool once; /* 0 */ - bool noreentry; /* 1 */ - int skew; /* 4 */ - int64_t lastaddr; /* 8 */ -}; +#define PTHREAD_ONCE_INIT _PTHREAD_INIT +#define PTHREAD_COND_INITIALIZER _PTHREAD_INIT +#define PTHREAD_BARRIER_INITIALIZER _PTHREAD_INIT +#define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_INIT +#define PTHREAD_MUTEX_INITIALIZER _PTHREAD_INIT +#define _PTHREAD_INIT \ + { 0 } -struct cthread_descriptor_t { - struct cthread_descriptor_t *self; /* 0x00 */ - struct Ftrace ftrace; /* 0x08 */ - void *garbages; /* 0x18 */ - locale_t locale; /* 0x20 */ - pthread_t pthread; /* 0x28 */ - struct cthread_descriptor_t *self2; /* 0x30 */ - int32_t tid; /* 0x38 */ - int32_t err; /* 0x3c */ -}; +typedef uintptr_t pthread_t; +typedef int pthread_id_np_t; +typedef char pthread_condattr_t; +typedef char pthread_rwlockattr_t; +typedef char pthread_barrierattr_t; +typedef unsigned pthread_key_t; +typedef void (*pthread_key_dtor)(void *); -typedef struct cthread_descriptor_t *cthread_t; +typedef struct pthread_once_s { + _Atomic(char) lock; +} pthread_once_t; -void cthread_ungarbage(void); +typedef struct pthread_spinlock_s { + _Atomic(char) lock; +} pthread_spinlock_t; + +typedef struct pthread_mutex_s { + char type; + char pshared; + int reent; + _Atomic(int) lock; + _Atomic(int) waits; +} pthread_mutex_t; + +typedef struct pthread_mutexattr_s { + char type; + char pshared; +} pthread_mutexattr_t; + +typedef struct pthread_cond_s { + char pshared; + _Atomic(int) waits; + _Atomic(unsigned) seq; +} pthread_cond_t; + +typedef struct pthread_barrier_s { + char pshared; + int count; + _Atomic(int) waits; + _Atomic(int) popped; +} pthread_barrier_t; + +typedef struct pthread_rwlock_s { + char pshared; + _Atomic(int) lock; + _Atomic(int) waits; +} pthread_rwlock_t; + +typedef struct pthread_attr_s { + char detachstate; + char inheritsched; + int schedparam; + int schedpolicy; + int scope; + unsigned guardsize; + unsigned stacksize; + char *stackaddr; +} pthread_attr_t; + +int pthread_yield(void); +void pthread_exit(void *) wontreturn; +pthread_t pthread_self(void) pureconst; +pthread_id_np_t pthread_getthreadid_np(void); +int64_t pthread_getunique_np(pthread_t); +int pthread_setname_np(pthread_t, const char *); +int pthread_getname_np(pthread_t, char *, size_t); +int pthread_getattr_np(pthread_t, pthread_attr_t *); +int pthread_attr_init(pthread_attr_t *); +int pthread_attr_destroy(pthread_attr_t *); +int pthread_attr_getdetachstate(const pthread_attr_t *, int *); +int pthread_attr_setdetachstate(pthread_attr_t *, int); +int pthread_attr_getguardsize(const pthread_attr_t *, size_t *); +int pthread_attr_setguardsize(pthread_attr_t *, size_t); +int pthread_attr_getinheritsched(const pthread_attr_t *, int *); +int pthread_attr_setinheritsched(pthread_attr_t *, int); +int pthread_attr_getschedpolicy(const pthread_attr_t *, int *); +int pthread_attr_setschedpolicy(pthread_attr_t *, int); +int pthread_attr_getscope(const pthread_attr_t *, int *); +int pthread_attr_setscope(pthread_attr_t *, int); +int pthread_attr_getstack(const pthread_attr_t *, void **, size_t *); +int pthread_attr_setstack(pthread_attr_t *, void *, size_t); +int pthread_attr_getstacksize(const pthread_attr_t *, size_t *); +int pthread_attr_setstacksize(pthread_attr_t *, size_t); +int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), + void *); +int pthread_detach(pthread_t); +int pthread_cancel(pthread_t); +int pthread_join(pthread_t, void **); +int pthread_equal(pthread_t, pthread_t); +int pthread_once(pthread_once_t *, void (*)(void)); +int pthread_spin_init(pthread_spinlock_t *, int); +int pthread_spin_destroy(pthread_spinlock_t *); +int pthread_spin_lock(pthread_spinlock_t *); +int pthread_spin_unlock(pthread_spinlock_t *); +int pthread_spin_trylock(pthread_spinlock_t *); +int pthread_mutexattr_init(pthread_mutexattr_t *); +int pthread_mutexattr_destroy(pthread_mutexattr_t *); +int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_settype(pthread_mutexattr_t *, int); +int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); +int pthread_mutexattr_getpshared(const pthread_mutexattr_t *, int *); +int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int pthread_mutex_lock(pthread_mutex_t *); +int pthread_mutex_unlock(pthread_mutex_t *); +int pthread_mutex_trylock(pthread_mutex_t *); +int pthread_mutex_destroy(pthread_mutex_t *); +int pthread_mutex_consistent(pthread_mutex_t *); +int pthread_condattr_init(pthread_condattr_t *); +int pthread_condattr_destroy(pthread_condattr_t *); +int pthread_condattr_setpshared(pthread_condattr_t *, int); +int pthread_condattr_getpshared(const pthread_condattr_t *, int *); +int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); +int pthread_cond_destroy(pthread_cond_t *); +int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); +int pthread_cond_broadcast(pthread_cond_t *); +int pthread_cond_signal(pthread_cond_t *); +int pthread_rwlockattr_init(pthread_rwlockattr_t *); +int pthread_rwlockattr_destroy(pthread_rwlockattr_t *); +int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int); +int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *, int *); +int pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *); +int pthread_rwlock_destroy(pthread_rwlock_t *); +int pthread_rwlock_rdlock(pthread_rwlock_t *); +int pthread_rwlock_tryrdlock(pthread_rwlock_t *); +int pthread_rwlock_wrlock(pthread_rwlock_t *); +int pthread_rwlock_trywrlock(pthread_rwlock_t *); +int pthread_rwlock_unlock(pthread_rwlock_t *); +int pthread_key_create(pthread_key_t *, pthread_key_dtor); +int pthread_key_delete(pthread_key_t); +int pthread_setspecific(pthread_key_t, void *); +void *pthread_getspecific(pthread_key_t); +int pthread_barrierattr_init(pthread_barrierattr_t *); +int pthread_barrierattr_destroy(pthread_barrierattr_t *); +int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *); +int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); +int pthread_barrier_wait(pthread_barrier_t *); +int pthread_barrier_destroy(pthread_barrier_t *); +int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, + unsigned); + +#define pthread_spin_init(pSpin, multiprocess) ((pSpin)->lock = 0, 0) +#define pthread_spin_destroy(pSpin) ((pSpin)->lock = -1, 0) +#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 && \ + !defined(__STRICT_ANSI__) +extern const errno_t EBUSY; +#define pthread_spin_lock(pSpin) \ + ({ \ + pthread_spinlock_t *_s = pSpin; \ + while (__atomic_test_and_set(&_s->lock, __ATOMIC_SEQ_CST)) donothing; \ + 0; \ + }) +#define pthread_spin_unlock(pSpin) \ + ({ \ + pthread_spinlock_t *_s = pSpin; \ + __atomic_store_n(&_s->lock, 0, __ATOMIC_RELAXED); \ + 0; \ + }) +#define pthread_spin_trylock(pSpin) \ + ({ \ + pthread_spinlock_t *_s = pSpin; \ + __atomic_test_and_set(&_s->lock, __ATOMIC_SEQ_CST) ? EBUSY : 0; \ + }) +#endif /* GCC 4.7+ */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/thread/thread.mk b/libc/thread/thread.mk index a953bde03..c0cff1e2e 100644 --- a/libc/thread/thread.mk +++ b/libc/thread/thread.mk @@ -24,12 +24,13 @@ LIBC_THREAD_A_CHECKS = \ $(LIBC_THREAD_A_HDRS:%=o/$(MODE)/%.ok) LIBC_THREAD_A_DIRECTDEPS = \ - LIBC_STUBS \ LIBC_CALLS \ LIBC_INTRIN \ LIBC_MEM \ + LIBC_NT_KERNEL32 \ LIBC_RUNTIME \ LIBC_STR \ + LIBC_STUBS \ LIBC_SYSV \ LIBC_SYSV_CALLS \ LIBC_NEXGEN32E diff --git a/libc/intrin/pthread2.h b/libc/thread/thread2.h similarity index 96% rename from libc/intrin/pthread2.h rename to libc/thread/thread2.h index 0dffe6892..ec6b4a216 100644 --- a/libc/intrin/pthread2.h +++ b/libc/thread/thread2.h @@ -3,8 +3,8 @@ #include "libc/calls/struct/cpuset.h" #include "libc/calls/struct/sched_param.h" #include "libc/calls/struct/timespec.h" -#include "libc/intrin/pthread.h" #include "libc/runtime/stack.h" +#include "libc/thread/thread.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/thread/tls.h b/libc/thread/tls.h new file mode 100644 index 000000000..f5bf47e47 --- /dev/null +++ b/libc/thread/tls.h @@ -0,0 +1,45 @@ +#ifndef COSMOPOLITAN_LIBC_THREAD_TLS_H_ +#define COSMOPOLITAN_LIBC_THREAD_TLS_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +struct CosmoFtrace { /* 16 */ + bool ft_once; /* 0 */ + bool ft_noreentry; /* 1 */ + int ft_skew; /* 4 */ + int64_t ft_lastaddr; /* 8 */ +}; + +struct CosmoTib { + struct CosmoTib *tib_self; /* 0x00 */ + struct CosmoFtrace tib_ftrace; /* 0x08 */ + void *tib_garbages; /* 0x18 */ + intptr_t tib_locale; /* 0x20 */ + intptr_t tib_pthread; /* 0x28 */ + struct CosmoTib *tib_self2; /* 0x30 */ + int32_t tib_tid; /* 0x38 */ + int32_t tib_errno; /* 0x3c */ +}; + +extern int __threaded; +extern bool __tls_enabled; +extern unsigned __tls_index; + +void __require_tls(void); + +#if defined(__GNUC__) && defined(__x86_64__) && !defined(__STRICT_ANSI__) +/** + * Returns location of thread information block. + * + * This can't be used in privileged functions. + */ +static inline struct CosmoTib *__get_tls(void) { + struct CosmoTib *_tib; + asm("mov\t%%fs:0,%0" : "=r"(_tib) : /* no inputs */ : "memory"); + return _tib; +} +#endif /* GNU x86-64 */ + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_LIBC_THREAD_TLS_H_ */ diff --git a/libc/nexgen32e/gettls.h b/libc/thread/tls2.h similarity index 56% rename from libc/nexgen32e/gettls.h rename to libc/thread/tls2.h index e9a930989..f5ef79147 100644 --- a/libc/nexgen32e/gettls.h +++ b/libc/thread/tls2.h @@ -1,23 +1,10 @@ -#ifndef COSMOPOLITAN_LIBC_NEXGEN32E_GETTLS_H_ -#define COSMOPOLITAN_LIBC_NEXGEN32E_GETTLS_H_ +#ifndef COSMOPOLITAN_LIBC_THREAD_TLS2_H_ +#define COSMOPOLITAN_LIBC_THREAD_TLS2_H_ #include "libc/dce.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -#if defined(__GNUC__) && defined(__x86_64__) && !defined(__STRICT_ANSI__) -/** - * Returns location of thread information block. - * - * This can't be used in privileged functions. - */ -static noasan inline char *__get_tls(void) { - char *tib; - asm("mov\t%%fs:0,%0" : "=r"(tib) : /* no inputs */ : "memory"); - return tib; -} -#endif /* GNU x86-64 */ - #if defined(__GNUC__) && defined(__x86_64__) && !defined(__STRICT_ANSI__) /** * Returns location of thread information block. @@ -25,7 +12,7 @@ static noasan inline char *__get_tls(void) { * This should be favored over __get_tls() for .privileged code that * can't be self-modified by __enable_tls(). */ -static noasan inline char *__get_tls_privileged(void) { +static noasan inline struct CosmoTib *__get_tls_privileged(void) { char *tib, *lin = (char *)0x30; if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd() || IsMetal()) { asm("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory"); @@ -35,10 +22,10 @@ static noasan inline char *__get_tls_privileged(void) { tib = *(char **)(tib + 0x1480 + __tls_index * 8); } } - return tib; + return (struct CosmoTib *)tib; } #endif /* GNU x86-64 */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_GETTLS_H_ */ +#endif /* COSMOPOLITAN_LIBC_THREAD_TLS2_H_ */ diff --git a/libc/thread/ungarbage.c b/libc/thread/ungarbage.c index 80de172cd..0a33e7011 100644 --- a/libc/thread/ungarbage.c +++ b/libc/thread/ungarbage.c @@ -19,12 +19,11 @@ #include "libc/assert.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/gc.internal.h" -#include "libc/nexgen32e/gettls.h" -#include "libc/thread/thread.h" +#include "libc/thread/tls.h" -void cthread_ungarbage(void) { +void _pthread_ungarbage(void) { struct Garbages *g; - if ((g = ((cthread_t)__get_tls())->garbages)) { + if ((g = __get_tls()->tib_garbages)) { // _pthread_exit() uses _gclongjmp() so if this assertion fails, // then the likely cause is the thread used gc() with longjmp(). assert(!g->i); diff --git a/libc/time/localtime.c b/libc/time/localtime.c index 407c10c5f..1b0458a32 100644 --- a/libc/time/localtime.c +++ b/libc/time/localtime.c @@ -5,9 +5,9 @@ #include "libc/calls/calls.h" #include "libc/intrin/bits.h" #include "libc/intrin/nopl.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/gc.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" diff --git a/libc/zipos/get.c b/libc/zipos/get.c index 6511d5526..352b3475d 100644 --- a/libc/zipos/get.c +++ b/libc/zipos/get.c @@ -20,7 +20,7 @@ #include "libc/calls/strace.internal.h" #include "libc/intrin/cmpxchg.h" #include "libc/intrin/promises.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/map.h" diff --git a/libc/zipos/lock.c b/libc/zipos/lock.c index 1b86a9346..464619308 100644 --- a/libc/zipos/lock.c +++ b/libc/zipos/lock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/zipos/zipos.internal.h" static pthread_mutex_t __zipos_lock_obj; diff --git a/libc/zipos/zipos.internal.h b/libc/zipos/zipos.internal.h index dcccad9cb..25ab4545d 100644 --- a/libc/zipos/zipos.internal.h +++ b/libc/zipos/zipos.internal.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_LIBC_ZIPOS_ZIPOS_H_ #define COSMOPOLITAN_LIBC_ZIPOS_ZIPOS_H_ #include "libc/intrin/nopl.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/test/libc/calls/reservefd_test.c b/test/libc/calls/reservefd_test.c index b06e47680..7c1e535c6 100644 --- a/test/libc/calls/reservefd_test.c +++ b/test/libc/calls/reservefd_test.c @@ -25,7 +25,7 @@ #include "libc/errno.h" #include "libc/intrin/wait0.internal.h" #include "libc/macros.internal.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/internal.h" #include "libc/runtime/stack.h" #include "libc/stdio/rand.h" diff --git a/test/libc/intrin/pthread_mutex_lock2_test.c b/test/libc/intrin/pthread_mutex_lock2_test.c index e8d8794f9..b4302ad79 100644 --- a/test/libc/intrin/pthread_mutex_lock2_test.c +++ b/test/libc/intrin/pthread_mutex_lock2_test.c @@ -19,7 +19,7 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/timespec.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.internal.h" #include "libc/testlib/ezbench.h" diff --git a/test/libc/intrin/pthread_mutex_lock_test.c b/test/libc/intrin/pthread_mutex_lock_test.c index 97e4f515a..7c431e1e8 100644 --- a/test/libc/intrin/pthread_mutex_lock_test.c +++ b/test/libc/intrin/pthread_mutex_lock_test.c @@ -22,13 +22,12 @@ #include "libc/calls/strace.internal.h" #include "libc/errno.h" #include "libc/intrin/futex.internal.h" -#include "libc/intrin/pthread.h" #include "libc/intrin/wait0.internal.h" #include "libc/log/check.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/gc.internal.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" @@ -39,8 +38,8 @@ #include "libc/sysv/consts/rlimit.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" -#include "libc/thread/spawn.h" #include "libc/thread/thread.h" +#include "libc/thread/spawn.h" #define THREADS 8 #define ITERATIONS 512 diff --git a/test/libc/intrin/pthread_once_test.c b/test/libc/intrin/pthread_once_test.c index 9032724a8..5ca803509 100644 --- a/test/libc/intrin/pthread_once_test.c +++ b/test/libc/intrin/pthread_once_test.c @@ -17,7 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.internal.h" #include "libc/testlib/testlib.h" diff --git a/test/libc/intrin/rand64_test.c b/test/libc/intrin/rand64_test.c index 4251d7a43..8314ea1fa 100644 --- a/test/libc/intrin/rand64_test.c +++ b/test/libc/intrin/rand64_test.c @@ -25,7 +25,6 @@ #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/threaded.h" #include "libc/runtime/internal.h" #include "libc/runtime/stack.h" #include "libc/stdio/rand.h" diff --git a/test/libc/runtime/arch_prctl_test.c b/test/libc/runtime/arch_prctl_test.c index 6c1e9cda9..a66123e20 100644 --- a/test/libc/runtime/arch_prctl_test.c +++ b/test/libc/runtime/arch_prctl_test.c @@ -24,7 +24,7 @@ #include "libc/intrin/fsgsbase.h" #include "libc/intrin/kprintf.h" #include "libc/intrin/segmentation.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/nt/version.h" #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sig.h" diff --git a/test/libc/runtime/clone_test.c b/test/libc/thread/clone_test.c similarity index 89% rename from test/libc/runtime/clone_test.c rename to test/libc/thread/clone_test.c index a6d9c2cce..3c680eebb 100644 --- a/test/libc/runtime/clone_test.c +++ b/test/libc/thread/clone_test.c @@ -25,7 +25,6 @@ #include "libc/log/backtrace.internal.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" -#include "libc/nexgen32e/gettls.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" @@ -42,6 +41,8 @@ #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" +#include "libc/thread/tls.h" +#include "libc/thread/tls2.h" #include "libc/time/time.h" int x, me, tid; @@ -52,16 +53,6 @@ void SetUpOnce(void) { ASSERT_SYS(0, 0, pledge("stdio rpath", 0)); } -void *__initialize_tls(char tib[64]) { - if (tib) { - *(intptr_t *)(tib + 0x00) = (intptr_t)tib; - *(intptr_t *)(tib + 0x30) = (intptr_t)tib; - *(int *)(tib + 0x38) = -1; // tid - *(int *)(tib + 0x3c) = 0; - } - return tib; -} - int Hog(void *arg, int tid) { return 0; } @@ -164,14 +155,3 @@ TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) { } ASSERT_EQ(0, errno); } - -//////////////////////////////////////////////////////////////////////////////// -// BENCHMARK - -BENCH(clone, bench) { - char *volatile tp; - errno_t *volatile ep; - EZBENCH2("__errno_location", donothing, (ep = __errno_location())); - EZBENCH2("__get_tls_priv", donothing, (tp = __get_tls_privileged())); - EZBENCH2("__get_tls", donothing, (tp = __get_tls())); -} diff --git a/test/libc/intrin/pthread_barrier_wait_test.c b/test/libc/thread/pthread_barrier_wait_test.c similarity index 99% rename from test/libc/intrin/pthread_barrier_wait_test.c rename to test/libc/thread/pthread_barrier_wait_test.c index baa0d4acb..8bacd1b6f 100644 --- a/test/libc/intrin/pthread_barrier_wait_test.c +++ b/test/libc/thread/pthread_barrier_wait_test.c @@ -19,11 +19,11 @@ #include "libc/assert.h" #include "libc/errno.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" +#include "libc/thread/thread.h" int i, n; _Atomic(int) p, w; diff --git a/test/libc/intrin/pthread_cond_broadcast_test.c b/test/libc/thread/pthread_cond_broadcast_test.c similarity index 98% rename from test/libc/intrin/pthread_cond_broadcast_test.c rename to test/libc/thread/pthread_cond_broadcast_test.c index 9d944690d..0f28cb4ee 100644 --- a/test/libc/intrin/pthread_cond_broadcast_test.c +++ b/test/libc/thread/pthread_cond_broadcast_test.c @@ -20,12 +20,12 @@ #include "libc/errno.h" #include "libc/intrin/atomic.h" #include "libc/intrin/kprintf.h" -#include "libc/intrin/pthread.h" -#include "libc/intrin/pthread2.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" +#include "libc/thread/thread.h" +#include "libc/thread/thread2.h" // TODO(jart): Re-enable me. #if 0 diff --git a/test/libc/thread/pthread_create_test.c b/test/libc/thread/pthread_create_test.c index b59d3c51b..96566f673 100644 --- a/test/libc/thread/pthread_create_test.c +++ b/test/libc/thread/pthread_create_test.c @@ -22,8 +22,6 @@ #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/kprintf.h" -#include "libc/intrin/pthread.h" -#include "libc/intrin/pthread2.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/nexgen32e.h" @@ -37,6 +35,7 @@ #include "libc/testlib/subprocess.h" #include "libc/testlib/testlib.h" #include "libc/thread/thread.h" +#include "libc/thread/thread2.h" void OnUsr1(int sig, struct siginfo *si, void *vctx) { struct ucontext *ctx = vctx; diff --git a/test/libc/thread/pthread_key_create_test.c b/test/libc/thread/pthread_key_create_test.c index 15cdc4bd2..ced6cf725 100644 --- a/test/libc/thread/pthread_key_create_test.c +++ b/test/libc/thread/pthread_key_create_test.c @@ -16,9 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" #include "libc/mem/mem.h" -#include "libc/runtime/internal.h" #include "libc/testlib/testlib.h" #include "libc/thread/thread.h" diff --git a/test/libc/intrin/pthread_rwlock_rdlock_test.c b/test/libc/thread/pthread_rwlock_rdlock_test.c similarity index 95% rename from test/libc/intrin/pthread_rwlock_rdlock_test.c rename to test/libc/thread/pthread_rwlock_rdlock_test.c index 6eaf59a9a..ff3be1459 100644 --- a/test/libc/intrin/pthread_rwlock_rdlock_test.c +++ b/test/libc/thread/pthread_rwlock_rdlock_test.c @@ -16,11 +16,11 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" #include "libc/mem/mem.h" -#include "libc/runtime/gc.internal.h" +#include "libc/runtime/gc.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" +#include "libc/thread/thread.h" #define ITERATIONS 50000 #define READERS 8 @@ -53,7 +53,7 @@ int Writer(void *arg, int tid) { TEST(pthread_rwlock_rdlock, test) { int i; - struct spawn *t = gc(malloc(sizeof(struct spawn) * (READERS + WRITERS))); + struct spawn *t = _gc(malloc(sizeof(struct spawn) * (READERS + WRITERS))); pthread_barrier_init(&barrier, 0, READERS + WRITERS); for (i = 0; i < READERS + WRITERS; ++i) { ASSERT_SYS(0, 0, _spawn(i < READERS ? Reader : Writer, 0, t + i)); diff --git a/test/libc/thread/pthread_setname_np_test.c b/test/libc/thread/pthread_setname_np_test.c index e39226e75..c572cf5d8 100644 --- a/test/libc/thread/pthread_setname_np_test.c +++ b/test/libc/thread/pthread_setname_np_test.c @@ -18,7 +18,6 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/dce.h" #include "libc/intrin/atomic.h" -#include "libc/intrin/pthread.h" #include "libc/testlib/testlib.h" #include "libc/thread/thread.h" diff --git a/test/libc/thread/spawn_test.c b/test/libc/thread/spawn_test.c index 88193d605..c0a8f382c 100644 --- a/test/libc/thread/spawn_test.c +++ b/test/libc/thread/spawn_test.c @@ -26,7 +26,6 @@ #include "libc/runtime/internal.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" -#include "libc/thread/thread.h" _Atomic(int) itworked; _Thread_local int var; diff --git a/test/libc/x/makedirs_test.c b/test/libc/x/makedirs_test.c index d5da76a09..164f00028 100644 --- a/test/libc/x/makedirs_test.c +++ b/test/libc/x/makedirs_test.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.internal.h" #include "libc/testlib/testlib.h" diff --git a/third_party/dlmalloc/locks.inc b/third_party/dlmalloc/locks.inc index e3b722b27..8b116d524 100644 --- a/third_party/dlmalloc/locks.inc +++ b/third_party/dlmalloc/locks.inc @@ -1,7 +1,7 @@ // clang-format off #include "libc/calls/calls.h" -#include "libc/intrin/pthread.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/thread.h" +#include "libc/thread/tls.h" /* --------------------------- Lock preliminaries ------------------------ */ diff --git a/third_party/gdtoa/lock.c b/third_party/gdtoa/lock.c index 18290c86a..6297114c7 100644 --- a/third_party/gdtoa/lock.c +++ b/third_party/gdtoa/lock.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "third_party/gdtoa/lock.h" static pthread_mutex_t __gdtoa_lock_obj; diff --git a/third_party/gdtoa/lock.h b/third_party/gdtoa/lock.h index e3b281f0c..06191e67d 100644 --- a/third_party/gdtoa/lock.h +++ b/third_party/gdtoa/lock.h @@ -1,7 +1,7 @@ #ifndef COSMOPOLITAN_THIRD_PARTY_GDTOA_LOCK_H_ #define COSMOPOLITAN_THIRD_PARTY_GDTOA_LOCK_H_ #include "libc/intrin/nopl.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/third_party/libcxx/__threading_support b/third_party/libcxx/__threading_support index d7a0b2114..b67666d84 100644 --- a/third_party/libcxx/__threading_support +++ b/third_party/libcxx/__threading_support @@ -13,7 +13,7 @@ #include "third_party/libcxx/__config" #include "third_party/libcxx/chrono" #include "third_party/libcxx/iosfwd" -#include "libc/intrin/pthread2.h" +#include "libc/thread/thread2.h" #include "third_party/libcxx/errno.h" #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER @@ -25,7 +25,7 @@ #elif !defined(_LIBCPP_HAS_NO_THREADS) #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/calls/calls.h" #include "libc/calls/struct/sched_param.h" #include "libc/sysv/consts/sched.h" diff --git a/third_party/lua/lrepl.c b/third_party/lua/lrepl.c index d99a5998c..1a47d135c 100644 --- a/third_party/lua/lrepl.c +++ b/third_party/lua/lrepl.c @@ -31,7 +31,7 @@ #include "libc/calls/struct/sigaction.h" #include "libc/errno.h" #include "libc/intrin/nomultics.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/log/check.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" diff --git a/tool/build/mkdeps.c b/tool/build/mkdeps.c index e2876b925..60db8e297 100644 --- a/tool/build/mkdeps.c +++ b/tool/build/mkdeps.c @@ -24,7 +24,7 @@ #include "libc/fmt/fmt.h" #include "libc/intrin/bits.h" #include "libc/intrin/kprintf.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/wait0.internal.h" #include "libc/log/check.h" @@ -37,7 +37,7 @@ #include "libc/mem/bisectcarleft.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/crc32.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/runtime/ezmap.internal.h" #include "libc/runtime/gc.internal.h" #include "libc/runtime/runtime.h" diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 890ec1321..2f053c5cc 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -36,7 +36,7 @@ #include "libc/intrin/atomic.h" #include "libc/intrin/likely.h" #include "libc/intrin/nomultics.internal.h" -#include "libc/intrin/pthread.h" +#include "libc/thread/thread.h" #include "libc/intrin/safemacros.internal.h" #include "libc/log/check.h" #include "libc/log/log.h" @@ -48,7 +48,7 @@ #include "libc/nexgen32e/crc32.h" #include "libc/nexgen32e/nt2sysv.h" #include "libc/nexgen32e/rdtsc.h" -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "libc/nexgen32e/x86feature.h" #include "libc/nt/enum/fileflagandattributes.h" #include "libc/nt/thread.h" diff --git a/tool/plinko/plinko.c b/tool/plinko/plinko.c index 7a6120a16..6a75105c4 100644 --- a/tool/plinko/plinko.c +++ b/tool/plinko/plinko.c @@ -16,7 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/nexgen32e/threaded.h" +#include "libc/thread/tls.h" #include "tool/plinko/lib/plinko.h" STATIC_YOINK("__zipos_get");