From 22b63d0b98b089fbcf4f1af8a463aa09db49c9ce Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Tue, 13 Sep 2022 01:46:29 -0700 Subject: [PATCH] Add *NSYNC to libc/isystem --- Makefile | 164 +++++++++--------- build/rules.mk | 16 +- examples/examples.mk | 1 + libc/isystem/nsync.h | 11 ++ libc/isystem/nsync_atomic.h | 4 + libc/isystem/nsync_counter.h | 4 + libc/isystem/nsync_cv.h | 4 + libc/isystem/nsync_debug.h | 4 + libc/isystem/nsync_mu.h | 4 + libc/isystem/nsync_mu_wait.h | 4 + libc/isystem/nsync_note.h | 4 + libc/isystem/nsync_once.h | 4 + libc/isystem/nsync_time.h | 4 + libc/isystem/nsync_waiter.h | 4 + libc/thread/thread.mk | 3 +- test/libc/release/test.mk | 2 +- test/libc/thread/test.mk | 3 +- third_party/nsync/README.md | 9 +- third_party/nsync/compat.S | 43 +++++ third_party/nsync/futex.c | 44 +++-- third_party/nsync/mem/mem.mk | 55 ++++++ third_party/nsync/mem/nsync.mk | 0 .../nsync/mem}/nsync_counter.c | 0 .../nsync/mem}/nsync_cv.c | 0 .../nsync/mem}/nsync_debug.c | 0 .../nsync/mem}/nsync_mu_wait.c | 0 .../nsync/mem}/nsync_note.c | 0 .../nsync/mem}/nsync_once.c | 0 .../nsync/mem}/nsync_sem_wait.c | 0 .../nsync/mem}/nsync_sem_wait_no_note.c | 0 .../nsync/mem}/nsync_wait.c | 0 third_party/nsync/nsync.mk | 14 +- 32 files changed, 286 insertions(+), 119 deletions(-) create mode 100644 libc/isystem/nsync.h create mode 100644 libc/isystem/nsync_atomic.h create mode 100644 libc/isystem/nsync_counter.h create mode 100644 libc/isystem/nsync_cv.h create mode 100644 libc/isystem/nsync_debug.h create mode 100644 libc/isystem/nsync_mu.h create mode 100644 libc/isystem/nsync_mu_wait.h create mode 100644 libc/isystem/nsync_note.h create mode 100644 libc/isystem/nsync_once.h create mode 100644 libc/isystem/nsync_time.h create mode 100644 libc/isystem/nsync_waiter.h create mode 100644 third_party/nsync/compat.S create mode 100644 third_party/nsync/mem/mem.mk create mode 100755 third_party/nsync/mem/nsync.mk rename {libc/thread => third_party/nsync/mem}/nsync_counter.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_cv.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_debug.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_mu_wait.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_note.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_once.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_sem_wait.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_sem_wait_no_note.c (100%) rename {libc/thread => third_party/nsync/mem}/nsync_wait.c (100%) diff --git a/Makefile b/Makefile index a8c92725b..c26e2a2d5 100644 --- a/Makefile +++ b/Makefile @@ -128,6 +128,7 @@ include libc/mem/mem.mk #─┐ include libc/zipos/zipos.mk # ├──DYNAMIC RUNTIME include third_party/gdtoa/gdtoa.mk # │ You can now use stdio include libc/time/time.mk # │ You can finally call malloc() +include third_party/nsync/mem/mem.mk # │ include libc/thread/thread.mk # │ include libc/stdio/stdio.mk # │ include third_party/libcxx/libcxx.mk # │ @@ -265,98 +266,101 @@ o/$(MODE)/srcs-old.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(SRCS o/$(MODE)/hdrs-old.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(HDRS) $(INCS),$(dir $(x)))) $(file >$@) $(foreach x,$(HDRS) $(INCS),$(file >>$@,$(x))) -TAGS: .UNSANDBOXED = 1 +TAGS: private .UNSANDBOXED = 1 TAGS: o/$(MODE)/srcs-old.txt $(SRCS) @$(RM) $@ @$(TAGS) $(TAGSFLAGS) -L $< -o $@ -HTAGS: .UNSANDBOXED = 1 +HTAGS: private .UNSANDBOXED = 1 HTAGS: o/$(MODE)/hdrs-old.txt $(HDRS) @$(RM) $@ @build/htags -L $< -o $@ -loc: .UNSANDBOXED = 1 +loc: private .UNSANDBOXED = 1 loc: o/$(MODE)/tool/build/summy.com find -name \*.h -or -name \*.c -or -name \*.S | \ $(XARGS) wc -l | grep total | awk '{print $$1}' | $< -COSMOPOLITAN_OBJECTS = \ - NET_HTTP \ - LIBC_DNS \ - LIBC_SOCK \ - LIBC_NT_WS2_32 \ - LIBC_NT_IPHLPAPI \ - LIBC_NT_MSWSOCK \ - LIBC_X \ - THIRD_PARTY_GETOPT \ - LIBC_LOG \ - LIBC_TIME \ - LIBC_ZIPOS \ - THIRD_PARTY_ZLIB \ - THIRD_PARTY_MUSL \ - LIBC_STDIO \ - THIRD_PARTY_GDTOA \ - THIRD_PARTY_REGEX \ - LIBC_MEM \ - THIRD_PARTY_DLMALLOC \ - LIBC_RUNTIME \ - THIRD_PARTY_NSYNC \ - LIBC_ELF \ - LIBC_CALLS \ - LIBC_SYSV_CALLS \ - LIBC_NT_PSAPI \ - LIBC_NT_POWRPROF \ - LIBC_NT_PDH \ - LIBC_NT_GDI32 \ - LIBC_NT_COMDLG32 \ - LIBC_NT_URL \ - LIBC_NT_USER32 \ - LIBC_NT_NTDLL \ - LIBC_NT_ADVAPI32 \ - LIBC_FMT \ - THIRD_PARTY_COMPILER_RT \ - LIBC_THREAD \ - LIBC_TINYMATH \ - THIRD_PARTY_XED \ - LIBC_STR \ - LIBC_SYSV \ - LIBC_INTRIN \ - LIBC_NT_KERNEL32 \ - LIBC_NEXGEN32E \ - LIBC_VGA +# PLEASE: MAINTAIN TOPOLOGICAL ORDER +# FROM HIGHEST LEVEL TO LOWEST LEVEL +COSMOPOLITAN_OBJECTS = \ + NET_HTTP \ + LIBC_DNS \ + LIBC_SOCK \ + LIBC_NT_WS2_32 \ + LIBC_NT_IPHLPAPI \ + LIBC_NT_MSWSOCK \ + LIBC_X \ + THIRD_PARTY_GETOPT \ + LIBC_LOG \ + LIBC_TIME \ + LIBC_ZIPOS \ + THIRD_PARTY_ZLIB \ + THIRD_PARTY_MUSL \ + LIBC_STDIO \ + THIRD_PARTY_GDTOA \ + THIRD_PARTY_REGEX \ + LIBC_THREAD \ + THIRD_PARTY_NSYNC_MALLOC \ + LIBC_MEM \ + THIRD_PARTY_DLMALLOC \ + LIBC_RUNTIME \ + THIRD_PARTY_NSYNC \ + LIBC_ELF \ + LIBC_CALLS \ + LIBC_SYSV_CALLS \ + LIBC_VGA \ + LIBC_NT_PSAPI \ + LIBC_NT_POWRPROF \ + LIBC_NT_PDH \ + LIBC_NT_GDI32 \ + LIBC_NT_COMDLG32 \ + LIBC_NT_URL \ + LIBC_NT_USER32 \ + LIBC_NT_NTDLL \ + LIBC_NT_ADVAPI32 \ + LIBC_FMT \ + THIRD_PARTY_COMPILER_RT \ + LIBC_TINYMATH \ + THIRD_PARTY_XED \ + LIBC_STR \ + LIBC_SYSV \ + LIBC_INTRIN \ + LIBC_NT_KERNEL32 \ + LIBC_NEXGEN32E -COSMOPOLITAN_HEADERS = \ - APE \ - LIBC \ - LIBC_CALLS \ - LIBC_DNS \ - LIBC_ELF \ - LIBC_FMT \ - LIBC_INTRIN \ - LIBC_LOG \ - LIBC_MEM \ - LIBC_NEXGEN32E \ - LIBC_NT \ - LIBC_RUNTIME \ - LIBC_SOCK \ - LIBC_STDIO \ - THIRD_PARTY_NSYNC \ - THIRD_PARTY_XED \ - LIBC_STR \ - LIBC_SYSV \ - LIBC_THREAD \ - LIBC_TIME \ - LIBC_TINYMATH \ - LIBC_X \ - LIBC_ZIPOS \ - LIBC_VGA \ - NET_HTTP \ - THIRD_PARTY_DLMALLOC \ - THIRD_PARTY_GDTOA \ - THIRD_PARTY_GETOPT \ - THIRD_PARTY_MUSL \ - THIRD_PARTY_ZLIB \ - THIRD_PARTY_ZLIB_GZ \ +COSMOPOLITAN_HEADERS = \ + APE \ + LIBC \ + LIBC_CALLS \ + LIBC_DNS \ + LIBC_ELF \ + LIBC_FMT \ + LIBC_INTRIN \ + LIBC_LOG \ + LIBC_MEM \ + LIBC_NEXGEN32E \ + LIBC_NT \ + LIBC_RUNTIME \ + LIBC_SOCK \ + LIBC_STDIO \ + THIRD_PARTY_NSYNC \ + THIRD_PARTY_XED \ + LIBC_STR \ + LIBC_SYSV \ + LIBC_THREAD \ + LIBC_TIME \ + LIBC_TINYMATH \ + LIBC_X \ + LIBC_ZIPOS \ + LIBC_VGA \ + NET_HTTP \ + THIRD_PARTY_DLMALLOC \ + THIRD_PARTY_GDTOA \ + THIRD_PARTY_GETOPT \ + THIRD_PARTY_MUSL \ + THIRD_PARTY_ZLIB \ + THIRD_PARTY_ZLIB_GZ \ THIRD_PARTY_REGEX o/$(MODE)/cosmopolitan.a: \ @@ -370,7 +374,7 @@ o/cosmopolitan.h: \ $(file >$(TMPDIR)/$(subst /,_,$@),libc/integral/normalize.inc $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))) @$(COMPILE) -AROLLUP -T$@ o/$(MODE)/tool/build/rollup.com @$(TMPDIR)/$(subst /,_,$@) >$@ -o/cosmopolitan.html: .UNSANDBOXED = 1 +o/cosmopolitan.html: private .UNSANDBOXED = 1 o/cosmopolitan.html: \ o/$(MODE)/third_party/chibicc/chibicc.com.dbg \ $(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))) \ diff --git a/build/rules.mk b/build/rules.mk index 17c964582..e93470a88 100644 --- a/build/rules.mk +++ b/build/rules.mk @@ -163,15 +163,15 @@ o/$(MODE)%/.zip.o: % # and it would be too costly in terms of make latency to have every # header file depend on $(HDRS) and $(INCS). -o/%.h.ok: .UNSANDBOXED = 1 +o/%.h.ok: private .UNSANDBOXED = 1 o/%.h.ok: %.h @$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $< -o/$(MODE)/%.h.ok: .UNSANDBOXED = 1 +o/$(MODE)/%.h.ok: private .UNSANDBOXED = 1 o/$(MODE)/%.h.ok: %.h @$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -g0 -o $@ $< -o/$(MODE)/%.hh.ok: .UNSANDBOXED = 1 +o/$(MODE)/%.hh.ok: private .UNSANDBOXED = 1 o/$(MODE)/%.hh.ok: %.hh @$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $< @@ -179,7 +179,7 @@ o/%.okk: .UNSANDBOXED = 1 o/%.okk: % @$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $< -o/$(MODE)/%.okk: .UNSANDBOXED = 1 +o/$(MODE)/%.okk: private .UNSANDBOXED = 1 o/$(MODE)/%.okk: % @$(COMPILE) -ACHECK.h $(COMPILE.cxx) -xc++ -g0 -o $@ $< @@ -207,19 +207,19 @@ MAKE_SYMTAB_ZIP = \ ################################################################################ # EMACS ASSEMBLY GENERATION -o/$(MODE)/%-gcc.asm: .UNSANDBOXED = 1 +o/$(MODE)/%-gcc.asm: private .UNSANDBOXED = 1 o/$(MODE)/%-gcc.asm: %.c @$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $< -o/$(MODE)/%-gcc.asm: .UNSANDBOXED = 1 +o/$(MODE)/%-gcc.asm: private .UNSANDBOXED = 1 o/$(MODE)/%-gcc.asm: %.cc @$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.cxx) -S -g0 $(OUTPUT_OPTION) $< -o/$(MODE)/%-clang.asm: .UNSANDBOXED = 1 +o/$(MODE)/%-clang.asm: private .UNSANDBOXED = 1 o/$(MODE)/%-clang.asm: %.c @$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $< o/$(MODE)/%-clang.asm: CC = $(CLANG) -o/$(MODE)/%-clang.asm: .UNSANDBOXED = 1 +o/$(MODE)/%-clang.asm: private .UNSANDBOXED = 1 o/$(MODE)/%-clang.asm: %.cc @$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.cxx) -S -g0 $(OUTPUT_OPTION) $< diff --git a/examples/examples.mk b/examples/examples.mk index d18fe533d..751227759 100644 --- a/examples/examples.mk +++ b/examples/examples.mk @@ -81,6 +81,7 @@ EXAMPLES_DIRECTDEPS = \ THIRD_PARTY_MBEDTLS \ THIRD_PARTY_MUSL \ THIRD_PARTY_NSYNC \ + THIRD_PARTY_NSYNC_MEM \ THIRD_PARTY_QUICKJS \ THIRD_PARTY_STB \ THIRD_PARTY_XED \ diff --git a/libc/isystem/nsync.h b/libc/isystem/nsync.h new file mode 100644 index 000000000..5da91c187 --- /dev/null +++ b/libc/isystem/nsync.h @@ -0,0 +1,11 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_H_ +#include "third_party/nsync/counter.h" +#include "third_party/nsync/cv.h" +#include "third_party/nsync/debug.h" +#include "third_party/nsync/mu.h" +#include "third_party/nsync/mu_wait.h" +#include "third_party/nsync/note.h" +#include "third_party/nsync/once.h" +#include "third_party/nsync/waiter.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_H_ */ diff --git a/libc/isystem/nsync_atomic.h b/libc/isystem/nsync_atomic.h new file mode 100644 index 000000000..75410c321 --- /dev/null +++ b/libc/isystem/nsync_atomic.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ATOMIC_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ATOMIC_H_ +#include "third_party/nsync/atomic.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ATOMIC_H_ */ diff --git a/libc/isystem/nsync_counter.h b/libc/isystem/nsync_counter.h new file mode 100644 index 000000000..6dbda4dc0 --- /dev/null +++ b/libc/isystem/nsync_counter.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_COUNTER_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_COUNTER_H_ +#include "third_party/nsync/counter.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_COUNTER_H_ */ diff --git a/libc/isystem/nsync_cv.h b/libc/isystem/nsync_cv.h new file mode 100644 index 000000000..88558e345 --- /dev/null +++ b/libc/isystem/nsync_cv.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_CV_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_CV_H_ +#include "third_party/nsync/cv.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_CV_H_ */ diff --git a/libc/isystem/nsync_debug.h b/libc/isystem/nsync_debug.h new file mode 100644 index 000000000..8db7e645f --- /dev/null +++ b/libc/isystem/nsync_debug.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_DEBUG_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_DEBUG_H_ +#include "third_party/nsync/debug.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_DEBUG_H_ */ diff --git a/libc/isystem/nsync_mu.h b/libc/isystem/nsync_mu.h new file mode 100644 index 000000000..166450452 --- /dev/null +++ b/libc/isystem/nsync_mu.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_H_ +#include "third_party/nsync/mu.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_H_ */ diff --git a/libc/isystem/nsync_mu_wait.h b/libc/isystem/nsync_mu_wait.h new file mode 100644 index 000000000..bc2f3a09b --- /dev/null +++ b/libc/isystem/nsync_mu_wait.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_WAIT_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_WAIT_H_ +#include "third_party/nsync/mu_wait.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_MU_WAIT_H_ */ diff --git a/libc/isystem/nsync_note.h b/libc/isystem/nsync_note.h new file mode 100644 index 000000000..11116db22 --- /dev/null +++ b/libc/isystem/nsync_note.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_NOTE_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_NOTE_H_ +#include "third_party/nsync/note.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_NOTE_H_ */ diff --git a/libc/isystem/nsync_once.h b/libc/isystem/nsync_once.h new file mode 100644 index 000000000..3073d2ece --- /dev/null +++ b/libc/isystem/nsync_once.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ONCE_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ONCE_H_ +#include "third_party/nsync/once.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_ONCE_H_ */ diff --git a/libc/isystem/nsync_time.h b/libc/isystem/nsync_time.h new file mode 100644 index 000000000..11375b474 --- /dev/null +++ b/libc/isystem/nsync_time.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_TIME_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_TIME_H_ +#include "third_party/nsync/time.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_TIME_H_ */ diff --git a/libc/isystem/nsync_waiter.h b/libc/isystem/nsync_waiter.h new file mode 100644 index 000000000..ceb831a95 --- /dev/null +++ b/libc/isystem/nsync_waiter.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_WAITER_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_WAITER_H_ +#include "third_party/nsync/waiter.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NSYNC_WAITER_H_ */ diff --git a/libc/thread/thread.mk b/libc/thread/thread.mk index d32ebd6ce..d2d555dad 100644 --- a/libc/thread/thread.mk +++ b/libc/thread/thread.mk @@ -34,7 +34,8 @@ LIBC_THREAD_A_DIRECTDEPS = \ LIBC_SYSV \ LIBC_SYSV_CALLS \ LIBC_NEXGEN32E \ - THIRD_PARTY_NSYNC + THIRD_PARTY_NSYNC \ + THIRD_PARTY_NSYNC_MEM LIBC_THREAD_A_DEPS := \ $(call uniq,$(foreach x,$(LIBC_THREAD_A_DIRECTDEPS),$($(x)))) diff --git a/test/libc/release/test.mk b/test/libc/release/test.mk index 23c3393e1..284042300 100644 --- a/test/libc/release/test.mk +++ b/test/libc/release/test.mk @@ -1,7 +1,7 @@ #-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ -o/$(MODE)/test/libc/release/cosmopolitan.zip: .UNSANDBOXED = 1 +o/$(MODE)/test/libc/release/cosmopolitan.zip: private .UNSANDBOXED = 1 o/$(MODE)/test/libc/release/cosmopolitan.zip: \ o/cosmopolitan.h \ o/$(MODE)/ape/public/ape.lds \ diff --git a/test/libc/thread/test.mk b/test/libc/thread/test.mk index 962a66a96..ed6621e56 100644 --- a/test/libc/thread/test.mk +++ b/test/libc/thread/test.mk @@ -35,7 +35,8 @@ TEST_LIBC_THREAD_DIRECTDEPS = \ LIBC_THREAD \ LIBC_TIME \ LIBC_TESTLIB \ - THIRD_PARTY_NSYNC + THIRD_PARTY_NSYNC \ + THIRD_PARTY_NSYNC_MEM TEST_LIBC_THREAD_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_THREAD_DIRECTDEPS),$($(x)))) diff --git a/third_party/nsync/README.md b/third_party/nsync/README.md index 12dadf03b..dfcf1c586 100644 --- a/third_party/nsync/README.md +++ b/third_party/nsync/README.md @@ -1,7 +1,12 @@ # *NSYNC -The `THIRD_PARTY_NSYNC` and `LIBC_THREAD` packages include source code -from *NSYNC. Here's the latest upstream synchronization point: +*NSYNC is a library providing scalable synchronization primitives. The +following packages are provided: + + - `THIRD_PARTY_NSYNC` has `nsync_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) diff --git a/third_party/nsync/compat.S b/third_party/nsync/compat.S new file mode 100644 index 000000000..5cdeee4bc --- /dev/null +++ b/third_party/nsync/compat.S @@ -0,0 +1,43 @@ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/macros.internal.h" + +nsync_time_now: + jmp _timespec_real + .endfn nsync_time_now,globl + +nsync_time_add: + jmp _timespec_add + .endfn nsync_time_add,globl + +nsync_time_sub: + jmp _timespec_sub + .endfn nsync_time_sub,globl + +nsync_time_cmp: + jmp _timespec_cmp + .endfn nsync_time_cmp,globl + +nsync_time_ms: + jmp _timespec_frommillis + .endfn nsync_time_ms,globl + +nsync_time_us: + jmp _timespec_frommicros + .endfn nsync_time_us,globl diff --git a/third_party/nsync/futex.c b/third_party/nsync/futex.c index c7be2127b..da7ed4da4 100644 --- a/third_party/nsync/futex.c +++ b/third_party/nsync/futex.c @@ -16,11 +16,11 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/strace.internal.h" #include "libc/calls/struct/timespec.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/describeflags.internal.h" +#include "libc/intrin/strace.internal.h" #include "libc/sysv/consts/futex.h" #include "libc/thread/thread.h" #include "third_party/nsync/common.internal.h" @@ -34,30 +34,15 @@ int _futex (int *, int, int, const struct timespec *, int *, int); static int FUTEX_WAIT_; -static int FUTEX_WAKE_; static int FUTEX_PRIVATE_FLAG_; static bool FUTEX_IS_SUPPORTED; bool FUTEX_TIMEOUT_IS_ABSOLUTE; -__attribute__((__constructor__)) static void sync_futex_init_ (void) { +__attribute__((__constructor__)) static void nsync_futex_init_ (void) { int x = 0; - FUTEX_WAKE_ = FUTEX_WAKE; - - if (IsLinux () && - _futex (&x, FUTEX_WAIT_BITSET, 1, 0, 0, - FUTEX_BITSET_MATCH_ANY) == -EAGAIN) { - FUTEX_WAIT_ = FUTEX_WAIT_BITSET; - FUTEX_TIMEOUT_IS_ABSOLUTE = true; - } else { - FUTEX_WAIT_ = FUTEX_WAIT; - } - - if (IsOpenbsd () || - (IsLinux () && - !_futex (&x, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0))) { - FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG; - } + if (!(FUTEX_IS_SUPPORTED = IsLinux() || IsOpenbsd())) + return; // In our testing, we found that the monotonic clock on various // popular systems (such as Linux, and some BSD variants) was no @@ -72,10 +57,23 @@ __attribute__((__constructor__)) static void sync_futex_init_ (void) { if (IsLinux () && _futex (&x, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, 1, 0, 0, FUTEX_BITSET_MATCH_ANY) == -EAGAIN) { - FUTEX_WAIT_ |= FUTEX_CLOCK_REALTIME; + FUTEX_WAIT_ = FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME; + FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG; + FUTEX_TIMEOUT_IS_ABSOLUTE = true; + } else if (IsLinux () && + _futex (&x, FUTEX_WAIT_BITSET, 1, 0, 0, + FUTEX_BITSET_MATCH_ANY) == -EAGAIN) { + FUTEX_WAIT_ = FUTEX_WAIT_BITSET; + FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG; + FUTEX_TIMEOUT_IS_ABSOLUTE = true; + } else if (IsOpenbsd () || + (IsLinux () && + !_futex (&x, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0))) { + FUTEX_WAIT_ = FUTEX_WAIT; + FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG; + } else { + FUTEX_WAIT_ = FUTEX_WAIT; } - - FUTEX_IS_SUPPORTED = IsLinux() || IsOpenbsd(); } int nsync_futex_wait_ (int *p, int expect, char pshare, struct timespec *timeout) { @@ -108,7 +106,7 @@ int nsync_futex_wake_ (int *p, int count, char pshare) { int rc, op; int wake (void *, int, int) asm ("_futex"); if (FUTEX_IS_SUPPORTED) { - op = FUTEX_WAKE_; + op = FUTEX_WAKE; if (pshare == PTHREAD_PROCESS_PRIVATE) { op |= FUTEX_PRIVATE_FLAG_; } diff --git a/third_party/nsync/mem/mem.mk b/third_party/nsync/mem/mem.mk new file mode 100644 index 000000000..1b01ce9da --- /dev/null +++ b/third_party/nsync/mem/mem.mk @@ -0,0 +1,55 @@ +#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ +#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ + +PKGS += THIRD_PARTY_NSYNC_MEM + +THIRD_PARTY_NSYNC_MEM_SRCS = $(THIRD_PARTY_NSYNC_MEM_A_SRCS) +THIRD_PARTY_NSYNC_MEM_HDRS = $(THIRD_PARTY_NSYNC_MEM_A_HDRS) + +THIRD_PARTY_NSYNC_MEM_ARTIFACTS += THIRD_PARTY_NSYNC_MEM_A +THIRD_PARTY_NSYNC_MEM = $(THIRD_PARTY_NSYNC_MEM_A_DEPS) $(THIRD_PARTY_NSYNC_MEM_A) +THIRD_PARTY_NSYNC_MEM_A = o/$(MODE)/third_party/nsync/mem/nsync.a +THIRD_PARTY_NSYNC_MEM_A_FILES := $(wildcard third_party/nsync/mem/*) +THIRD_PARTY_NSYNC_MEM_A_HDRS = $(filter %.h,$(THIRD_PARTY_NSYNC_MEM_A_FILES)) +THIRD_PARTY_NSYNC_MEM_A_SRCS = $(filter %.c,$(THIRD_PARTY_NSYNC_MEM_A_FILES)) +THIRD_PARTY_NSYNC_MEM_A_OBJS = $(THIRD_PARTY_NSYNC_MEM_A_SRCS:%.c=o/$(MODE)/%.o) + +THIRD_PARTY_NSYNC_MEM_A_DIRECTDEPS = \ + LIBC_CALLS \ + LIBC_INTRIN \ + LIBC_NEXGEN32E \ + LIBC_MEM \ + LIBC_STUBS \ + LIBC_SYSV \ + THIRD_PARTY_NSYNC + +THIRD_PARTY_NSYNC_MEM_A_DEPS := \ + $(call uniq,$(foreach x,$(THIRD_PARTY_NSYNC_MEM_A_DIRECTDEPS),$($(x)))) + +THIRD_PARTY_NSYNC_MEM_A_CHECKS = \ + $(THIRD_PARTY_NSYNC_MEM_A).pkg \ + $(THIRD_PARTY_NSYNC_MEM_A_HDRS:%=o/$(MODE)/%.ok) + +$(THIRD_PARTY_NSYNC_MEM_A): \ + third_party/nsync/mem/ \ + $(THIRD_PARTY_NSYNC_MEM_A).pkg \ + $(THIRD_PARTY_NSYNC_MEM_A_OBJS) + +$(THIRD_PARTY_NSYNC_MEM_A).pkg: \ + $(THIRD_PARTY_NSYNC_MEM_A_OBJS) \ + $(foreach x,$(THIRD_PARTY_NSYNC_MEM_A_DIRECTDEPS),$($(x)_A).pkg) + +$(THIRD_PARTY_NSYNC_MEM_A_OBJS): private \ + OVERRIDE_CCFLAGS += \ + -ffunction-sections \ + -fdata-sections + +THIRD_PARTY_NSYNC_MEM_LIBS = $(foreach x,$(THIRD_PARTY_NSYNC_MEM_ARTIFACTS),$($(x))) +THIRD_PARTY_NSYNC_MEM_SRCS = $(foreach x,$(THIRD_PARTY_NSYNC_MEM_ARTIFACTS),$($(x)_SRCS)) +THIRD_PARTY_NSYNC_MEM_CHECKS = $(foreach x,$(THIRD_PARTY_NSYNC_MEM_ARTIFACTS),$($(x)_CHECKS)) +THIRD_PARTY_NSYNC_MEM_OBJS = $(foreach x,$(THIRD_PARTY_NSYNC_MEM_ARTIFACTS),$($(x)_OBJS)) +$(THIRD_PARTY_NSYNC_MEM_OBJS): third_party/nsync/mem/nsync.mk + +.PHONY: o/$(MODE)/third_party/nsync/mem +o/$(MODE)/third_party/nsync/mem: $(THIRD_PARTY_NSYNC_MEM_CHECKS) + diff --git a/third_party/nsync/mem/nsync.mk b/third_party/nsync/mem/nsync.mk new file mode 100755 index 000000000..e69de29bb diff --git a/libc/thread/nsync_counter.c b/third_party/nsync/mem/nsync_counter.c similarity index 100% rename from libc/thread/nsync_counter.c rename to third_party/nsync/mem/nsync_counter.c diff --git a/libc/thread/nsync_cv.c b/third_party/nsync/mem/nsync_cv.c similarity index 100% rename from libc/thread/nsync_cv.c rename to third_party/nsync/mem/nsync_cv.c diff --git a/libc/thread/nsync_debug.c b/third_party/nsync/mem/nsync_debug.c similarity index 100% rename from libc/thread/nsync_debug.c rename to third_party/nsync/mem/nsync_debug.c diff --git a/libc/thread/nsync_mu_wait.c b/third_party/nsync/mem/nsync_mu_wait.c similarity index 100% rename from libc/thread/nsync_mu_wait.c rename to third_party/nsync/mem/nsync_mu_wait.c diff --git a/libc/thread/nsync_note.c b/third_party/nsync/mem/nsync_note.c similarity index 100% rename from libc/thread/nsync_note.c rename to third_party/nsync/mem/nsync_note.c diff --git a/libc/thread/nsync_once.c b/third_party/nsync/mem/nsync_once.c similarity index 100% rename from libc/thread/nsync_once.c rename to third_party/nsync/mem/nsync_once.c diff --git a/libc/thread/nsync_sem_wait.c b/third_party/nsync/mem/nsync_sem_wait.c similarity index 100% rename from libc/thread/nsync_sem_wait.c rename to third_party/nsync/mem/nsync_sem_wait.c diff --git a/libc/thread/nsync_sem_wait_no_note.c b/third_party/nsync/mem/nsync_sem_wait_no_note.c similarity index 100% rename from libc/thread/nsync_sem_wait_no_note.c rename to third_party/nsync/mem/nsync_sem_wait_no_note.c diff --git a/libc/thread/nsync_wait.c b/third_party/nsync/mem/nsync_wait.c similarity index 100% rename from libc/thread/nsync_wait.c rename to third_party/nsync/mem/nsync_wait.c diff --git a/third_party/nsync/nsync.mk b/third_party/nsync/nsync.mk index 4402fa12d..efc01d64f 100644 --- a/third_party/nsync/nsync.mk +++ b/third_party/nsync/nsync.mk @@ -11,10 +11,16 @@ THIRD_PARTY_NSYNC = $(THIRD_PARTY_NSYNC_A_DEPS) $(THIRD_PARTY_NSYNC_A) THIRD_PARTY_NSYNC_A = o/$(MODE)/third_party/nsync/nsync.a THIRD_PARTY_NSYNC_A_FILES := $(wildcard third_party/nsync/*) THIRD_PARTY_NSYNC_A_HDRS = $(filter %.h,$(THIRD_PARTY_NSYNC_A_FILES)) -THIRD_PARTY_NSYNC_A_SRCS = $(filter %.c,$(THIRD_PARTY_NSYNC_A_FILES)) +THIRD_PARTY_NSYNC_A_SRCS_C = $(filter %.c,$(THIRD_PARTY_NSYNC_A_FILES)) +THIRD_PARTY_NSYNC_A_SRCS_S = $(filter %.S,$(THIRD_PARTY_NSYNC_A_FILES)) + +THIRD_PARTY_NSYNC_A_SRCS = \ + $(THIRD_PARTY_NSYNC_A_SRCS_S) \ + $(THIRD_PARTY_NSYNC_A_SRCS_C) THIRD_PARTY_NSYNC_A_OBJS = \ - $(THIRD_PARTY_NSYNC_A_SRCS:%.c=o/$(MODE)/%.o) + $(THIRD_PARTY_NSYNC_A_SRCS_C:%.c=o/$(MODE)/%.o) \ + $(THIRD_PARTY_NSYNC_A_SRCS_S:%.S=o/$(MODE)/%.o) THIRD_PARTY_NSYNC_A_DIRECTDEPS = \ LIBC_CALLS \ @@ -54,4 +60,6 @@ THIRD_PARTY_NSYNC_OBJS = $(foreach x,$(THIRD_PARTY_NSYNC_ARTIFACTS),$($(x)_OBJS) $(THIRD_PARTY_NSYNC_OBJS): third_party/nsync/nsync.mk .PHONY: o/$(MODE)/third_party/nsync -o/$(MODE)/third_party/nsync: $(THIRD_PARTY_NSYNC_CHECKS) +o/$(MODE)/third_party/nsync: \ + o/$(MODE)/third_party/nsync/mem \ + $(THIRD_PARTY_NSYNC_CHECKS)