Skip to content

build(atomic): detect GCC/Clang __atomic builtins, enabling native atomics#32

Merged
gburd merged 1 commit into
masterfrom
build/atomic-detection
Jun 25, 2026
Merged

build(atomic): detect GCC/Clang __atomic builtins, enabling native atomics#32
gburd merged 1 commit into
masterfrom
build/atomic-detection

Conversation

@gburd

@gburd gburd commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

What

Teach configure to detect the GCC/Clang __atomic builtins and select them
as the atomic-operations backend, ahead of the inline-assembly tier.

Why

The atomic-operation detection in dist/aclocal/mutex.m4 only knew the
x86/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 then
falls through every remaining tier and db_cv_atomic ends up no.

With HAVE_ATOMIC_SUPPORT undefined, the engine uses the mutex-emulated atomic
tier: atomic_inc/atomic_dec/atomic_compare_exchange on mpool buffer
refcounts, 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

  • Add a __atomic builtin probe (tests __atomic_add_fetch plus 32-bit and
    pointer-width __atomic_compare_exchange_n) and try it before the inline-asm
    probe. 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.
  • When it matches, define HAVE_ATOMIC_SUPPORT, HAVE_ATOMIC_BUILTINS, and
    HAVE_ATOMIC_GCC_BUILTIN, and probe 64-bit support so HAVE_ATOMIC_64BIT is
    set.
  • 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.
  • Replace the bare exit(0) in the x86/gcc-assembly probe with (void)0 so it
    compiles cleanly on modern toolchains.
  • configure and config.hin regenerated with Autoconf 2.73.

No engine source changes; this only selects which existing os_atomic.c tier
compiles.

Verification

On x86_64-linux / clang, building from clean master plus these files:

  • checking for atomic operations... gcc/__atomic
  • checking for 64-bit atomic operations... yes
  • db_config.h: HAVE_ATOMIC_SUPPORT, HAVE_ATOMIC_BUILTINS,
    HAVE_ATOMIC_GCC_BUILTIN, HAVE_ATOMIC_64BIT all defined
  • full build clean; __os_atomic_cas_ptr compiles to a single lock cmpxchg
    with no mutex call
  • TCL: test001, test012, ssi001, ssi002 pass; recd001 recovery cycles verify

…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.
@gburd gburd merged commit e2e4ad6 into master Jun 25, 2026
36 of 39 checks passed
@gburd gburd deleted the build/atomic-detection branch June 25, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant