mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +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
1
third_party/dlmalloc/dlmalloc.c
vendored
1
third_party/dlmalloc/dlmalloc.c
vendored
|
@ -45,7 +45,6 @@
|
|||
#define USE_LOCKS 2
|
||||
#define MALLOC_INSPECT_ALL 1
|
||||
#define ABORT_ON_ASSERT_FAILURE 0
|
||||
#define LOCK_AT_FORK 1
|
||||
#define NO_MALLOC_STATS 1
|
||||
|
||||
#if IsModeDbg()
|
||||
|
|
26
third_party/dlmalloc/init.inc
vendored
26
third_party/dlmalloc/init.inc
vendored
|
@ -3,38 +3,38 @@
|
|||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
||||
/* ---------------------------- setting mparams -------------------------- */
|
||||
|
||||
#if LOCK_AT_FORK
|
||||
#if ONLY_MSPACES
|
||||
|
||||
void dlmalloc_pre_fork(void) {
|
||||
#if ONLY_MSPACES
|
||||
mstate h;
|
||||
for (unsigned i = ARRAYLEN(g_heaps); i--;)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
ACQUIRE_LOCK(&h->mutex);
|
||||
#else
|
||||
ACQUIRE_LOCK(&(gm)->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void dlmalloc_post_fork_parent(void) {
|
||||
#if ONLY_MSPACES
|
||||
mstate h;
|
||||
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
RELEASE_LOCK(&h->mutex);
|
||||
#else
|
||||
RELEASE_LOCK(&(gm)->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void dlmalloc_post_fork_child(void) {
|
||||
#if ONLY_MSPACES
|
||||
mstate h;
|
||||
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
||||
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
|
||||
(void)REFRESH_LOCK(&h->mutex);
|
||||
}
|
||||
|
||||
REFRESH_LOCK(&h->mutex);
|
||||
#else
|
||||
void dlmalloc_pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); }
|
||||
void dlmalloc_post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); }
|
||||
void dlmalloc_post_fork_child(void) { (void)REFRESH_LOCK(&(gm)->mutex); }
|
||||
#endif /* ONLY_MSPACES */
|
||||
#endif /* LOCK_AT_FORK */
|
||||
REFRESH_LOCK(&(gm)->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Initialize mparams */
|
||||
__attribute__((__constructor__(49))) int init_mparams(void) {
|
||||
|
|
4
third_party/dlmalloc/platform.inc
vendored
4
third_party/dlmalloc/platform.inc
vendored
|
@ -151,10 +151,6 @@
|
|||
========================================================================
|
||||
*/
|
||||
|
||||
#ifndef LOCK_AT_FORK
|
||||
#define LOCK_AT_FORK 0
|
||||
#endif
|
||||
|
||||
/* ------------------- size_t and alignment properties -------------------- */
|
||||
|
||||
/* The byte and bit size of a size_t */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue