cosmopolitan/third_party/nsync/README.cosmo
Justine Tunney 85f64f3851
Make futexes 100x better on x86 MacOS
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
2023-10-03 15:15:43 -07:00

41 lines
1.5 KiB
Text

DESCRIPTION
*NSYNC is a synchronization primitives library.
LICENSE
Apache 2.0
ORIGIN
git@github.com:google/nsync
commit ac5489682760393fe21bd2a8e038b528442412a7
Author: Mike Burrows <m3b@google.com>
Date: Wed Jun 1 16:47:52 2022 -0700
LOCAL CHANGES
- Time APIs were so good that they're now in libc
- Double linked list API was so good that it's now in libc
- Support Apple's ulock futexes which are internal but nicer than GCD
- Ensure resources such as POSIX semaphores are are released on fork.
- Modified *NSYNC to allocate waiter objects on the stack. We need it
because we use *NSYNC mutexes to implement POSIX mutexes, which are
too low-level to safely depend on malloc, or even mmap in our case.
- Rewrote most of the semaphore and futex system call support code so
it works well with Cosmopolitan's fat runtime portability. *NSYNC's
unit test suite passes on all supported platforms. However the BSDs
currently appear to be overutilizing CPU time compared with others.
This appears to be the fault of the OSes rather than *NSYNC / Cosmo
- Support POSIX thread cancellation. APIs that wait on condition vars
are now cancellation points. In PTHREAD_CANCEL_MASKED mode they may
return ECANCELED. In PTHREAD_CANCEL_DEFERRED mode the POSIX threads
library will unwind the stack to re-acquire locks and free waiters.
On the other hand the *NSYNC APIs for mutexes will now safely block
thread cancellation, but you can still use *NSYNC notes to do that.