|
12 | 12 | // |
13 | 13 | //===----------------------------------------------------------------------===// |
14 | 14 |
|
15 | | -// MARK: - Public Interface for Non-Async Usage - |
16 | | - |
17 | | -/// `DispatchGroup` is a drop-in replacement for the `DispatchGroup` implemented |
18 | | -/// in Grand Central Dispatch. However, this class uses Swift Concurrency, instead of low-level threading API's. |
19 | | -/// |
20 | | -/// The primary goal of this implementation is to enable WASM support for Dispatch. |
21 | | -/// |
22 | | -/// Refer to documentation for the original [DispatchGroup](https://developer.apple.com/documentation/dispatch/dispatchgroup) |
23 | | -/// for more details, |
24 | | -@available(macOS 10.15, *) |
25 | | -public class DispatchGroup: @unchecked Sendable { |
26 | | - private let group = _AsyncGroup() |
27 | | - private let queue = FIFOQueue() |
28 | | - |
29 | | - public func enter() { |
30 | | - queue.enqueue { [weak self] in |
31 | | - guard let self else { return } |
32 | | - await group.enter() |
| 15 | +// NOTE: The following typealias mirrors Dispatch API's, but only for |
| 16 | +// specific compilation conditions where Dispatch is not available. |
| 17 | +// It is designed to safely elide away if and when Dispatch is introduced |
| 18 | +// in the required Dispatch support becomes available. |
| 19 | +#if os(WASI) && !canImport(Dispatch) |
| 20 | +/// Drop-in replacement for ``Dispatch.DispatchGroup``, implemented using pure swift. |
| 21 | +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
| 22 | +public typealias DispatchGroup = DispatchAsync.DispatchGroup |
| 23 | +#endif |
| 24 | + |
| 25 | +extension DispatchAsync { |
| 26 | + // MARK: - Public Interface for Non-Async Usage - |
| 27 | + |
| 28 | + /// Drop-in replacement for ``Dispatch.DispatchGroup``, implemented using pure swift. |
| 29 | + /// |
| 30 | + /// The primary goal of this implementation is to enable WASM support for Dispatch. |
| 31 | + /// |
| 32 | + /// Refer to documentation for the original [DispatchGroup](https://developer.apple.com/documentation/dispatch/dispatchgroup) |
| 33 | + /// for more details, |
| 34 | + #if !os(WASI) |
| 35 | + @_spi(DispatchAsync) |
| 36 | + #endif |
| 37 | + @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
| 38 | + public class DispatchGroup: @unchecked Sendable { |
| 39 | + private let group = _AsyncGroup() |
| 40 | + private let queue = DispatchAsync.FIFOQueue() |
| 41 | + |
| 42 | + public func enter() { |
| 43 | + queue.enqueue { [weak self] in |
| 44 | + guard let self else { return } |
| 45 | + await group.enter() |
| 46 | + } |
33 | 47 | } |
34 | | - } |
35 | 48 |
|
36 | | - public func leave() { |
37 | | - queue.enqueue { [weak self] in |
38 | | - guard let self else { return } |
39 | | - await group.leave() |
| 49 | + public func leave() { |
| 50 | + queue.enqueue { [weak self] in |
| 51 | + guard let self else { return } |
| 52 | + await group.leave() |
| 53 | + } |
40 | 54 | } |
41 | | - } |
42 | 55 |
|
43 | | - public func notify(queue notificationQueue: DispatchQueue, execute work: @escaping @Sendable @convention(block) () -> Void) { |
44 | | - queue.enqueue { [weak self] in |
45 | | - guard let self else { return } |
46 | | - await group.notify { |
47 | | - await withCheckedContinuation { continuation in |
48 | | - notificationQueue.async { |
49 | | - work() |
50 | | - continuation.resume() |
| 56 | + public func notify( |
| 57 | + queue notificationQueue: DispatchAsync.DispatchQueue, |
| 58 | + execute work: @escaping @Sendable @convention(block) () -> Void |
| 59 | + ) { |
| 60 | + queue.enqueue { [weak self] in |
| 61 | + guard let self else { return } |
| 62 | + await group.notify { |
| 63 | + await withCheckedContinuation { continuation in |
| 64 | + notificationQueue.async { |
| 65 | + work() |
| 66 | + continuation.resume() |
| 67 | + } |
51 | 68 | } |
52 | 69 | } |
53 | 70 | } |
54 | 71 | } |
55 | | - } |
56 | 72 |
|
57 | | - func wait() async { |
58 | | - await withCheckedContinuation { continuation in |
59 | | - queue.enqueue { [weak self] in |
60 | | - guard let self else { return } |
61 | | - // NOTE: We use a task for the wait, because |
62 | | - // otherwise the queue won't execute any more |
63 | | - // tasks until the wait finishes, which is not the |
64 | | - // behavior we want here. We want to enqueue the wait |
65 | | - // in FIFO call order, but then we want to allow the wait |
66 | | - // to be non-blocking for the queue until the last leave |
67 | | - // is called on the group. |
68 | | - Task { |
69 | | - await group.wait() |
70 | | - continuation.resume() |
| 73 | + func wait() async { |
| 74 | + await withCheckedContinuation { continuation in |
| 75 | + queue.enqueue { [weak self] in |
| 76 | + guard let self else { return } |
| 77 | + // NOTE: We use a task for the wait, because |
| 78 | + // otherwise the queue won't execute any more |
| 79 | + // tasks until the wait finishes, which is not the |
| 80 | + // behavior we want here. We want to enqueue the wait |
| 81 | + // in FIFO call order, but then we want to allow the wait |
| 82 | + // to be non-blocking for the queue until the last leave |
| 83 | + // is called on the group. |
| 84 | + Task { |
| 85 | + await group.wait() |
| 86 | + continuation.resume() |
| 87 | + } |
71 | 88 | } |
72 | 89 | } |
73 | 90 | } |
| 91 | + |
| 92 | + public init() {} |
74 | 93 | } |
75 | 94 |
|
76 | | - public init() {} |
77 | | -} |
| 95 | + // MARK: - Private Interface for Async Usage - |
78 | 96 |
|
79 | | -// MARK: - Private Interface for Async Usage - |
| 97 | + #if !os(WASI) |
| 98 | + @_spi(DispatchAsync) |
| 99 | + #endif |
| 100 | + @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
| 101 | + fileprivate actor _AsyncGroup { |
| 102 | + private var taskCount = 0 |
| 103 | + private var notifyHandlers: [@Sendable () async -> Void] = [] |
80 | 104 |
|
81 | | -@available(macOS 10.15, *) |
82 | | -fileprivate actor _AsyncGroup { |
83 | | - private var taskCount = 0 |
84 | | - private var notifyHandlers: [@Sendable () async -> Void] = [] |
| 105 | + func enter() { |
| 106 | + taskCount += 1 |
| 107 | + } |
85 | 108 |
|
86 | | - func enter() { |
87 | | - taskCount += 1 |
88 | | - } |
| 109 | + func leave() { |
| 110 | + defer { |
| 111 | + checkCompletion() |
| 112 | + } |
| 113 | + guard taskCount > 0 else { |
| 114 | + assertionFailure("leave() called more times than enter()") |
| 115 | + return |
| 116 | + } |
| 117 | + taskCount -= 1 |
| 118 | + } |
89 | 119 |
|
90 | | - func leave() { |
91 | | - defer { |
| 120 | + func notify(handler: @escaping @Sendable () async -> Void) { |
| 121 | + notifyHandlers.append(handler) |
92 | 122 | checkCompletion() |
93 | 123 | } |
94 | | - guard taskCount > 0 else { |
95 | | - assertionFailure("leave() called more times than enter()") |
96 | | - return |
97 | | - } |
98 | | - taskCount -= 1 |
99 | | - } |
100 | 124 |
|
101 | | - func notify(handler: @escaping @Sendable () async -> Void) { |
102 | | - notifyHandlers.append(handler) |
103 | | - checkCompletion() |
104 | | - } |
105 | | - |
106 | | - func wait() async { |
107 | | - if taskCount <= 0 { |
108 | | - return |
109 | | - } |
| 125 | + func wait() async { |
| 126 | + if taskCount <= 0 { |
| 127 | + return |
| 128 | + } |
110 | 129 |
|
111 | | - await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in |
112 | | - notify { |
113 | | - continuation.resume() |
| 130 | + await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in |
| 131 | + notify { |
| 132 | + continuation.resume() |
| 133 | + } |
| 134 | + checkCompletion() |
114 | 135 | } |
115 | | - checkCompletion() |
116 | 136 | } |
117 | | - } |
118 | 137 |
|
119 | | - private func checkCompletion() { |
120 | | - if taskCount <= 0, !notifyHandlers.isEmpty { |
121 | | - let handlers = notifyHandlers |
122 | | - notifyHandlers.removeAll() |
| 138 | + private func checkCompletion() { |
| 139 | + if taskCount <= 0, !notifyHandlers.isEmpty { |
| 140 | + let handlers = notifyHandlers |
| 141 | + notifyHandlers.removeAll() |
123 | 142 |
|
124 | | - for handler in handlers { |
125 | | - Task { |
126 | | - await handler() |
| 143 | + for handler in handlers { |
| 144 | + Task { |
| 145 | + await handler() |
| 146 | + } |
127 | 147 | } |
128 | 148 | } |
129 | 149 | } |
|
0 commit comments