Skip to content

Back ManagedCriticalState with Synchronization.Mutex - #70

Closed
lhoward wants to merge 1 commit into
sideeffect-io:mainfrom
lhoward:upstream/locking-mutex
Closed

lhoward wants to merge 1 commit into
sideeffect-io:mainfrom
lhoward:upstream/locking-mutex

Conversation

@lhoward

@lhoward lhoward commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Description

ManagedCriticalState is a ManagedBuffer around an os_unfair_lock, which makes Sources/Supporting/ManagedCriticalState.swift the only platform-specific code in Sources/. This replaces it with a Synchronization.Mutex held in a final-class box, so the wrapper keeps the reference semantics and copyability that the iterators and state machines rely on, and the API (withCriticalRegion, apply(criticalState:), criticalState) is unchanged. The file goes from 45 to 34 lines and the package then builds on Linux as well.

The cost is the deployment target: Mutex needs macOS 15 / iOS 18 / tvOS 18 / watchOS 11 / visionOS 2, so Package.swift moves to that floor (tools 5.9 only for the visionOS entry; the language mode is unchanged). If you would rather keep the current floor, a #if canImport(Darwin)-gated variant that leaves the os_unfair_lock path byte-for-byte and uses Mutex only elsewhere is available instead.

Tests

Linux, Swift 6.4, x86_64, with the two test-only fixes from the companion PR applied (an unused import Combine and a sync test that returns before its task; without them the test target does not compile here), three consecutive runs:

Executed 199 tests, with 0 failures (0 unexpected) in 15.396 (15.396) seconds
Executed 199 tests, with 0 failures (0 unexpected) in 15.653 (15.653) seconds
Executed 199 tests, with 0 failures (0 unexpected) in 15.802 (15.802) seconds

Checklist

  • this PR is based on the main branch and is up-to-date
  • the commits inside this PR have explicit commit messages
  • unit tests cover the new feature or the bug fix (no behaviour change; the existing suite covers the lock)
  • the CHANGELOG is up-to-date (happy to add an entry if you want this)

🤖 Generated with Claude Code

https://claude.ai/code/session_01T8cahEd4GfsHQ89sNrmP4p

Replace the os_unfair_lock ManagedBuffer with a Mutex held in a final
class box, so the wrapper keeps its reference semantics and copyable
holders while carrying no platform-specific locking code; it was the
only platform-specific code in Sources.

Mutex needs macOS 15 / iOS 18 / tvOS 18 / watchOS 11 / visionOS 2, so
the manifest moves to that floor (tools 5.9 for the visionOS entry; the
language mode is unchanged).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T8cahEd4GfsHQ89sNrmP4p
@twittemb

twittemb commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded by merged PR #74: #74.

Current main now uses Synchronization.Mutex in a reference-backed ManagedCriticalState, preserves shared state between iterator copies and callbacks, raises the supported Apple deployment targets, and requires Swift 6.1+. Linux release builds and the full test suite pass in CI on Swift 6.1.3 and 6.4.0. This covers the Linux compatibility change proposed here.

The additional explicit visionOS 2 platform declaration in this PR was not included in #74; that can be considered separately as a visionOS support follow-up.

@twittemb twittemb closed this Oct 4, 2026
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.

2 participants