cosmopolitan/libc/runtime
Justine Tunney 85f64f3851
Make futexes 100x better on x86 MacOS
Thanks to @autumnjolitz (in ) 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 
2023-10-03 15:15:43 -07:00
..
.cosmo Move zipos into runtime package 2023-08-11 23:14:02 -07:00
__sigsetjmp_tail.c Add siglongjmp() for aarch64 2023-09-21 10:10:20 -07:00
clktck.c Make improvements 2023-10-03 06:17:16 -07:00
clktck.h Make major improvements to redbean and libraries 2021-04-18 12:34:15 -07:00
clone-linux.S Make improvements 2023-06-03 08:12:22 -07:00
clone-openbsd.S Pay off more technical debt 2022-09-12 23:36:56 -07:00
clone-xnu.S Make fixes and improvements 2022-11-08 10:11:46 -08:00
clone.c Make futexes 100x better on x86 MacOS 2023-10-03 15:15:43 -07:00
closesymboltable.c Reduce makefile dependencies by 10% 2022-06-08 20:01:28 -07:00
cosmo.S Remove old stack code and improve dirstream 2023-08-16 07:54:40 -07:00
cosmo2.c Make improvements 2023-09-18 21:04:47 -07:00
cxaguard.c Add support for C++ thread safe statics 2023-08-12 07:45:32 -07:00
dlfcn.h Get Fabrice Bellard's JavaScript engine to build 2021-04-09 01:06:57 -07:00
dsohandle.S Introduce --strace flag for system call tracing 2022-03-18 18:07:28 -07:00
e820.internal.h Support malloc() on bare metal 2021-02-24 00:53:24 -08:00
efimain.greg.c Make improvements 2023-09-18 21:04:47 -07:00
efipostboot.S Introduce native support for MacOS ARM64 2023-05-20 04:17:03 -07:00
enable_tls.c Make futexes 100x better on x86 MacOS 2023-10-03 15:15:43 -07:00
exit.c Make improvements for Actually Portable Emacs 2023-08-19 06:44:58 -07:00
fenv.h Make further progress on non-x86 support 2023-05-10 04:20:47 -07:00
fesetround.c Make more progress on aarch64 2023-05-10 04:20:47 -07:00
findcombinary.c Make improvements 2023-09-06 12:34:59 -07:00
finddebugbinary.c Make improvements 2023-09-18 21:04:47 -07:00
fltrounds.c Make progress towards aarch64 build 2023-05-10 04:20:46 -07:00
fpathconf.c Make more compatibility improvements 2022-09-06 12:36:34 -07:00
fpreset.S Get --ftrace working on aarch64 2023-06-05 23:35:31 -07:00
ftrace-hook.S Incorporate more small improvements 2023-07-23 10:57:18 -07:00
ftrace_install.c Incorporate more small improvements 2023-07-23 10:57:18 -07:00
ftrace_stackdigs.c Make fixes and improvements 2023-07-09 05:21:11 -07:00
ftraceinit.greg.c Make improvements 2023-09-21 07:30:39 -07:00
ftracer.c Make improvements 2023-09-18 21:04:47 -07:00
getargmax.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
getavphyspages.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
getdosargv.c Make improvements 2023-09-21 07:30:39 -07:00
getdosenviron.c Make improvements 2023-09-18 21:04:47 -07:00
getinterpreterexecutablename.c Fix warnings 2023-09-01 20:50:18 -07:00
getlogin.c Fix warnings 2023-09-01 20:50:18 -07:00
getlogin_r.c Emulate ENOTDIR better 2023-08-16 20:11:23 -07:00
getmemtracksize.c Make improvements 2023-09-18 21:04:47 -07:00
getpagesize.c Port a lot more code to AARCH64 2023-05-14 09:37:26 -07:00
getphyspages.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
getresourcelimit.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
getsymbol.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
getsymbolbyaddr.c Make improvements 2023-09-18 21:04:47 -07:00
getsymbolname.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
getsymboltable.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
getsysctl.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
grow.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
hook.greg.c Emulate ENOTDIR better 2023-08-16 20:11:23 -07:00
inflate.c Support non-blocking i/o across platforms 2023-07-23 02:56:47 -07:00
init.S Fix breakages in Linux-only build modes 2023-07-09 19:51:44 -07:00
interceptflag.greg.c Fix warnings 2023-09-01 20:50:18 -07:00
internal.h Make improvements 2023-09-18 21:04:47 -07:00
isheap.c Make improvements 2023-10-03 06:17:16 -07:00
ismemtracked.greg.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
isstackoverflow.c Make improvements 2023-09-18 21:04:47 -07:00
ldso.c Get Fabrice Bellard's JavaScript engine to build 2021-04-09 01:06:57 -07:00
login_tty.c Overhaul process spawning 2023-09-10 08:17:44 -07:00
mapanon.c Pay off more technical debt 2022-09-12 23:36:56 -07:00
mapshared.c Pay off more technical debt 2022-09-12 23:36:56 -07:00
mapstack.c Fix stack memory, undefined behavior, etc. 2023-08-15 19:10:08 -07:00
memtrack.internal.h Make improvements 2023-09-18 21:04:47 -07:00
memtrack64.txt Get rid of kmalloc() 2023-09-11 21:56:00 -07:00
memtracknt.c Make improvements 2023-09-18 21:04:47 -07:00
metalprintf.greg.c Remove _Hide keyword 2023-07-24 08:34:58 -07:00
metalprintf.internal.h Support malloc() on bare metal 2021-02-24 00:53:24 -08:00
mman.internal.h Run clang-format on most sources 2023-04-27 05:44:32 -07:00
mmap.c Make improvements 2023-09-21 07:30:39 -07:00
morph.c Remove _Hide keyword 2023-07-24 08:34:58 -07:00
morph_tls.c Remove _Hide keyword 2023-07-24 08:34:58 -07:00
mprotect-nt.greg.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
mprotect.c Make more fixes and improvements 2023-07-29 18:44:15 -07:00
msync-nt.c Make improvements 2023-09-18 21:04:47 -07:00
msync.c Fix msync() flags on FreeBSD 2023-08-21 04:15:05 -07:00
munmap.c Make improvements 2023-09-18 21:04:47 -07:00
openexecutable.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
opensymboltable.greg.c Incorporate more small improvements 2023-07-23 10:57:18 -07:00
pathconf.h Make more compatibility improvements 2022-09-06 12:36:34 -07:00
pc.internal.h [metal] Fix video mode filtering & frame buffer ref-counting () 2023-09-06 03:41:07 -07:00
printargs.c Make improvements 2023-09-18 21:04:47 -07:00
printgarbage.c Fix bugs in cosmocc toolchain 2023-06-08 23:44:03 -07:00
printmaps.c Clean up some of the threading code 2022-09-08 12:31:56 -07:00
progname.S Clean old .source directive out of asm code 2022-03-18 12:43:21 -07:00
runtime.h Make improvements 2023-10-03 06:17:16 -07:00
runtime.mk Add siglongjmp() for aarch64 2023-09-21 10:10:20 -07:00
set_tls.c Make improvements 2023-09-21 07:30:39 -07:00
sigsetjmp.S Add siglongjmp() for aarch64 2023-09-21 10:10:20 -07:00
stack.h Make improvements 2023-09-18 21:04:47 -07:00
straceinit.greg.c Give Emacs another performance boost 2023-08-18 09:34:14 -07:00
symbols.c Get Fat Emacs working on Apple Silicon 2023-08-17 22:01:42 -07:00
symbols.internal.h Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
sysconf.c Make improvements 2023-09-18 21:04:47 -07:00
sysconf.h Make improvements 2023-09-18 21:04:47 -07:00
syslib.internal.h Make improvements 2023-09-21 07:30:39 -07:00
untrackmemoryintervals.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
unwind.c Fix libunwind stubs 2022-06-26 05:29:25 -07:00
unwind.h Fix libunwind stubs 2022-06-26 05:29:25 -07:00
utmp.c Clean up more code 2023-06-18 01:00:05 -07:00
utmp.h Add finger demo to redbean and fix regression 2022-06-23 03:42:05 -07:00
utmpx.h Add finger demo to redbean and fix regression 2022-06-23 03:42:05 -07:00
valist.c Remove plenty of makefile misconfigurations 2022-07-21 09:20:59 -07:00
warnifpowersave.c Make improvements 2023-09-18 21:04:47 -07:00
weakfree.c Pay off more technical debt 2022-09-12 23:36:56 -07:00
winargs.internal.h Make improvements 2023-09-21 07:30:39 -07:00
winmain.greg.c Make improvements 2023-09-21 07:30:39 -07:00
zipos-access.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-close.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-fcntl.c Make ZipOS and Qemu work better 2023-08-15 18:32:50 -07:00
zipos-find.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-fstat.c Improve zip read-only filesystem 2023-08-16 17:52:12 -07:00
zipos-get.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-inode.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-mmap.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-normpath.c Make improvements 2023-08-21 02:34:17 -07:00
zipos-notat.c Move zipos into runtime package 2023-08-11 23:14:02 -07:00
zipos-open.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-parseuri.c Introduce COSMOPOLITAN_DISABLE_ZIPOS environ var 2023-08-16 21:04:16 -07:00
zipos-read.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-seek.c Make improvements 2023-09-18 21:04:47 -07:00
zipos-stat-impl.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos-stat.c Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos.internal.h Revert "Rewrite ZipOS" 2023-10-03 14:40:03 -07:00
zipos.S Make improvements for Actually Portable Emacs 2023-08-19 06:44:58 -07:00