mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-06 06:31:02 +00:00
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:
parent
906bd06a5a
commit
624573207e
51 changed files with 1006 additions and 321 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue