mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
StrictConcurrency: ValueObservation.print cautiously uses its stream argument
This commit is contained in:
1 parent
2947b3d78b
commit
5f8b39b7e6
2 files changed
+32
-1
No files matched your search
@@ -49,3 +49,32 @@ extension Mutex where T: Numeric {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension Mutex: @unchecked Sendable where T: Sendable { }
|
extension Mutex: @unchecked Sendable where T: Sendable { }
|
||||||
|
|
||||||
|
// MARK: - UnsafeSendableMutex
|
||||||
|
|
||||||
|
/// `UnsafeSendableMutex` is a Mutex that is always Sendable. It is unsafe,
|
||||||
|
/// because it does not guarantee that its value won't escape from the
|
||||||
|
/// critical section.
|
||||||
|
///
|
||||||
|
/// We'll replace it with the SE-0433 Mutex when it is available.
|
||||||
|
/// <https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md>
|
||||||
|
///
|
||||||
|
/// For a longer discussion about Sendable and mutexes, see
|
||||||
|
/// <https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md#interactions-with-swift-concurrency>
|
||||||
|
final class UnsafeSendableMutex<T>: @unchecked Sendable {
|
||||||
|
private var _value: T
|
||||||
|
private var lock = NSLock()
|
||||||
|
|
||||||
|
init(_ value: T) {
|
||||||
|
_value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the provided closure while holding a lock on the value.
|
||||||
|
///
|
||||||
|
/// - parameter body: A closure that can modify the value.
|
||||||
|
func withLock<U>(_ body: (inout T) throws -> U) rethrows -> U {
|
||||||
|
lock.lock()
|
||||||
|
defer { lock.unlock() }
|
||||||
|
return try body(&_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -234,7 +234,9 @@ extension ValueObservation: Refinable {
|
|||||||
to stream: TextOutputStream? = nil)
|
to stream: TextOutputStream? = nil)
|
||||||
-> ValueObservation<ValueReducers.Trace<Reducer>>
|
-> ValueObservation<ValueReducers.Trace<Reducer>>
|
||||||
{
|
{
|
||||||
let streamMutex = Mutex(stream ?? PrintOutputStream())
|
// Safe because we don't escape the stream from the mutex
|
||||||
|
let streamMutex = UnsafeSendableMutex(stream ?? PrintOutputStream())
|
||||||
|
|
||||||
let prefix = prefix.isEmpty ? "" : "\(prefix): "
|
let prefix = prefix.isEmpty ? "" : "\(prefix): "
|
||||||
return handleEvents(
|
return handleEvents(
|
||||||
willStart: {
|
willStart: {
|
||||||
|
|||||||
Reference in new issue
Block a user