cosmopolitan/libc/thread
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
..
alarm.c Make improvements 2023-09-18 21:04:47 -07:00
freebsd.internal.h Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
getitimer.c Make improvements 2023-09-18 21:04:47 -07:00
itimer.c Make improvements 2023-09-18 21:04:47 -07:00
itimer.internal.h Make improvements 2023-09-18 21:04:47 -07:00
makecontext.c Make improvements 2023-10-03 06:17:16 -07:00
mktls.c Make improvements 2023-09-18 21:04:47 -07:00
openbsd.internal.h Make fixes and improvements 2022-06-25 21:09:09 -07:00
posixthread.internal.h Make improvements 2023-09-18 21:04:47 -07:00
pthread_atfork.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_attr_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getdetachstate.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getguardsize.c Make more improvements to threading support 2022-10-09 00:08:47 -07:00
pthread_attr_getinheritsched.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getschedparam.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getschedpolicy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getscope.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_getsigmask_np.c Make improvements 2022-11-10 21:52:47 -08:00
pthread_attr_getstack.c Make more improvements to threading support 2022-10-09 00:08:47 -07:00
pthread_attr_getstacksize.c Make more improvements to threading support 2022-10-09 00:08:47 -07:00
pthread_attr_init.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
pthread_attr_setdetachstate.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_setguardsize.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_attr_setinheritsched.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_attr_setschedparam.c Make improvements 2022-11-10 21:52:47 -08:00
pthread_attr_setschedpolicy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_setscope.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_attr_setsigmask_np.c Fix some more issues with aarch64 and llama.cpp 2023-05-10 07:34:26 -07:00
pthread_attr_setstack.c Hunt down more bugs 2023-07-03 18:43:29 -07:00
pthread_attr_setstacksize.c Make more improvements to threading support 2022-10-09 00:08:47 -07:00
pthread_barrier_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_barrier_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_barrier_wait.c Improve system call wrappers 2022-09-19 15:06:25 -07:00
pthread_barrierattr_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_barrierattr_getpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_barrierattr_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_barrierattr_setpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_cancel.c Make improvements 2023-09-21 07:30:39 -07:00
pthread_cond_broadcast.c Clean up some code 2022-12-11 14:30:59 -08:00
pthread_cond_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_cond_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_cond_signal.c Improve system call wrappers 2022-09-19 15:06:25 -07:00
pthread_cond_timedwait.c Improve cancellations, randomness, and time 2022-11-05 23:45:32 -07:00
pthread_cond_wait.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_condattr_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_condattr_getpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_condattr_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_condattr_setpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_create.c Make improvements 2023-10-03 06:17:16 -07:00
pthread_decimate.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_detach.c Make improvements 2023-10-03 06:17:16 -07:00
pthread_equal.c Improve cosmo's conformance to libc-test 2022-10-10 17:52:41 -07:00
pthread_exit.c Make futexes 100x better on x86 MacOS 2023-10-03 15:15:43 -07:00
pthread_getaffinity_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_getattr_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_getname_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_getschedparam.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_getspecific.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_getthreadid_np.c Tidy up the threading implementation 2022-09-10 02:56:25 -07:00
pthread_getunique_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_join.c Make improvements 2023-10-03 06:17:16 -07:00
pthread_key_create.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_key_delete.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_keys.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_kill.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_once.c Make more fixes and improvements 2023-07-29 18:44:15 -07:00
pthread_orphan_np.c Make improvements 2022-11-10 21:52:47 -08:00
pthread_reschedule.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_rwlock_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_rwlock_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_rwlock_rdlock.c Improve system call wrappers 2022-09-19 15:06:25 -07:00
pthread_rwlock_unlock.c Improve system call wrappers 2022-09-19 15:06:25 -07:00
pthread_rwlock_wrlock.c Improve system call wrappers 2022-09-19 15:06:25 -07:00
pthread_rwlockattr_destroy.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_rwlockattr_getpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_rwlockattr_init.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_rwlockattr_setpshared.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_self.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_setaffinity_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_setcanceltype.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_setname_np.c Make improvements 2023-09-18 21:04:47 -07:00
pthread_setschedparam.c Make improvements 2022-11-10 21:52:47 -08:00
pthread_setschedparam_freebsd.c Add sys_ prefix to unwrapped system calls 2022-09-13 11:20:35 -07:00
pthread_setschedprio.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_sigmask.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
pthread_timedjoin_np.c Make futexes 100x better on x86 MacOS 2023-10-03 15:15:43 -07:00
pthread_tryjoin_np.c Make improvements 2023-10-03 06:17:16 -07:00
pthread_ungarbage.c Fix warnings 2023-09-01 20:50:18 -07:00
pthread_zombify.c Make improvements 2023-09-18 21:04:47 -07:00
README.md Use *NSYNC for POSIX threads locking APIs 2022-09-11 11:04:50 -07:00
sem_destroy.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
sem_getvalue.c Make POSIX semaphores always process shared 2023-02-23 08:07:54 -08:00
sem_init.c Make improvements 2022-11-07 02:26:06 -08:00
sem_open.c Make improvements 2023-10-03 06:17:16 -07:00
sem_path_np.c Remove Windows executable path guessing logic 2023-09-21 08:13:50 -07:00
sem_post.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
sem_timedwait.c Fix warnings 2023-09-01 20:50:18 -07:00
sem_trywait.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
sem_wait.c Make futexes cancellable by pthreads 2022-11-04 18:36:34 -07:00
semaphore.h Implement pthread_atfork() 2022-10-16 12:25:13 -07:00
setitimer.c Make improvements 2023-09-18 21:04:47 -07:00
thread.h Make improvements 2023-09-18 21:04:47 -07:00
thread.mk Make improvements 2023-09-18 21:04:47 -07:00
thread2.h Make improvements 2022-11-10 21:52:47 -08:00
tls.h Make improvements 2023-10-03 06:17:16 -07:00
tls2.internal.h Make improvements 2023-09-18 21:04:47 -07:00
ualarm.c Make improvements 2023-09-18 21:04:47 -07:00
xnu.internal.h Make fixes and improvements 2022-11-08 10:11:46 -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.