diff --git a/GRDB/Core/Concurrency/SerializedDatabase.swift b/GRDB/Core/Concurrency/SerializedDatabase.swift index e55323e22..59cc22d8e 100644 --- a/GRDB/Core/Concurrency/SerializedDatabase.swift +++ b/GRDB/Core/Concurrency/SerializedDatabase.swift @@ -226,13 +226,8 @@ final class SerializedDatabase { func async( _ block: sending @escaping (Database) -> Void ) { - // DispatchQueue does not accept a sending closure yet, as - // discussed at . - // So let's wrap the closure in a Sendable wrapper. - let block = UncheckedSendableWrapper(value: block) - - queue.async { - block.value(self.db) + queue.asyncSending { + block(self.db) self.preconditionNoUnsafeTransactionLeft(self.db) } } diff --git a/GRDB/Utils/Pool.swift b/GRDB/Utils/Pool.swift index d245456a2..cb4af1810 100644 --- a/GRDB/Utils/Pool.swift +++ b/GRDB/Utils/Pool.swift @@ -124,19 +124,14 @@ final class Pool: Sendable { func asyncGet( _ execute: sending @escaping (Result) -> Void ) { - // DispatchQueue does not accept a sending closure yet, as - // discussed at . - // So let's wrap the closure in a Sendable wrapper. - let execute = UncheckedSendableWrapper(value: execute) - // Inspired by https://khanlou.com/2016/04/the-GCD-handbook/ // > We wait on the semaphore in the serial queue, which means that // > we’ll have at most one blocked thread when we reach maximum // > executing blocks on the concurrent queue. Any other tasks the user // > enqueues will sit inertly on the serial queue waiting to be // > executed, and won’t cause new threads to be started. - semaphoreWaitingQueue.async { - execute.value(Result { try self.get() }) + semaphoreWaitingQueue.asyncSending { + execute(Result { try self.get() }) } } @@ -196,14 +191,9 @@ final class Pool: Sendable { func asyncBarrier( execute barrier: sending @escaping () -> Void ) { - // DispatchQueue does not accept a sending closure yet, as - // discussed at . - // So let's wrap the closure in a Sendable wrapper. - let barrier = UncheckedSendableWrapper(value: barrier) - - barrierQueue.async(flags: [.barrier]) { + barrierQueue.asyncSending(flags: [.barrier]) { self.itemsGroup.wait() - barrier.value() + barrier() } } } diff --git a/GRDB/Utils/Utils.swift b/GRDB/Utils/Utils.swift index 578def5af..e92cc7339 100644 --- a/GRDB/Utils/Utils.swift +++ b/GRDB/Utils/Utils.swift @@ -1,3 +1,4 @@ +import Dispatch import Foundation // MARK: - Public @@ -207,6 +208,22 @@ class StrongReference: @unchecked Sendable { } } +extension DispatchQueue { + func asyncSending( + flags: DispatchWorkItemFlags = [], + execute work: sending @escaping () -> Void + ) { + // DispatchQueue does not accept a sending closure yet, as + // discussed at . + // So let's wrap the closure in a Sendable wrapper. + let work = UncheckedSendableWrapper(value: work) + + self.async { + work.value() + } + } +} + /// Helps compiler accept valid code it can't validate. struct UncheckedSendableWrapper: @unchecked Sendable { let value: Value