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

@ -27,6 +27,7 @@ LIBC_TESTLIB_A_HDRS = \
libc/testlib/ezbench.h \
libc/testlib/fastrandomstring.h \
libc/testlib/hyperion.h \
libc/testlib/manystack.h \
libc/testlib/moby.h \
libc/testlib/subprocess.h \
libc/testlib/testlib.h \
@ -70,6 +71,7 @@ LIBC_TESTLIB_A_SRCS_C = \
libc/testlib/globals.c \
libc/testlib/hexequals.c \
libc/testlib/incrementfailed.c \
libc/testlib/manystack.c \
libc/testlib/memoryexists.c \
libc/testlib/seterrno.c \
libc/testlib/shoulddebugbreak.c \
@ -110,6 +112,7 @@ LIBC_TESTLIB_A_DIRECTDEPS = \
LIBC_STR \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TINYMATH \
LIBC_X \
THIRD_PARTY_COMPILER_RT \

69
libc/testlib/manystack.c Normal file
View file

@ -0,0 +1,69 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 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/testlib/manystack.h"
#include "libc/atomic.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/intrin/dll.h"
#include "libc/sysv/consts/sig.h"
#include "libc/thread/posixthread.internal.h"
static atomic_int manystack_gotsig;
static atomic_bool manystack_shutdown;
static void manystack_signal(int sig) {
manystack_gotsig = sig;
}
static void *manystack_thread(void *arg) {
sigset_t ss;
sigfillset(&ss);
sigdelset(&ss, SIGUSR2);
while (!manystack_shutdown) {
sigsuspend(&ss);
if (!manystack_shutdown) {
_pthread_lock();
for (struct Dll *e = dll_first(_pthread_list); e;
e = dll_next(_pthread_list, e)) {
pthread_t th = (pthread_t)POSIXTHREAD_CONTAINER(e);
if (!pthread_equal(th, pthread_self()))
pthread_kill(th, SIGQUIT);
}
_pthread_unlock();
}
}
return 0;
}
pthread_t manystack_start(void) {
sigset_t ss;
pthread_t msh;
sigemptyset(&ss);
sigaddset(&ss, SIGUSR2);
sigprocmask(SIG_BLOCK, &ss, 0);
signal(SIGUSR2, manystack_signal);
pthread_create(&msh, 0, manystack_thread, 0);
return msh;
}
void manystack_stop(pthread_t msh) {
manystack_shutdown = true;
pthread_kill(msh, SIGUSR2);
pthread_join(msh, 0);
}

10
libc/testlib/manystack.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_LIBC_TESTLIB_MANYSTACK_H_
#define COSMOPOLITAN_LIBC_TESTLIB_MANYSTACK_H_
#include "libc/thread/thread.h"
COSMOPOLITAN_C_START_
pthread_t manystack_start(void);
void manystack_stop(pthread_t);
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_TESTLIB_MANYSTACK_H_ */

View file

@ -156,7 +156,7 @@ int main(int argc, char *argv[]) {
// make sure threads are in a good state
if (_weaken(_pthread_decimate))
_weaken(_pthread_decimate)(false);
_weaken(_pthread_decimate)();
if (_weaken(pthread_orphan_np) && !_weaken(pthread_orphan_np)()) {
tinyprint(2, "error: tests ended with threads still active\n", NULL);
_Exit(1);