Repository navigation
fix: make concurrency/storage primitives arm64-correct (explicit acquire/release barriers) - #489
Merged
Merged
Conversation
…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>
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.
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 (
Volatilecompiles to a plainmovunder TSO); on arm64 they emitstlr/ldar, which is what the code needed all along.Changes
Explicit acquire/release barriers (replacing bare field access on the ordering-critical flag/counter):
Durability/internals/WalCommitBuffer.csWalFrameHeader.FrameLength(pairs with the producer'sInterlocked.Exchangerelease)Foundation/Collections/internals/ConcurrentHashMap.cs/ConcurrentHashMapKV.csOlcVersion— acquire on read, release onReleaseStripeLockIndexing/internals/OlcLatch.csReadVersion/ValidateVersion/WriteUnlock/AbortWriteLockProfiler/internals/TraceRecordRing.cs_head/_tailcursor publication (Publish/Drain/IsEmpty) + the class<remarks>that still claimed no fences were neededQuerying/internals/ViewDeltaRingBuffer.cs_writtenflagRuntime/public/DagScheduler.cs_isReady[i]vsTotalChunks/_remainingChunkspublicationNon-barrier portability fixes:
Foundation/Collections/internals/HashMap.cs/HashMapKV.cs— gateSse.Prefetch0behindSse.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 withInvariantCulturesoN0/F1don'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/stlrfor the newVolatilecalls rather than an x64 proxy.--blame-hangnever fired).CpuSampleParserTests.Parse_MergesFramesByMethod,Parse_ResolvesEngineFrameToSource) are unrelated and branch-independent: they touch none of the changed files, pass in isolation on both this branch andmain(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