mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Rename LockedBox to Mutex
Maybe we'll be able to plug in the standard one eventually: https://forums.swift.org/t/pitch-synchronous-mutual-exclusion-lock/69889
This commit is contained in:
1 parent
a00825c882
commit
fb0866c6fa
12 files changed
+89
-102
No files matched your search
@@ -183,7 +183,7 @@
|
||||
566B912B1FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566B912A1FA4D0CC0012D5B0 /* StatementAuthorizer.swift */; };
|
||||
566B91331FA4D3810012D5B0 /* TransactionObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566B91321FA4D3810012D5B0 /* TransactionObserver.swift */; };
|
||||
566B9C2025C6CC24004542CF /* RowDecodingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566B9C1F25C6CC24004542CF /* RowDecodingError.swift */; };
|
||||
566BE71E2342542F00A8254B /* LockedBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BE7172342542F00A8254B /* LockedBox.swift */; };
|
||||
566BE71E2342542F00A8254B /* Mutex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BE7172342542F00A8254B /* Mutex.swift */; };
|
||||
566DDE0D288D763C0000DCFB /* Fixits.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566DDE0C288D763C0000DCFB /* Fixits.swift */; };
|
||||
56703297212B5450007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56703290212B544F007D270F /* DatabaseUUIDEncodingStrategyTests.swift */; };
|
||||
56713FDD2691F409006153C3 /* JSONRequiredEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56713FDC2691F409006153C3 /* JSONRequiredEncoder.swift */; };
|
||||
@@ -619,7 +619,7 @@
|
||||
566B912A1FA4D0CC0012D5B0 /* StatementAuthorizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementAuthorizer.swift; sourceTree = "<group>"; };
|
||||
566B91321FA4D3810012D5B0 /* TransactionObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionObserver.swift; sourceTree = "<group>"; };
|
||||
566B9C1F25C6CC24004542CF /* RowDecodingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RowDecodingError.swift; sourceTree = "<group>"; };
|
||||
566BE7172342542F00A8254B /* LockedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LockedBox.swift; sourceTree = "<group>"; };
|
||||
566BE7172342542F00A8254B /* Mutex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mutex.swift; sourceTree = "<group>"; };
|
||||
566DDE0C288D763C0000DCFB /* Fixits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fixits.swift; sourceTree = "<group>"; };
|
||||
56703290212B544F007D270F /* DatabaseUUIDEncodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseUUIDEncodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
56713FDC2691F409006153C3 /* JSONRequiredEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONRequiredEncoder.swift; sourceTree = "<group>"; };
|
||||
@@ -1306,7 +1306,7 @@
|
||||
56717270261C68E900423B6F /* CaseInsensitiveIdentifier.swift */,
|
||||
563EF4492161F179007DAACD /* Inflections.swift */,
|
||||
569BBA482291707D00478429 /* Inflections+English.swift */,
|
||||
566BE7172342542F00A8254B /* LockedBox.swift */,
|
||||
566BE7172342542F00A8254B /* Mutex.swift */,
|
||||
563B8FC424A1D3B9007A48C9 /* OnDemandFuture.swift */,
|
||||
563EF414215F87EB007DAACD /* OrderedDictionary.swift */,
|
||||
5659F4971EA8D989004A4992 /* Pool.swift */,
|
||||
@@ -2246,7 +2246,7 @@
|
||||
5690C3401D23E82A00E59934 /* Data.swift in Sources */,
|
||||
5659F4881EA8D94E004A4992 /* Utils.swift in Sources */,
|
||||
567B5BE82AD3284100629622 /* DumpFormat.swift in Sources */,
|
||||
566BE71E2342542F00A8254B /* LockedBox.swift in Sources */,
|
||||
566BE71E2342542F00A8254B /* Mutex.swift in Sources */,
|
||||
56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */,
|
||||
5603CEBB2AC862EC00CF097D /* SQLJSONExpressible.swift in Sources */,
|
||||
56F89DF72A57EAA9002FE2AA /* ColumnDefinition.swift in Sources */,
|
||||
|
||||
@@ -281,7 +281,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
var isInsideTransactionBlock = false
|
||||
|
||||
/// Support for `checkForSuspensionViolation(from:)`
|
||||
@LockedBox var isSuspended = false
|
||||
@Mutex var isSuspended = false
|
||||
|
||||
/// Support for `checkForSuspensionViolation(from:)`
|
||||
/// This cache is never cleared: we assume journal mode never changes.
|
||||
@@ -1112,7 +1112,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
///
|
||||
/// Suspension ends with `resume()`.
|
||||
func suspend() {
|
||||
$isSuspended.update { isSuspended in
|
||||
$isSuspended.withLock { isSuspended in
|
||||
if isSuspended {
|
||||
return
|
||||
}
|
||||
@@ -1169,7 +1169,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
///
|
||||
/// See `suspend()` and ``Configuration/observesSuspensionNotifications``.
|
||||
func checkForSuspensionViolation(from statement: Statement) throws {
|
||||
try $isSuspended.read { isSuspended in
|
||||
try $isSuspended.withLock { isSuspended in
|
||||
guard isSuspended else {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public final class DatabasePool {
|
||||
/// It is constant, until close() sets it to nil.
|
||||
private var readerPool: Pool<SerializedDatabase>?
|
||||
|
||||
@LockedBox var databaseSnapshotCount = 0
|
||||
@Mutex var databaseSnapshotCount = 0
|
||||
|
||||
/// If Database Suspension is enabled, this array contains the necessary `NotificationCenter` observers.
|
||||
private var suspensionObservers: [NSObjectProtocol] = []
|
||||
|
||||
@@ -91,16 +91,16 @@ extension DatabaseRegionObservation {
|
||||
onChange: @escaping @Sendable (Database) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
let stateBox = LockedBox(wrappedValue: ObservationState.pending)
|
||||
let state = Mutex(ObservationState.pending)
|
||||
|
||||
// Use unsafeReentrantWrite so that observation can start from any
|
||||
// dispatch queue.
|
||||
writer.unsafeReentrantWrite { db in
|
||||
do {
|
||||
let region = try observedRegion(db).observableRegion(db)
|
||||
stateBox.update {
|
||||
state.withLock {
|
||||
let observer = DatabaseRegionObserver(region: region, onChange: {
|
||||
if case .cancelled = stateBox.wrappedValue {
|
||||
if case .cancelled = state.wrappedValue {
|
||||
return
|
||||
}
|
||||
onChange($0)
|
||||
@@ -124,7 +124,7 @@ extension DatabaseRegionObservation {
|
||||
// Deallocates the transaction observer. This makes sure that the
|
||||
// `onChange` callback will never be called again, because the
|
||||
// observation was started with the `.observerLifetime` extent.
|
||||
stateBox.wrappedValue = .cancelled
|
||||
state.wrappedValue = .cancelled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
/// A LockedBox protects a value with an NSLock.
|
||||
@propertyWrapper
|
||||
final class LockedBox<T> {
|
||||
private var _wrappedValue: T
|
||||
private var lock = NSLock()
|
||||
|
||||
var wrappedValue: T {
|
||||
get { read { $0 } }
|
||||
set { update { $0 = newValue } }
|
||||
}
|
||||
|
||||
var projectedValue: LockedBox<T> { self }
|
||||
|
||||
init(wrappedValue: T) {
|
||||
_wrappedValue = wrappedValue
|
||||
}
|
||||
|
||||
/// Runs the provided closure while holding a lock on the value.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // Prints "0"
|
||||
/// @LockedBox var count = 0
|
||||
/// $count.read { print($0) }
|
||||
///
|
||||
/// - parameter block: A closure that accepts the value.
|
||||
@inline(__always)
|
||||
@usableFromInline
|
||||
func read<U>(_ block: (T) throws -> U) rethrows -> U {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try block(_wrappedValue)
|
||||
}
|
||||
|
||||
/// Runs the provided closure while holding a lock on the value.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // Prints "1"
|
||||
/// @LockedBox var count = 0
|
||||
/// $count.update { $0 += 1 }
|
||||
/// print(count)
|
||||
///
|
||||
/// - parameter block: A closure that can modify the value.
|
||||
func update<U>(_ block: (inout T) throws -> U) rethrows -> U {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try block(&_wrappedValue)
|
||||
}
|
||||
}
|
||||
|
||||
extension LockedBox where T: Numeric {
|
||||
@discardableResult
|
||||
func increment() -> T {
|
||||
update { n in
|
||||
n += 1
|
||||
return n
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func decrement() -> T {
|
||||
update { n in
|
||||
n -= 1
|
||||
return n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LockedBox: @unchecked Sendable where T: Sendable { }
|
||||
@@ -0,0 +1,59 @@
|
||||
import Foundation
|
||||
|
||||
/// A Mutex protects a value with an NSLock.
|
||||
@propertyWrapper
|
||||
final class Mutex<T> {
|
||||
private var _wrappedValue: T
|
||||
private var lock = NSLock()
|
||||
|
||||
var wrappedValue: T {
|
||||
get { withLock { $0 } }
|
||||
set { withLock { $0 = newValue } }
|
||||
}
|
||||
|
||||
var projectedValue: Mutex<T> { self }
|
||||
|
||||
init(wrappedValue: T) {
|
||||
_wrappedValue = wrappedValue
|
||||
}
|
||||
|
||||
init(_ value: T) {
|
||||
_wrappedValue = value
|
||||
}
|
||||
|
||||
/// Runs the provided closure while holding a lock on the value.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // Prints "1"
|
||||
/// @Mutex var count = 0
|
||||
/// $count.withLock { $0 += 1 }
|
||||
/// print(count)
|
||||
///
|
||||
/// - parameter block: 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(&_wrappedValue)
|
||||
}
|
||||
}
|
||||
|
||||
extension Mutex where T: Numeric {
|
||||
@discardableResult
|
||||
func increment() -> T {
|
||||
withLock { n in
|
||||
n += 1
|
||||
return n
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func decrement() -> T {
|
||||
withLock { n in
|
||||
n -= 1
|
||||
return n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Mutex: @unchecked Sendable where T: Sendable { }
|
||||
@@ -149,7 +149,7 @@ final class ValueConcurrentObserver<Reducer: ValueReducer, Scheduler: ValueObser
|
||||
private var notificationCallbacks: NotificationCallbacks?
|
||||
|
||||
/// The fetching state for observation of constant regions.
|
||||
@LockedBox private var fetchingState = FetchingState.idle
|
||||
@Mutex private var fetchingState = FetchingState.idle
|
||||
|
||||
/// Support for `TransactionObserver`, protected by the serialized writer
|
||||
/// dispatch queue.
|
||||
@@ -782,7 +782,7 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
}
|
||||
|
||||
private func setNeedsFetching(databaseAccess: DatabaseAccess) {
|
||||
$fetchingState.update { state in
|
||||
$fetchingState.withLock { state in
|
||||
switch state {
|
||||
case .idle:
|
||||
state = .fetching
|
||||
@@ -808,7 +808,7 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
|
||||
self.reduce(fetchResult)
|
||||
|
||||
$fetchingState.update { state in
|
||||
$fetchingState.withLock { state in
|
||||
switch state {
|
||||
case .idle:
|
||||
// GRDB bug
|
||||
|
||||
@@ -265,29 +265,29 @@ extension ValueObservation: Refinable {
|
||||
to stream: Stream)
|
||||
-> ValueObservation<ValueReducers.Trace<Reducer>>
|
||||
{
|
||||
let stream = LockedBox(wrappedValue: stream)
|
||||
let stream = Mutex(stream)
|
||||
let prefix = prefix.isEmpty ? "" : "\(prefix): "
|
||||
return handleEvents(
|
||||
willStart: {
|
||||
stream.update { $0.write("\(prefix)start") }
|
||||
stream.withLock { $0.write("\(prefix)start") }
|
||||
},
|
||||
willFetch: {
|
||||
stream.update { $0.write("\(prefix)fetch") }
|
||||
stream.withLock { $0.write("\(prefix)fetch") }
|
||||
},
|
||||
willTrackRegion: { region in
|
||||
stream.update { $0.write("\(prefix)tracked region: \(region)") }
|
||||
stream.withLock { $0.write("\(prefix)tracked region: \(region)") }
|
||||
},
|
||||
databaseDidChange: {
|
||||
stream.update { $0.write("\(prefix)database did change") }
|
||||
stream.withLock { $0.write("\(prefix)database did change") }
|
||||
},
|
||||
didReceiveValue: { value in
|
||||
stream.update { $0.write("\(prefix)value: \(value)") }
|
||||
stream.withLock { $0.write("\(prefix)value: \(value)") }
|
||||
},
|
||||
didFail: { error in
|
||||
stream.update { $0.write("\(prefix)failure: \(error)") }
|
||||
stream.withLock { $0.write("\(prefix)failure: \(error)") }
|
||||
},
|
||||
didCancel: {
|
||||
stream.update { $0.write("\(prefix)cancel") }
|
||||
stream.withLock { $0.write("\(prefix)cancel") }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@
|
||||
566B91351FA4D3810012D5B0 /* TransactionObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566B91321FA4D3810012D5B0 /* TransactionObserver.swift */; };
|
||||
566BD7332927AFD600595649 /* ValueConcurrentObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BD7312927AFD600595649 /* ValueConcurrentObserver.swift */; };
|
||||
566BD7342927AFD600595649 /* ValueWriteOnlyObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BD7322927AFD600595649 /* ValueWriteOnlyObserver.swift */; };
|
||||
566BE7152342541F00A8254B /* LockedBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BE7132342541F00A8254B /* LockedBox.swift */; };
|
||||
566BE7152342541F00A8254B /* Mutex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566BE7132342541F00A8254B /* Mutex.swift */; };
|
||||
566DDE12288D76400000DCFB /* Fixits.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566DDE11288D76400000DCFB /* Fixits.swift */; };
|
||||
5670329B212B5462007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56703299212B5461007D270F /* DatabaseUUIDEncodingStrategyTests.swift */; };
|
||||
567071F4208A00BE006AD95A /* SQLiteDateParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567071F2208A00BE006AD95A /* SQLiteDateParser.swift */; };
|
||||
@@ -646,7 +646,7 @@
|
||||
566B91321FA4D3810012D5B0 /* TransactionObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionObserver.swift; sourceTree = "<group>"; };
|
||||
566BD7312927AFD600595649 /* ValueConcurrentObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueConcurrentObserver.swift; sourceTree = "<group>"; };
|
||||
566BD7322927AFD600595649 /* ValueWriteOnlyObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueWriteOnlyObserver.swift; sourceTree = "<group>"; };
|
||||
566BE7132342541F00A8254B /* LockedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LockedBox.swift; sourceTree = "<group>"; };
|
||||
566BE7132342541F00A8254B /* Mutex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mutex.swift; sourceTree = "<group>"; };
|
||||
566DDE11288D76400000DCFB /* Fixits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fixits.swift; sourceTree = "<group>"; };
|
||||
56703299212B5461007D270F /* DatabaseUUIDEncodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseUUIDEncodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
567071F2208A00BE006AD95A /* SQLiteDateParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteDateParser.swift; sourceTree = "<group>"; };
|
||||
@@ -1327,7 +1327,7 @@
|
||||
564D4F91261E1D3300F55856 /* CaseInsensitiveIdentifier.swift */,
|
||||
563EF44C2161F196007DAACD /* Inflections.swift */,
|
||||
569BBA4B229170B300478429 /* Inflections+English.swift */,
|
||||
566BE7132342541F00A8254B /* LockedBox.swift */,
|
||||
566BE7132342541F00A8254B /* Mutex.swift */,
|
||||
563B8FBC24A1D388007A48C9 /* OnDemandFuture.swift */,
|
||||
563EF41E215F8A76007DAACD /* OrderedDictionary.swift */,
|
||||
5659F4971EA8D989004A4992 /* Pool.swift */,
|
||||
@@ -2081,7 +2081,7 @@
|
||||
5656A8A72295BF44001FF3FF /* DatabasePromise.swift in Sources */,
|
||||
5656A86D2295BD56001FF3FF /* HasManyThroughAssociation.swift in Sources */,
|
||||
56894FE3260658A400268F4D /* Decimal.swift in Sources */,
|
||||
566BE7152342541F00A8254B /* LockedBox.swift in Sources */,
|
||||
566BE7152342541F00A8254B /* Mutex.swift in Sources */,
|
||||
F3BA806D1CFB2E55003DC1BA /* DatabaseSchemaCache.swift in Sources */,
|
||||
F3BA80841CFB2E67003DC1BA /* StandardLibrary.swift in Sources */,
|
||||
5656A87F2295BD56001FF3FF /* SQLForeignKeyRequest.swift in Sources */,
|
||||
|
||||
@@ -56,7 +56,7 @@ class GRDBTestCase: XCTestCase {
|
||||
private var dbDirectoryPath: String!
|
||||
|
||||
// Populated by default configuration
|
||||
@LockedBox var sqlQueries: [String] = []
|
||||
@Mutex var sqlQueries: [String] = []
|
||||
|
||||
// Populated by default configuration
|
||||
var lastSQLQuery: String? { sqlQueries.last }
|
||||
|
||||
@@ -4,7 +4,7 @@ import Dispatch
|
||||
|
||||
class ValueObservationPrintTests: GRDBTestCase {
|
||||
class TestStream: TextOutputStream {
|
||||
@LockedBox var strings: [String] = []
|
||||
@Mutex var strings: [String] = []
|
||||
func write(_ string: String) {
|
||||
strings.append(string)
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ class ValueObservationTests: GRDBTestCase {
|
||||
|
||||
func testTrackingExplicitRegion() throws {
|
||||
class TestStream: TextOutputStream {
|
||||
@LockedBox var strings: [String] = []
|
||||
@Mutex var strings: [String] = []
|
||||
func write(_ string: String) {
|
||||
strings.append(string)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user