cosmopolitan/libc/runtime
Jōshin d95d61b1af
Better zipos refcounts and atomic reads/seeks (#973)
* Better refcounting

Cribbed from [Rust Arc][1] and the [Boost docs][2]:

"""
Increasing the reference counter can always be done with
memory_order_relaxed: New references to an object can only be formed
from an existing reference, and passing an existing reference from one
thread to another must already provide any required synchronization.

It is important to enforce any possible access to the object in one
thread (through an existing reference) to happen before deleting the
object in a different thread. This is achieved by a "release" operation
after dropping a reference (any access to the object through this
reference must obviously happened before), and an "acquire" operation
before deleting the object.

It would be possible to use memory_order_acq_rel for the fetch_sub
operation, but this results in unneeded "acquire" operations when the
reference counter does not yet reach zero and may impose a performance
penalty.
"""

[1] https://moshg.github.io/rust-std-ja/src/alloc/arc.rs.html
[2] https://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html

* Make ZiposHandle's pos atomic

Implements a somewhat stronger guarantee than POSIX specifies: reads and
seeks are atomic. They may be arbitrarily reordered between threads, but
each one happens all the way and leaves the fd in a consistent state.

This is achieved by "locking" pos in __zipos_read by storing SIZE_MAX to
pos during the operation, so only one can be in-flight at a time. Seeks,
on the other hand, just update pos in one go, and rerun if it changed in
the meantime.

I used `LIKELY` / `UNLIKELY` to pessimize the concurrent case; hopefully
that buys back some performance.
2023-12-01 01:01:03 -08: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
BUILD.mk Rename makefiles BUILD.mk 2023-11-28 11:21:08 -08:00
clktck.h Reduce header complexity 2023-11-28 14:39:42 -08: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 Improve threading and i/o routines 2023-10-12 23:13:04 -07:00
closesymboltable.c Reduce makefile dependencies by 10% 2022-06-08 20:01:28 -07:00
cosmo.S Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
cosmo2.c Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
cxaguard.c Add more fixes for new cosmocc toolchain 2023-11-11 23:28:19 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
efipostboot.S Introduce native support for MacOS ARM64 2023-05-20 04:17:03 -07:00
enable_tls.c Put more thought into i/o polyfills 2023-11-07 18:32:35 -08:00
exit.c Add more fixes for new cosmocc toolchain 2023-11-11 23:28:19 -08:00
fenv.h Reduce header complexity 2023-11-28 14:39:42 -08: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
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 Make improvements 2023-10-15 16:45:00 -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-10-08 08:59:53 -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 Fix issues for latest GCC toolchain 2023-10-11 14:54:42 -07:00
getdosenviron.c Reduce header complexity 2023-11-28 14:39:42 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
getsysctl.c Fix small matters and improve sysconf() 2023-08-17 00:32:11 -07:00
grow.c Reduce header complexity 2023-11-28 14:39:42 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
interceptflag.greg.c Fix warnings 2023-09-01 20:50:18 -07:00
internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
isheap.c Remove bool from public headers 2023-11-15 20:58:46 -08:00
ismemtracked.greg.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
isstackoverflow.c Improve stack overflow recovery 2023-10-04 07:35:17 -07:00
jit.c Improve dlopen() on Apple Silicon 2023-11-17 02:33:14 -08:00
login_tty.c Fix bug in login_tty() 2023-10-31 18:03:13 -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 Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
memtrack64.txt Iterate more on recent changes 2023-11-11 00:28:22 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
mman.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
mmap.c Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
morph.c Remove _Hide keyword 2023-07-24 08:34:58 -07:00
morph_tls.c Reduce header complexity 2023-11-28 14:39:42 -08:00
mprotect-nt.greg.c Rewrite special file handling on Windows 2023-10-14 02:53:34 -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 Make improvements 2023-10-08 08:59:53 -07:00
munmap.c Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08:00
openexecutable.c Replace COSMO define with _COSMO_SOURCE 2023-08-13 20:55:04 -07:00
opensymboltable.greg.c Reduce header complexity 2023-11-28 14:39:42 -08:00
pathconf.h Reduce header complexity 2023-11-28 14:39:42 -08:00
pc.internal.h [metal] Fix video mode filtering & frame buffer ref-counting (#889) 2023-09-06 03:41:07 -07:00
printgarbage.c Clean up some code 2023-11-16 17:31:07 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
set_tls.c Revert "Use %gs as TLS register when dlopen() is linked" 2023-11-08 01:33:01 -08:00
sigsetjmp.S Add siglongjmp() for aarch64 2023-09-21 10:10:20 -07:00
stack.h Reduce header complexity 2023-11-28 14:39:42 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
sysconf.c Make improvements 2023-09-18 21:04:47 -07:00
sysconf.h Reduce header complexity 2023-11-28 14:39:42 -08:00
syslib.internal.h Reduce header complexity 2023-11-28 14:39:42 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
utmp.c Stub out getutline and pututline 2023-10-31 21:58:39 -07:00
utmp.h Reduce header complexity 2023-11-28 14:39:42 -08:00
utmpx.h Reduce header complexity 2023-11-28 14:39:42 -08:00
valist.c Remove plenty of makefile misconfigurations 2022-07-21 09:20:59 -07:00
warnifpowersave.c Make improvements 2023-10-08 08:59:53 -07:00
weakfree.c Pay off more technical debt 2022-09-12 23:36:56 -07:00
winargs.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
winmain.greg.c Exclude strace from MODE=tiny builds 2023-11-29 03:45:54 -08: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 Implement __zipos_dup (#972) 2023-12-01 00:08:30 -08: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 Reduce header complexity 2023-11-28 14:39:42 -08:00
zipos-inode.c Rewrite special file handling on Windows 2023-10-14 02:53:34 -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 Better zipos refcounts and atomic reads/seeks (#973) 2023-12-01 01:01:03 -08:00
zipos-parseuri.c Introduce COSMOPOLITAN_DISABLE_ZIPOS environ var 2023-08-16 21:04:16 -07:00
zipos-read.c Better zipos refcounts and atomic reads/seeks (#973) 2023-12-01 01:01:03 -08:00
zipos-seek.c Better zipos refcounts and atomic reads/seeks (#973) 2023-12-01 01:01:03 -08: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 Better zipos refcounts and atomic reads/seeks (#973) 2023-12-01 01:01:03 -08:00
zipos.S Implement __zipos_dup (#972) 2023-12-01 00:08:30 -08:00