-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoroutine.swift
49 lines (39 loc) · 1.96 KB
/
Coroutine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Dispatch
private let coroutineQueue = DispatchQueue(label: "coroutine",
qos: DispatchQoS.default,
attributes: DispatchQueue.Attributes.concurrent,
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
target: nil)
public class Coroutine<Element>: IteratorProtocol {
private let callerReady = DispatchSemaphore(value: 0)
private let coroutineReady = DispatchSemaphore(value: 0)
private var done: Bool = false
private var transportStorage: Element?
public typealias Yield = (Element) -> ()
public init(implementation: @escaping (Yield) -> ()) {
coroutineQueue.async {
// Don't start coroutine until first call.
_ = self.callerReady.wait(timeout: DispatchTime(uptimeNanoseconds: UInt64.max))
implementation { next in
// Place element in transport storage, and let caller know it's ready.
self.transportStorage = next
self.coroutineReady.signal()
// Don't continue coroutine until next call.
_ = self.callerReady.wait(timeout: DispatchTime(uptimeNanoseconds: UInt64.max))
}
// The coroutine is forever over, so let's let the caller know.
self.done = true
self.coroutineReady.signal()
}
}
public func next() -> Element? {
// Make sure work is happening before we wait.
guard !done else { return nil }
// Return to the coroutine.
callerReady.signal()
// Wait until it has finished, then return and clear the result.
_ = coroutineReady.wait(timeout: DispatchTime(uptimeNanoseconds: UInt64.max))
defer { transportStorage = nil }
return transportStorage
}
}