#if os(Linux) @preconcurrency import Dispatch #else import Dispatch #endif /// An actor that runs in a DispatchQueue. /// /// Inspired by actor DispatchQueueActor { private let executor: DispatchQueueExecutor /// - precondition: the queue is serial, or flags contains `.barrier`. init(queue: DispatchQueue, flags: DispatchWorkItemFlags = []) { self.executor = DispatchQueueExecutor(queue: queue, flags: flags) } nonisolated var unownedExecutor: UnownedSerialExecutor { executor.asUnownedSerialExecutor() } func execute(_ work: () throws -> T) rethrows -> T { try work() } } private final class DispatchQueueExecutor: SerialExecutor { private let queue: DispatchQueue private let flags: DispatchWorkItemFlags init(queue: DispatchQueue, flags: DispatchWorkItemFlags) { self.queue = queue self.flags = flags } func enqueue(_ job: UnownedJob) { queue.async(flags: flags) { job.runSynchronously(on: self.asUnownedSerialExecutor()) } } func asUnownedSerialExecutor() -> UnownedSerialExecutor { UnownedSerialExecutor(ordinary: self) } func checkIsolated() { dispatchPrecondition(condition: .onQueue(queue)) } } #if os(Linux) || os(Android) || os(Windows) extension DispatchQueueExecutor: @unchecked Sendable {} #endif