Update SwappableLock to support NET9+ Lock type#1077
Merged
dwcullop merged 4 commits intoreactivemarbles:mainfrom Apr 17, 2026
Merged
Update SwappableLock to support NET9+ Lock type#1077dwcullop merged 4 commits intoreactivemarbles:mainfrom
dwcullop merged 4 commits intoreactivemarbles:mainfrom
Conversation
Add Lock overloads for SwappableLock.SwapTo and constructor to support the new System.Threading.Lock type on .NET 9+. Uses #if NET9_0_OR_GREATER conditional compilation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds .NET 9+ System.Threading.Lock support to SwappableLock so ObservableCache can atomically swap between synchronization primitives without forcing object/Monitor locks on newer runtimes.
Changes:
- Added
CreateAndEnter(Lock gate)factory for NET9+. - Added
SwapTo(Lock gate)overload and updatedSwapTo(object gate)to correctly release whichever gate type is currently held. - Updated
Dispose()to release a heldLockon NET9+.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
JakenVeina
requested changes
Apr 13, 2026
… tests Replace #if spaghetti throughout every method with two complete, independent implementations behind a single top-level #if NET9_0_OR_GREATER. NET9: uses System.Threading.Lock directly (simpler, no _hasLock bool needed). Pre-NET9: uses Monitor.Enter/Exit on object gates (unchanged behavior). Filter.Dynamic: gates upgraded to Lock on NET9 for consistency. Added SwappableLockFixture with 7 tests covering CreateAndEnter, Dispose (release + idempotent), SwapTo (basic + chained), uninitialized throws, and Dispose after swap.
JakenVeina
approved these changes
Apr 14, 2026
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.
Problem
SwappableLockis aref structused internally byFilter.Dynamicto atomically swap between lock objects during filtered changeset processing. It originally only supportedMonitor.Enter/Monitor.Exitonobjectgates..NET 9 introduced
System.Threading.Lock: a dedicated, more efficient lock type that is non-reentrant and does not supportMonitor.Enter. Code paths that useLockon .NET 9 were incompatible withSwappableLock.Approach
Instead of sprinkling
#if NET9_0_OR_GREATERthroughout every method (mixingLockandobjectstate in the same struct), we splitSwappableLockinto two complete, independent implementations behind a single top-level#if:System.Threading.Lockdirectly. Simpler (no_hasLockbool needed,Locktracks its own state).Monitor.Enter/Exitonobjectgates. Unchanged behavior from the original.Each implementation is self-contained and readable without mental
#ifgymnastics.Changes
Internal/SwappableLock.cs- Two complete implementations behind top-level#if. NET9 version usesLock.Cache/Internal/Filter.Dynamic.cs- Gate properties returnLockon NET9,objectotherwise. DedicatedLockfields on NET9.Tests/Internal/SwappableLockFixture.cs- New. 7 tests covering CreateAndEnter, Dispose (release + idempotent), SwapTo (basic + chained), uninitialized throws, and Dispose after swap.Zero behavioral change on pre-.NET 9 targets.