mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
85f64f3851
Thanks to @autumnjolitz (in #876) the Cosmopolitan codebase is now acquainted with Apple's outstanding ulock system calls which offer something much closer to futexes than Grand Central Dispatch which wasn't quite as good, since its wait function can't be interrupted by signals (therefore necessitating a busy loop) and it also needs semaphore objects to be created and freed. Even though ulock is an internal Apple API, strictly speaking, the benefits of futexes are so great that it's worth the risk for now especially since we have the GCD implementation still as a quick escape hatch if it changes Here's why this change is important for x86 XNU users. Cosmo has a suboptimal polyfill when the operating system doesn't offer an API that let's us implement futexes properly. Sadly we had to use that on X86 XNU until now. The polyfill works using clock_nanosleep, to poll the futex in a busy loop with exponential backoff. On XNU x86 clock_nanosleep suffers from us not being able to use a fast clock gettime implementation, which had a compounding effect that's made the polyfill function even more poorly. On X86 XNU we also need to polyfill sched_yield() using select(), which made things even more troublesome. Now that we have futexes we don't have any busy loops anymore for both condition variables and thread joining so optimal performance is attained. To demonstrate, consider these benchmarks Before: $ ./lockscale_test.com -b consumed 38.8377 seconds real time and 0.087131 seconds cpu time After: $ ./lockscale_test.com -b consumed 0.007955 seconds real time and 0.011515 seconds cpu time Fixes #876 |
||
---|---|---|
.. | ||
mem | ||
testing | ||
array.internal.h | ||
atomic.h | ||
atomic.internal.h | ||
common.c | ||
common.internal.h | ||
compat.S | ||
counter.h | ||
cv.h | ||
debug.h | ||
futex.c | ||
futex.internal.h | ||
heap.internal.h | ||
LICENSE.txt | ||
mu.c | ||
mu.h | ||
mu_semaphore.c | ||
mu_semaphore.h | ||
mu_semaphore.internal.h | ||
mu_semaphore_futex.c | ||
mu_semaphore_gcd.c | ||
mu_semaphore_sem.c | ||
mu_wait.h | ||
note.h | ||
nsync.mk | ||
once.h | ||
panic.c | ||
races.internal.h | ||
README.cosmo | ||
README.md | ||
time.h | ||
wait_s.internal.h | ||
waiter.h | ||
yield.c |
*NSYNC
*NSYNC is a library providing scalable synchronization primitives. The following packages are provided:
THIRD_PARTY_NSYNC
hasnsync_mu
which doesn't depend on malloc().THIRD_PARTY_NSYNC_MEM
has the rest of *NSYNC, e.g.nsync_cv
.
The origin of this code is here:
git@github.com:google/nsync
ac5489682760393fe21bd2a8e038b528442412a7 (1.25.0)
Author: Mike Burrows <m3b@google.com>
Date: Wed Jun 1 16:47:52 2022 -0700
NSYNC uses the Apache 2.0 license. We made the following local changes:
-
Write custom
nsync_malloc_()
somalloc()
can use *NSYNC. -
Rewrite
futex()
wrapper to support old Linux kernels and OpenBSD. -
Normalize sources to Cosmopolitan style conventions; *NSYNC upstream supports dozens of compilers and operating systems, at compile-time. Since Cosmo solves portability at runtime instead, most of the build config toil has been removed, in order to help the NSYNC source code be more readable and hackable.