build(atomic): detect GCC/Clang __atomic builtins, enabling native atomics#32
Merged
Conversation
…omics configure's atomic-operation detection only knew the x86/gcc-assembly, Solaris, and MinGW tiers. On a modern clang or gcc toolchain the x86/gcc-assembly probe is tried first and fails to compile (its body calls exit() with no <stdlib.h>, which is a hard error under C99+), detection falls through every remaining tier, and db_cv_atomic ends up "no". HAVE_ATOMIC_SUPPORT is then left undefined and the engine falls back to the mutex-emulated atomic tier: every atomic_inc/dec/cas on mpool buffer refcounts, shared-latch reader counts, and lock-manager counters actually takes a pool mutex. Native atomics were silently off on every modern build. Add a GCC/Clang __atomic builtin probe and try it before the inline-asm probe. The builtin tier is portable across architectures and provides the full set of primitives the engine relies on, including 64-bit and pointer-width compare-and-swap. When it matches, define HAVE_ATOMIC_SUPPORT, HAVE_ATOMIC_BUILTINS, and HAVE_ATOMIC_GCC_BUILTIN, and probe 64-bit support via __atomic_compare_exchange_n. HAVE_ATOMIC_GCC_BUILTIN is the macro os_ext.h uses to expose the __os_atomic_* prototypes; defining it from configure makes those prototypes visible to every translation unit, not just os_atomic.c. Also replace the bare exit(0) in the x86/gcc-assembly probe with (void)0 so that probe compiles cleanly on modern toolchains too. This is the same class of fix as the K&R/C-standard work in #25. Verified on x86_64-linux/clang: "checking for atomic operations... gcc/__atomic", "checking for 64-bit atomic operations... yes", full build clean, and __os_atomic_cas_ptr compiles to a single lock cmpxchg with no mutex call. configure regenerated with Autoconf 2.73.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Teach
configureto detect the GCC/Clang__atomicbuiltins and select themas the atomic-operations backend, ahead of the inline-assembly tier.
Why
The atomic-operation detection in
dist/aclocal/mutex.m4only knew thex86/gcc-assembly, Solaris, and MinGW tiers. On a modern clang or gcc toolchain
the x86/gcc-assembly probe runs first and fails to compile (its probe body
calls
exit()with no<stdlib.h>, a hard error under C99+). Detection thenfalls through every remaining tier and
db_cv_atomicends upno.With
HAVE_ATOMIC_SUPPORTundefined, the engine uses the mutex-emulated atomictier:
atomic_inc/atomic_dec/atomic_compare_exchangeon mpool bufferrefcounts, shared-latch reader counts, and lock-manager counters each take a
pool mutex. Native hardware atomics were silently off on every modern build.
This is the same class of issue as the K&R / C-standard fix in #25.
Change
__atomicbuiltin probe (tests__atomic_add_fetchplus 32-bit andpointer-width
__atomic_compare_exchange_n) and try it before the inline-asmprobe. The builtin tier is portable across architectures and provides the
full set of primitives the engine uses, including 64-bit and pointer-width
compare-and-swap.
HAVE_ATOMIC_SUPPORT,HAVE_ATOMIC_BUILTINS, andHAVE_ATOMIC_GCC_BUILTIN, and probe 64-bit support soHAVE_ATOMIC_64BITisset.
HAVE_ATOMIC_GCC_BUILTINis the macroos_ext.huses to expose the__os_atomic_*prototypes; defining it fromconfiguremakes thoseprototypes visible to every translation unit, not just
os_atomic.c.exit(0)in the x86/gcc-assembly probe with(void)0so itcompiles cleanly on modern toolchains.
configureandconfig.hinregenerated with Autoconf 2.73.No engine source changes; this only selects which existing
os_atomic.ctiercompiles.
Verification
On x86_64-linux / clang, building from clean master plus these files:
checking for atomic operations... gcc/__atomicchecking for 64-bit atomic operations... yesdb_config.h:HAVE_ATOMIC_SUPPORT,HAVE_ATOMIC_BUILTINS,HAVE_ATOMIC_GCC_BUILTIN,HAVE_ATOMIC_64BITall defined__os_atomic_cas_ptrcompiles to a singlelock cmpxchgwith no mutex call