Skip to content

fix: make concurrency/storage primitives arm64-correct (explicit acquire/release barriers) - #489

Merged
nockawa merged 2 commits into
mainfrom
fix/arm64-portability-memory-barriers
Jul 17, 2026
Merged

nockawa merged 2 commits into
mainfrom
fix/arm64-portability-memory-barriers

Conversation

@nockawa

@nockawa nockawa commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

Several concurrency and storage primitives relied on x86/x64 Total Store Order (TSO) for cross-thread ordering — plain loads/stores on publication flags and version counters, with comments explicitly citing "on x64, stores/loads are never reordered." That reasoning does not hold on arm64, which is weakly ordered: a plain store can be exposed before the data it guards, and a plain load can observe a published flag before the payload it protects.

This branch makes the release/acquire pairing explicit where correctness depends on it. On x64 the barriers are free (Volatile compiles to a plain mov under TSO); on arm64 they emit stlr/ldar, which is what the code needed all along.

Changes

Explicit acquire/release barriers (replacing bare field access on the ordering-critical flag/counter):

File What now carries the barrier
Durability/internals/WalCommitBuffer.cs acquire loads on WalFrameHeader.FrameLength (pairs with the producer's Interlocked.Exchange release)
Foundation/Collections/internals/ConcurrentHashMap.cs / ConcurrentHashMapKV.cs OLC stripe OlcVersion — acquire on read, release on ReleaseStripeLock
Indexing/internals/OlcLatch.cs ReadVersion / ValidateVersion / WriteUnlock / AbortWriteLock
Profiler/internals/TraceRecordRing.cs _head/_tail cursor publication (Publish/Drain/IsEmpty) + the class <remarks> that still claimed no fences were needed
Querying/internals/ViewDeltaRingBuffer.cs per-slot _written flag
Runtime/public/DagScheduler.cs _isReady[i] vs TotalChunks/_remainingChunks publication

Non-barrier portability fixes:

  • Foundation/Collections/internals/HashMap.cs / HashMapKV.cs — gate Sse.Prefetch0 behind Sse.IsSupported (an x86-only intrinsic that would otherwise throw on arm64; the JIT elides the whole block as a constant on non-x86).
  • Resources/public/ResourceExhaustedException.cs — format the diagnostic message with InvariantCulture so N0/F1 don't pick up the ambient culture.

Plus a doc-only note on AccessControlSmall's 32,767 simultaneously-live-thread ceiling (signed backing field for the thread id), as a separate commit.

Verification

Run on Apple Silicon (osx-arm64) — the actual weakly-ordered target, so the JIT emits ldar/stlr for the new Volatile calls rather than an x64 proxy.

  • Full suite: 3999 passed / 2 failed / 39 skipped. No deadlock (--blame-hang never fired).
  • All 405 tests covering the 10 changed files pass (1 skipped).
  • The 2 failures (CpuSampleParserTests.Parse_MergesFramesByMethod, Parse_ResolvesEngineFrameToSource) are unrelated and branch-independent: they touch none of the changed files, pass in isolation on both this branch and main (18/18), and fail only inside the full parallel suite — both assert that a short CPU-burn method appears in captured samples, a load-sensitive check starved by 13 minutes of 8-core saturation.

What this proves — and what it doesn't

A green arm64 run confirms no regression, no deadlock, and correct execution on the target hardware. It does not prove the barriers were strictly necessary or that every latent reordering race is eliminated — missing-barrier bugs are probabilistic and rarely tripped by a functional suite. The stronger guarantee lives with the concurrency TLA+ specs and a dedicated stress/interleaving harness, out of scope here.

🤖 Generated with Claude Code

nockawa and others added 2 commits July 17, 2026 00:22
…ards, invariant formatting

Several primitives relied on x86 TSO for ordering. Replace the plain
loads/stores on the publication flags and version counters with
Volatile.Read/Write so the release/acquire pairing is explicit and correct
on arm64 (free on x64 — compiles to plain mov):

- WalCommitBuffer: acquire loads on WalFrameHeader.FrameLength
- ConcurrentHashMap / ConcurrentHashMapKV: OLC stripe version
- OlcLatch: ReadVersion / ValidateVersion / WriteUnlock / AbortWriteLock
- TraceRecordRing: head/tail cursor publication
- ViewDeltaRingBuffer: per-slot _written flag
- DagScheduler: _isReady flag vs TotalChunks/_remainingChunks

Also:
- HashMap / HashMapKV: gate Sse.Prefetch0 on Sse.IsSupported (x86-only
  intrinsic; block is elided by the JIT on arm64)
- ResourceExhaustedException: format with InvariantCulture so N0/F1 don't
  pick up the ambient culture

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The thread id occupies bits 16-31 of a signed int, so a managed thread id
≥32,768 would set the sign bit and LockedByThreadId would sign-extend on read.
This is half AccessControl's 65,535 and is an accepted bound: managed thread ids
are allocated lowest-available-first and recycled on thread death, so the ceiling
applies to simultaneously-live threads, not threads created over the process
lifetime — far beyond any workload this engine targets.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nockawa
nockawa merged commit 307066e into main Jul 17, 2026
16 of 18 checks passed
@nockawa
nockawa deleted the fix/arm64-portability-memory-barriers branch July 18, 2026 12:11
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