DatabaseWriter.barrierWriteWithoutTransaction accepts a sending closure

This commit is contained in:
Gwendal Roué committed 2025-12-30 13:01:09 +01:00
1 parent 080f9275cc
commit 7bb5c636e7
3 files changed
+26 -20

No files matched your search

+23 -17
View File
@@ -814,32 +814,38 @@ extension DatabasePool: DatabaseWriter {
}
public func barrierWriteWithoutTransaction<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
_ updates: sending (Database) throws -> T
) async throws -> T {
guard let readerPool else {
throw DatabaseError.connectionIsClosed()
}
// Pool.barrier does not support async calls (yet?).
// So we perform cancellation checks just as in
// the async version of SerializedDatabase.execute().
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
return try await withTaskCancellationHandler {
try Task.checkCancellation()
return try await readerPool.barrier {
// Compiler does not see that updates can be sent.
return try await withoutActuallyEscaping(updates) { updates in
typealias SendableClosure = @Sendable (Database) throws -> T
let updates = unsafeBitCast(updates, to: SendableClosure.self)
// Pool.barrier does not support async calls (yet?).
// So we perform cancellation checks just as in
// the async version of SerializedDatabase.execute().
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
return try await withTaskCancellationHandler {
try Task.checkCancellation()
return try writer.sync { db in
defer {
cancelMutex.store(nil)
db.uncancel()
}
cancelMutex.store(db.cancel)
return try await readerPool.barrier {
try Task.checkCancellation()
return try updates(db)
return try writer.sync { db in
defer {
cancelMutex.store(nil)
db.uncancel()
}
cancelMutex.store(db.cancel)
try Task.checkCancellation()
return try updates(db)
}
}
} onCancel: {
cancelMutex.withLock { $0?() }
}
} onCancel: {
cancelMutex.withLock { $0?() }
}
}
+1 -1
View File
@@ -408,7 +408,7 @@ extension DatabaseQueue: DatabaseWriter {
}
public func barrierWriteWithoutTransaction<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
_ updates: sending (Database) throws -> T
) async throws -> T {
try await writer.execute(updates)
}
+2 -2
View File
@@ -213,7 +213,7 @@ public protocol DatabaseWriter: DatabaseReader {
/// database access, or the error thrown by `updates`, or
/// `CancellationError` if the task is cancelled.
func barrierWriteWithoutTransaction<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
_ updates: sending (Database) throws -> T
) async throws -> T
/// Schedules database operations for execution, and returns immediately.
@@ -947,7 +947,7 @@ extension AnyDatabaseWriter: DatabaseWriter {
}
public func barrierWriteWithoutTransaction<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
_ updates: sending (Database) throws -> T
) async throws -> T {
try await base.barrierWriteWithoutTransaction(updates)
}