Re-core ThreadSafeBox with RefBox<Mutex<Value>>#59
Merged
Conversation
9b66066 to
0e7a7fb
Compare
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.
hamishknight
approved these changes
May 27, 2026
`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.
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.
Add
RefBox<Value>: a class wrapping a single value. It provides reference semantics for value types.Replace
ThreadSafeBox'sNSLock-backed class with a struct holdingRefBox<Mutex<_>>. TheMutexlives in theRefBoxso the struct stays cheap to pass while preserving lock identity across copies.ThreadSafeBoxis now a thin wrapper providing convenient APIs.NonDarwinLogging._logLeveland._privacyLevelmigrate toRefBox<Atomic<_>>because they are accessed heavily and we want them lock free.Drop the now-unused
NSLock.withLockextension.