mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
DispatchQueue.asyncSending
This commit is contained in:
1 parent
f900990c5a
commit
7b075e6b3a
3 files changed
+23
-21
No files matched your search
@@ -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 <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5>.
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
+4
-14
@@ -124,19 +124,14 @@ final class Pool<T: Sendable>: Sendable {
|
||||
func asyncGet(
|
||||
_ execute: sending @escaping (Result<ElementAndRelease, Error>) -> Void
|
||||
) {
|
||||
// DispatchQueue does not accept a sending closure yet, as
|
||||
// discussed at <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5>.
|
||||
// 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<T: Sendable>: Sendable {
|
||||
func asyncBarrier(
|
||||
execute barrier: sending @escaping () -> Void
|
||||
) {
|
||||
// DispatchQueue does not accept a sending closure yet, as
|
||||
// discussed at <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5>.
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Dispatch
|
||||
import Foundation
|
||||
|
||||
// MARK: - Public
|
||||
@@ -207,6 +208,22 @@ class StrongReference<Value: AnyObject>: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
extension DispatchQueue {
|
||||
func asyncSending(
|
||||
flags: DispatchWorkItemFlags = [],
|
||||
execute work: sending @escaping () -> Void
|
||||
) {
|
||||
// DispatchQueue does not accept a sending closure yet, as
|
||||
// discussed at <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5>.
|
||||
// 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<Value>: @unchecked Sendable {
|
||||
let value: Value
|
||||
|
||||
Reference in new issue
Block a user