cosmopolitan/libc/thread
Justine Tunney 3609f65de3
Make malloc() go 200x faster
If pthread_create() is linked into the binary, then the cosmo runtime
will create an independent dlmalloc arena for each core. Whenever the
malloc() function is used it will index `g_heaps[sched_getcpu() / 2]`
to find the arena with the greatest hyperthread / numa locality. This
may be configured via an environment variable. For example if you say
`export COSMOPOLITAN_HEAP_COUNT=1` then you can restore the old ways.
Your process may be configured to have anywhere between 1 - 128 heaps

We need this revision because it makes multithreaded C++ applications
faster. For example, an HTTP server I'm working on that makes extreme
use of the STL went from 16k to 2000k requests per second, after this
change was made. To understand why, try out the malloc_test benchmark
which calls malloc() + realloc() in a loop across many threads, which
sees a a 250x improvement in process clock time and 200x on wall time

The tradeoff is this adds ~25ns of latency to individual malloc calls
compared to MODE=tiny, once the cosmo runtime has transitioned into a
fully multi-threaded state. If you don't need malloc() to be scalable
then cosmo provides many options for you. For starters the heap count
variable above can be set to put the process back in single heap mode
plus you can go even faster still, if you include tinymalloc.inc like
many of the programs in tool/build/.. are already doing since that'll
shave tens of kb off your binary footprint too. Theres also MODE=tiny
which is configured to use just 1 plain old dlmalloc arena by default

Another tradeoff is we need more memory now (except in MODE=tiny), to
track the provenance of memory allocation. This is so allocations can
be freely shared across threads, and because OSes can reschedule code
to different CPUs at any time.
2024-06-05 02:02:14 -07:00
..
__cxa_thread_atexit_impl.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
alarm.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
BUILD.mk Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
freebsd.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
getitimer.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
itimer.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
itimer.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
makecontext.c Make improvements 2024-02-12 10:23:00 -08:00
mktls.c Fix thread-local storage bugs on aarch64 2024-05-08 04:20:22 -07:00
openbsd.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
posixthread.internal.h Fix fork locking on win32 (#1141) 2024-04-25 23:01:27 -04:00
pt.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
pthread_atfork.c Fix fork locking on win32 (#1141) 2024-04-25 23:01:27 -04:00
pthread_attr_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getdetachstate.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getguardsize.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getinheritsched.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getschedpolicy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getscope.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getsigmask_np.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_attr_getstack.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_getstacksize.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_init.c Make improvements 2024-01-29 16:31:58 -08:00
pthread_attr_setdetachstate.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setguardsize.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_attr_setinheritsched.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setschedpolicy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setscope.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setsigmask_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setstack.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_attr_setstacksize.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_barrier_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_barrier_init.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_barrier_wait.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_barrierattr_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_barrierattr_getpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_barrierattr_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_barrierattr_setpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cancel.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_cond_broadcast.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cond_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cond_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cond_signal.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cond_timedwait.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_cond_wait.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_condattr_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_condattr_getpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_condattr_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_condattr_setpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_create.c Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
pthread_detach.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_equal.c Add much of C11 threads.h API 2024-04-28 07:04:08 -07:00
pthread_exit.c Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
pthread_getaffinity_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_getattr_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_getname_np.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_getschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_getspecific.c Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
pthread_getthreadid_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_getunique_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_join.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_key_create.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_key_delete.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_keys.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_kill.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_once.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_orphan_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_reschedule.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_rdlock.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_tryrdlock.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_trywrlock.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_unlock.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlock_wrlock.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlockattr_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlockattr_getpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlockattr_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_rwlockattr_setpshared.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_self.c Add much of C11 threads.h API 2024-04-28 07:04:08 -07:00
pthread_setaffinity_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_setcanceltype.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_setname_np.c Introduce FreeBSD ARM64 support 2023-12-29 20:14:02 -08:00
pthread_setschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_setschedparam_freebsd.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_setschedprio.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_sigmask.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_timedjoin_np.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
pthread_tryjoin_np.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_ungarbage.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
pthread_zombify.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
README.md Use *NSYNC for POSIX threads locking APIs 2022-09-11 11:04:50 -07:00
sem_destroy.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
sem_getvalue.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
sem_init.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
sem_open.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
sem_post.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
sem_timedwait.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
sem_trywait.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
sem_wait.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
semaphore.h Add dontthrow attribute to most libc functions 2024-01-09 01:26:03 -08:00
setitimer.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
thrd_create.c Add much of C11 threads.h API 2024-04-28 07:04:08 -07:00
thrd_detach.c Add much of C11 threads.h API 2024-04-28 07:04:08 -07:00
thrd_join.c Add much of C11 threads.h API 2024-04-28 07:04:08 -07:00
thread.h Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
thread2.h Add dontthrow attribute to most libc functions 2024-01-09 01:26:03 -08:00
threads.h Introduce support for trapping math 2024-04-30 13:38:43 -07:00
tls.h Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
tls2.internal.h Walk back most uses of __STRICT_ANSI__ 2024-02-27 04:09:49 -08:00
ualarm.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
xnu.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00

Cosmpolitan POSIX Threads Library

Cosmopolitan Libc implements threading as it is written in The Open Group Base Specifications Issue 7, 2018 edition IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) in addition to GNU extensions.