Skip to content

Instantly share code, notes, and snippets.

@Joannis
Last active February 7, 2025 13:35
Show Gist options
  • Select an option

  • Save Joannis/516cf0489da6aed957b363030830249e to your computer and use it in GitHub Desktop.

Select an option

Save Joannis/516cf0489da6aed957b363030830249e to your computer and use it in GitHub Desktop.
@available(macOS 14, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
final class DispatchSerialExecutor: SerialExecutor {
let queue: DispatchQueue
init(queue: DispatchQueue) {
self.queue = queue
}
init() {
self.queue = DispatchQueue(
label: "software.unbeatable.dispatch-serial-executor",
attributes: .concurrent
)
}
static let global = DispatchSerialExecutor(queue: .global())
func enqueue(_ job: UnownedJob) {
queue.async { [job] in
job.runSynchronously(on: self.asUnownedSerialExecutor())
}
}
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
UnownedSerialExecutor(ordinary: self)
}
}
@globalActor fileprivate actor GlobalQueueActor {
static let shared = GlobalQueueActor()
nonisolated private let executor = DispatchSerialExecutor.global
nonisolated var unownedExecutor: UnownedSerialExecutor {
executor.asUnownedSerialExecutor()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment