Make threads faster and more reliable

This change doubles the performance of thread spawning. That's thanks to
our new stack manager, which allows us to avoid zeroing stacks. It gives
us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is
faster now, since it doesn't need to acquire the pthread GIL. On NetBSD,
that helps us avoid allocating too many semaphores. Even if that happens
we're now able to survive semaphores running out and even memory running
out, when allocating *NSYNC waiter objects. I found a lot more rare bugs
in the POSIX threads runtime that could cause things to crash, if you've
got dozens of threads all spawning and joining dozens of threads. I want
cosmo to be world class production worthy for 2025 so happy holidays all
This commit is contained in:
Justine Tunney 2024-12-18 04:59:02 -08:00
parent 906bd06a5a
commit 624573207e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
51 changed files with 1006 additions and 321 deletions

View file

@ -33,18 +33,13 @@
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/itimer.internal.h"
#include "libc/thread/itimer.h"
#include "libc/thread/thread2.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
#define STACK_SIZE 65536
struct IntervalTimer __itimer = {
.lock = PTHREAD_MUTEX_INITIALIZER,
.cond = PTHREAD_COND_INITIALIZER,
};
static textwindows dontinstrument uint32_t __itimer_worker(void *arg) {
struct CosmoTib tls;
char *sp = __builtin_frame_address(0);
@ -55,7 +50,7 @@ static textwindows dontinstrument uint32_t __itimer_worker(void *arg) {
for (;;) {
bool dosignal = false;
struct timeval now, waituntil;
pthread_mutex_lock(&__itimer.lock);
__itimer_lock();
now = timeval_real();
if (timeval_iszero(__itimer.it.it_value)) {
waituntil = timeval_max;
@ -76,13 +71,13 @@ static textwindows dontinstrument uint32_t __itimer_worker(void *arg) {
dosignal = true;
}
}
pthread_mutex_unlock(&__itimer.lock);
__itimer_unlock();
if (dosignal)
__sig_generate(SIGALRM, SI_TIMER);
pthread_mutex_lock(&__itimer.lock);
__itimer_lock();
struct timespec deadline = timeval_totimespec(waituntil);
pthread_cond_timedwait(&__itimer.cond, &__itimer.lock, &deadline);
pthread_mutex_unlock(&__itimer.lock);
__itimer_unlock();
}
return 0;
}
@ -92,39 +87,30 @@ static textwindows void __itimer_setup(void) {
kNtStackSizeParamIsAReservation, 0);
}
textwindows void __itimer_wipe(void) {
// this function is called by fork(), because
// timers aren't inherited by forked subprocesses
bzero(&__itimer, sizeof(__itimer));
}
textwindows int sys_setitimer_nt(int which, const struct itimerval *neu,
struct itimerval *old) {
struct itimerval config;
cosmo_once(&__itimer.once, __itimer_setup);
if (which != ITIMER_REAL || (neu && (!timeval_isvalid(neu->it_value) ||
!timeval_isvalid(neu->it_interval)))) {
!timeval_isvalid(neu->it_interval))))
return einval();
}
if (neu) {
if (neu)
// POSIX defines setitimer() with the restrict keyword but let's
// accommodate the usage setitimer(ITIMER_REAL, &it, &it) anyway
config = *neu;
}
BLOCK_SIGNALS;
pthread_mutex_lock(&__itimer.lock);
__itimer_lock();
if (old) {
old->it_interval = __itimer.it.it_interval;
old->it_value = timeval_subz(__itimer.it.it_value, timeval_real());
}
if (neu) {
if (!timeval_iszero(config.it_value)) {
if (!timeval_iszero(config.it_value))
config.it_value = timeval_add(config.it_value, timeval_real());
}
__itimer.it = config;
pthread_cond_signal(&__itimer.cond);
}
pthread_mutex_unlock(&__itimer.lock);
__itimer_unlock();
ALLOW_SIGNALS;
return 0;
}

View file

@ -15,7 +15,9 @@ struct IntervalTimer {
extern struct IntervalTimer __itimer;
void __itimer_wipe(void);
void __itimer_lock(void);
void __itimer_unlock(void);
void __itimer_wipe_and_reset(void);
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_ITIMER_H_ */

View file

@ -16,22 +16,41 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/sysv/consts/clock.h"
#include "libc/thread/thread.h"
#include "libc/cosmo.h"
#include "libc/errno.h"
#include "libc/runtime/stack.h"
/**
* Initializes condition variable.
* Allocates stack.
*
* @param attr may be null
* @return 0 on success, or error number on failure
* The size of your returned stack is always GetStackSize().
*
* The bottom 4096 bytes of your stack can't be used, since it's always
* reserved for a read-only guard page. With ASAN it'll be poisoned too.
*
* The top 16 bytes of a stack can't be used due to openbsd:stackbound
* and those bytes are also poisoned under ASAN build modes.
*
* @return stack bottom address on success, or null w/ errno
*/
errno_t pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr) {
*cond = (pthread_cond_t){0};
if (attr) {
cond->_pshared = attr->_pshared;
cond->_clock = attr->_clock;
}
void *NewCosmoStack(void) {
void *stackaddr;
size_t stacksize = GetStackSize();
size_t guardsize = GetGuardSize();
errno_t err = cosmo_stack_alloc(&stacksize, &guardsize, &stackaddr);
if (!err)
return stackaddr;
errno = err;
return 0;
}
/**
* Frees stack.
*
* @param stackaddr was allocated by NewCosmoStack()
* @return 0 on success, or -1 w/ errno
*/
int FreeCosmoStack(void *stackaddr) {
cosmo_stack_free(stackaddr, GetStackSize(), GetGuardSize());
return 0;
}

View file

@ -74,6 +74,7 @@ struct PosixThread {
int pt_flags; // 0x00: see PT_* constants
atomic_int pt_canceled; // 0x04: thread has bad beliefs
_Atomic(enum PosixThreadStatus) pt_status;
_Atomic(atomic_int *) pt_blocker;
atomic_int ptid; // transitions 0 → tid
atomic_int pt_refs; // prevents decimation
void *(*pt_start)(void *); // creation callback
@ -83,11 +84,10 @@ struct PosixThread {
struct CosmoTib *tib; // middle of tls allocation
struct Dll list; // list of threads
struct _pthread_cleanup_buffer *pt_cleanup;
_Atomic(atomic_int *) pt_blocker;
uint64_t pt_blkmask;
int64_t pt_event;
locale_t pt_locale;
jmp_buf pt_exiter;
intptr_t pt_exiter[5];
pthread_attr_t pt_attr;
atomic_bool pt_intoff;
};
@ -95,6 +95,7 @@ struct PosixThread {
typedef void (*atfork_f)(void);
extern struct Dll *_pthread_list;
extern _Atomic(unsigned) _pthread_count;
extern struct PosixThread _pthread_static;
extern _Atomic(pthread_key_dtor) _pthread_key_dtor[PTHREAD_KEYS_MAX];
@ -103,7 +104,7 @@ int _pthread_setschedparam_freebsd(int, int, const struct sched_param *);
int _pthread_tid(struct PosixThread *) libcesque;
intptr_t _pthread_syshand(struct PosixThread *) libcesque;
long _pthread_cancel_ack(void) libcesque;
void _pthread_decimate(bool) libcesque;
void _pthread_decimate(void) libcesque;
void _pthread_free(struct PosixThread *) libcesque;
void _pthread_lock(void) libcesque;
void _pthread_onfork_child(void) libcesque;

View file

@ -16,6 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/atomic.h"
#include "libc/cosmo.h"
#include "libc/errno.h"
#include "libc/intrin/strace.h"
#include "libc/mem/mem.h"
@ -28,13 +30,12 @@ struct AtFork {
};
struct AtForks {
pthread_once_t once;
atomic_uint once;
pthread_mutex_t lock;
struct AtFork *list;
};
static struct AtForks _atforks = {
.once = PTHREAD_ONCE_INIT,
.lock = PTHREAD_MUTEX_INITIALIZER,
};
@ -161,7 +162,7 @@ void _pthread_onfork_child(void) {
* @raise ENOMEM if we require more vespene gas
*/
int pthread_atfork(atfork_f prepare, atfork_f parent, atfork_f child) {
pthread_once(&_atforks.once, pthread_atfork_init);
cosmo_once(&_atforks.once, pthread_atfork_init);
struct AtFork *a;
if (!(a = calloc(1, sizeof(struct AtFork))))
return ENOMEM;

View file

@ -28,6 +28,10 @@
* pthread_create(0, &attr, func, 0);
* pthread_attr_destroy(&attr);
*
* If you use this, please be warned that your thread might run and exit
* before pthread_create() even returns. You really should assume it can
* not be used with any pthread APIs from the calling thread.
*
* @param detachstate can be one of
* - `PTHREAD_CREATE_JOINABLE` (default)
* - `PTHREAD_CREATE_DETACHED`

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
#include "libc/thread/thread2.h"
/**

View file

@ -18,10 +18,12 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/sigaltstack.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
@ -29,6 +31,7 @@
#include "libc/intrin/describeflags.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/stack.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/log/internal.h"
@ -48,7 +51,6 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/ss.h"
@ -65,9 +67,6 @@ __static_yoink("_pthread_onfork_prepare");
__static_yoink("_pthread_onfork_parent");
__static_yoink("_pthread_onfork_child");
#define MAP_ANON_OPENBSD 0x1000
#define MAP_STACK_OPENBSD 0x4000
void _pthread_free(struct PosixThread *pt) {
// thread must be removed from _pthread_list before calling
@ -79,7 +78,8 @@ void _pthread_free(struct PosixThread *pt) {
// unmap stack if the cosmo runtime was responsible for mapping it
if (pt->pt_flags & PT_OWNSTACK)
unassert(!munmap(pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize));
cosmo_stack_free(pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
pt->pt_attr.__guardsize);
// free any additional upstream system resources
// our fork implementation wipes this handle in child automatically
@ -99,7 +99,7 @@ void _pthread_free(struct PosixThread *pt) {
free(pt);
}
void _pthread_decimate(bool annihilation_only) {
void _pthread_decimate(void) {
struct PosixThread *pt;
struct Dll *e, *e2, *list = 0;
enum PosixThreadStatus status;
@ -123,17 +123,6 @@ void _pthread_decimate(bool annihilation_only) {
dll_make_first(&list, e);
}
// code like pthread_exit() needs to call this in order to know if
// it's appropriate to run exit() handlers however we really don't
// want to have a thread exiting block on a bunch of __maps locks!
// therefore we only take action if we'll destroy all but the self
if (annihilation_only)
if (!(_pthread_list == _pthread_list->prev &&
_pthread_list == _pthread_list->next)) {
dll_make_last(&_pthread_list, list);
list = 0;
}
// release posix threads gil
_pthread_unlock();
@ -167,11 +156,14 @@ static int PosixThread(void *arg, int tid) {
}
// set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->pt_exiter)) {
sigdelset(&pt->pt_attr.__sigmask, SIGTHR);
if (!__builtin_setjmp(pt->pt_exiter)) {
// setup signals for new thread
pt->pt_attr.__sigmask &= ~(1ull << (SIGTHR - 1));
if (IsWindows() || IsMetal()) {
atomic_store_explicit(&__get_tls()->tib_sigmask, pt->pt_attr.__sigmask,
memory_order_release);
if (_weaken(__sig_check))
_weaken(__sig_check)();
} else {
sys_sigprocmask(SIG_SETMASK, &pt->pt_attr.__sigmask, 0);
}
@ -189,39 +181,6 @@ static int PosixThread(void *arg, int tid) {
return 0;
}
static bool TellOpenbsdThisIsStackMemory(void *addr, size_t size) {
return __sys_mmap(
addr, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANON_OPENBSD | MAP_STACK_OPENBSD, -1,
0, 0) == addr;
}
// OpenBSD only permits RSP to occupy memory that's been explicitly
// defined as stack memory, i.e. `lo <= %rsp < hi` must be the case
static errno_t FixupCustomStackOnOpenbsd(pthread_attr_t *attr) {
// get interval
uintptr_t lo = (uintptr_t)attr->__stackaddr;
uintptr_t hi = lo + attr->__stacksize;
// squeeze interval
lo = (lo + __pagesize - 1) & -__pagesize;
hi = hi & -__pagesize;
// tell os it's stack memory
errno_t olderr = errno;
if (!TellOpenbsdThisIsStackMemory((void *)lo, hi - lo)) {
errno_t err = errno;
errno = olderr;
return err;
}
// update attributes with usable stack address
attr->__stackaddr = (void *)lo;
attr->__stacksize = hi - lo;
return 0;
}
static errno_t pthread_create_impl(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg,
@ -266,37 +225,18 @@ static errno_t pthread_create_impl(pthread_t *thread,
}
} else {
// cosmo is managing the stack
int pagesize = __pagesize;
pt->pt_attr.__guardsize = ROUNDUP(pt->pt_attr.__guardsize, pagesize);
pt->pt_attr.__stacksize = pt->pt_attr.__stacksize;
if (pt->pt_attr.__guardsize + pagesize > pt->pt_attr.__stacksize) {
pt->pt_flags |= PT_OWNSTACK;
errno_t err =
cosmo_stack_alloc(&pt->pt_attr.__stacksize, &pt->pt_attr.__guardsize,
&pt->pt_attr.__stackaddr);
if (err) {
_pthread_free(pt);
return EINVAL;
}
pt->pt_attr.__stackaddr =
mmap(0, pt->pt_attr.__stacksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (pt->pt_attr.__stackaddr != MAP_FAILED) {
if (IsOpenbsd())
if (!TellOpenbsdThisIsStackMemory(pt->pt_attr.__stackaddr,
pt->pt_attr.__stacksize))
notpossible;
if (pt->pt_attr.__guardsize)
if (mprotect(pt->pt_attr.__stackaddr, pt->pt_attr.__guardsize,
PROT_NONE | PROT_GUARD))
notpossible;
}
if (!pt->pt_attr.__stackaddr || pt->pt_attr.__stackaddr == MAP_FAILED) {
rc = errno;
_pthread_free(pt);
errno = e;
if (rc == EINVAL || rc == EOVERFLOW) {
if (err == EINVAL || err == EOVERFLOW) {
return EINVAL;
} else {
return EAGAIN;
}
}
pt->pt_flags |= PT_OWNSTACK;
}
// setup signal stack
@ -338,6 +278,10 @@ static errno_t pthread_create_impl(pthread_t *thread,
dll_make_first(&_pthread_list, &pt->list);
_pthread_unlock();
// if pthread_attr_setdetachstate() was used then it's possible for
// the `pt` object to be freed before this clone call has returned!
_pthread_ref(pt);
// launch PosixThread(pt) in new thread
if ((rc = clone(PosixThread, pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES |
@ -400,8 +344,8 @@ static const char *DescribeHandle(char buf[12], errno_t err, pthread_t *th) {
* _lwp_create
*
*
* @param thread if non-null is used to output the thread id
* upon successful completion
* @param thread is used to output the thread id upon success, which
* must be non-null
* @param attr points to launch configuration, or may be null
* to use sensible defaults; it must be initialized using
* pthread_attr_init()
@ -417,12 +361,14 @@ static const char *DescribeHandle(char buf[12], errno_t err, pthread_t *th) {
errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
errno_t err;
_pthread_decimate(false);
_pthread_decimate();
BLOCK_SIGNALS;
err = pthread_create_impl(thread, attr, start_routine, arg, _SigMask);
ALLOW_SIGNALS;
STRACE("pthread_create([%s], %p, %t, %p) → %s",
DescribeHandle(alloca(12), err, thread), attr, start_routine, arg,
DescribeErrno(err));
if (!err)
_pthread_unref(*(struct PosixThread **)thread);
return err;
}

View file

@ -16,22 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/cosmo.h"
#include "libc/intrin/stack.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
/**
* Garbage collects POSIX threads runtime.
*
* Let's say you want to run a memory leak detector. You can say:
* This function frees unreferenced zombie threads and empties cache
* memory associated with the Cosmopolitan POSIX threads runtime.
*
* Here's an example use case for this function. Let's say you want to
* create a malloc() memory leak detector. If your program was running
* threads earlier, then there might still be allocations lingering
* around, that'll give you false positives. To fix this, what you would
* do is call the following, right before running your leak detector:
*
* while (!pthread_orphan_np())
* pthread_decimate_np();
*
* To wait until all threads have exited.
* Which will wait until all threads have exited and their memory freed.
*
* @return 0 on success, or errno on error
*/
int pthread_decimate_np(void) {
_pthread_decimate(false);
_pthread_decimate();
cosmo_stack_clear();
return 0;
}

View file

@ -69,7 +69,7 @@
* @noreturn
*/
wontreturn void pthread_exit(void *rc) {
int orphan;
unsigned population;
struct CosmoTib *tib;
struct PosixThread *pt;
enum PosixThreadStatus status, transition;
@ -94,10 +94,21 @@ wontreturn void pthread_exit(void *rc) {
__cxa_thread_finalize();
// run atexit handlers if orphaned thread
_pthread_decimate(true);
if ((orphan = pthread_orphan_np()))
if (_weaken(__cxa_finalize))
_weaken(__cxa_finalize)(NULL);
// notice how we avoid acquiring the pthread gil
if (!(population = atomic_fetch_sub(&_pthread_count, 1) - 1)) {
// we know for certain we're an orphan. any other threads that
// exist, will terminate and clear their tid very soon. but...
// some goofball could spawn more threads from atexit handlers
for (;;) {
_pthread_decimate();
if (pthread_orphan_np()) {
if (_weaken(__cxa_finalize))
_weaken(__cxa_finalize)(NULL);
population = atomic_load(&_pthread_count);
break;
}
}
}
// transition the thread to a terminated state
status = atomic_load_explicit(&pt->pt_status, memory_order_acquire);
@ -127,7 +138,7 @@ wontreturn void pthread_exit(void *rc) {
// thread has been terminated. The behavior shall be as if the
// implementation called exit() with a zero argument at thread
// termination time." ──Quoth POSIX.1-2017
if (orphan) {
if (!population) {
for (int i = __fini_array_end - __fini_array_start; i--;)
((void (*)(void))__fini_array_start[i])();
_Exit(0);
@ -143,7 +154,7 @@ wontreturn void pthread_exit(void *rc) {
}
// this is a child thread
longjmp(pt->pt_exiter, 1);
__builtin_longjmp(pt->pt_exiter, 1);
}
__weak_reference(pthread_exit, thr_exit);

View file

@ -43,6 +43,8 @@ errno_t pthread_kill(pthread_t thread, int sig) {
int err = 0;
struct PosixThread *pt;
pt = (struct PosixThread *)thread;
if (pt)
_pthread_ref(pt);
if (!thread) {
err = EFAULT;
} else if (!(1 <= sig && sig <= 64)) {
@ -69,5 +71,7 @@ errno_t pthread_kill(pthread_t thread, int sig) {
}
STRACE("pthread_kill(%d, %G) → %s", _pthread_tid(pt), sig,
DescribeErrno(err));
if (pt)
_pthread_unref(pt);
return err;
}

View file

@ -17,10 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
@ -40,7 +42,7 @@
#include "libc/thread/tls.h"
static struct Semaphores {
pthread_once_t once;
atomic_uint once;
pthread_mutex_t lock;
struct Semaphore {
struct Semaphore *next;
@ -49,7 +51,9 @@ static struct Semaphores {
bool dead;
int refs;
} *list;
} g_semaphores;
} g_semaphores = {
.lock = PTHREAD_MUTEX_INITIALIZER,
};
static void sem_open_lock(void) {
pthread_mutex_lock(&g_semaphores.lock);
@ -69,7 +73,7 @@ static void sem_open_setup(void) {
}
static void sem_open_init(void) {
pthread_once(&g_semaphores.once, sem_open_setup);
cosmo_once(&g_semaphores.once, sem_open_setup);
}
static sem_t *sem_open_impl(const char *path, int oflag, unsigned mode,

View file

@ -128,10 +128,10 @@ typedef struct pthread_attr_s {
int __schedparam;
int __schedpolicy;
int __contentionscope;
int __guardsize;
int __stacksize;
int __sigaltstacksize;
uint64_t __sigmask;
size_t __guardsize;
size_t __stacksize;
void *__stackaddr;
void *__sigaltstackaddr;
} pthread_attr_t;