mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +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(
|
func async(
|
||||||
_ block: sending @escaping (Database) -> Void
|
_ block: sending @escaping (Database) -> Void
|
||||||
) {
|
) {
|
||||||
// DispatchQueue does not accept a sending closure yet, as
|
queue.asyncSending {
|
||||||
// discussed at <https://forums.swift.org/t/how-can-i-use-region-based-isolation/71426/5>.
|
block(self.db)
|
||||||
// So let's wrap the closure in a Sendable wrapper.
|
|
||||||
let block = UncheckedSendableWrapper(value: block)
|
|
||||||
|
|
||||||
queue.async {
|
|
||||||
block.value(self.db)
|
|
||||||
self.preconditionNoUnsafeTransactionLeft(self.db)
|
self.preconditionNoUnsafeTransactionLeft(self.db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-14
@@ -124,19 +124,14 @@ final class Pool<T: Sendable>: Sendable {
|
|||||||
func asyncGet(
|
func asyncGet(
|
||||||
_ execute: sending @escaping (Result<ElementAndRelease, Error>) -> Void
|
_ 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/
|
// Inspired by https://khanlou.com/2016/04/the-GCD-handbook/
|
||||||
// > We wait on the semaphore in the serial queue, which means that
|
// > We wait on the semaphore in the serial queue, which means that
|
||||||
// > we’ll have at most one blocked thread when we reach maximum
|
// > we’ll have at most one blocked thread when we reach maximum
|
||||||
// > executing blocks on the concurrent queue. Any other tasks the user
|
// > executing blocks on the concurrent queue. Any other tasks the user
|
||||||
// > enqueues will sit inertly on the serial queue waiting to be
|
// > enqueues will sit inertly on the serial queue waiting to be
|
||||||
// > executed, and won’t cause new threads to be started.
|
// > executed, and won’t cause new threads to be started.
|
||||||
semaphoreWaitingQueue.async {
|
semaphoreWaitingQueue.asyncSending {
|
||||||
execute.value(Result { try self.get() })
|
execute(Result { try self.get() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,14 +191,9 @@ final class Pool<T: Sendable>: Sendable {
|
|||||||
func asyncBarrier(
|
func asyncBarrier(
|
||||||
execute barrier: sending @escaping () -> Void
|
execute barrier: sending @escaping () -> Void
|
||||||
) {
|
) {
|
||||||
// DispatchQueue does not accept a sending closure yet, as
|
barrierQueue.asyncSending(flags: [.barrier]) {
|
||||||
// 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]) {
|
|
||||||
self.itemsGroup.wait()
|
self.itemsGroup.wait()
|
||||||
barrier.value()
|
barrier()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import Dispatch
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
// MARK: - Public
|
// 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.
|
/// Helps compiler accept valid code it can't validate.
|
||||||
struct UncheckedSendableWrapper<Value>: @unchecked Sendable {
|
struct UncheckedSendableWrapper<Value>: @unchecked Sendable {
|
||||||
let value: Value
|
let value: Value
|
||||||
|
|||||||
Reference in new issue
Block a user