Skip to content

Re-core ThreadSafeBox with RefBox<Mutex<Value>>#59

Merged
rintaro merged 2 commits into
swiftlang:mainfrom
rintaro:add-refbox
May 28, 2026
Merged

Re-core ThreadSafeBox with RefBox<Mutex<Value>>#59
rintaro merged 2 commits into
swiftlang:mainfrom
rintaro:add-refbox

Conversation

@rintaro
Copy link
Copy Markdown
Member

@rintaro rintaro commented May 27, 2026

Add RefBox<Value>: a class wrapping a single value. It provides reference semantics for value types.

Replace ThreadSafeBox's NSLock-backed class with a struct holding RefBox<Mutex<_>>. The Mutex lives in the RefBox so the struct stays cheap to pass while preserving lock identity across copies. ThreadSafeBox is now a thin wrapper providing convenient APIs.

NonDarwinLogging._logLevel and ._privacyLevel migrate to RefBox<Atomic<_>> because they are accessed heavily and we want them lock free.

Drop the now-unused NSLock.withLock extension.

Add RefBox<Value>: a class wrapping a single value. It provides
reference semantics for value types.

Replace ThreadSafeBox's NSLock-backed class with a struct holding
RefBox<Mutex<_>>. The Mutex lives in the RefBox so the struct stays
cheap to pass while preserving lock identity across copies.
ThreadSafeBox is now a thin wrapper providing convenient APIs.

NonDarwinLogging._logLevel and ._privacyLevel migrate to
RefBox<Atomic<_>> because they are accessed heavily and we want them
lock free.

Drop the now-unused NSLock.withLock extension.
Comment thread Sources/ToolsProtocolsSwiftExtensions/ThreadSafeBox.swift Outdated
Comment thread Sources/ToolsProtocolsSwiftExtensions/ThreadSafeBox.swift Outdated
`ThreadSafeBox.value`'s setter took the lock independently from
the getter, so a natural-looking `box.value += 1` would silently
do load-modify-store with two separate lock acquisitions and
lose concurrent increments.

Remove the setter; `value` is now read-only. Writes go through
`withLock { $0 = ... }`, making the lock-holding scope explicit
and covering read-modify-write atomically.
@rintaro rintaro merged commit a0c1735 into swiftlang:main May 28, 2026
52 of 53 checks passed
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