-
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathAsyncAwait.swift
More file actions
238 lines (208 loc) · 6.53 KB
/
AsyncAwait.swift
File metadata and controls
238 lines (208 loc) · 6.53 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#if !os(WASI)
import Dispatch
import Foundation
#if canImport(Testing)
@_implementationOnly import Testing
#endif
private let timeoutLeeway = NimbleTimeInterval.milliseconds(1)
private let pollLeeway = NimbleTimeInterval.milliseconds(1)
// Like PollResult, except it doesn't support objective-c exceptions.
// Which is tolerable because Swift Concurrency doesn't support recording objective-c exceptions.
internal enum AsyncPollResult<T> {
/// Incomplete indicates None (aka - this value hasn't been fulfilled yet)
case incomplete
/// TimedOut indicates the result reached its defined timeout limit before returning
case timedOut
/// The async block successfully executed and returned a given result
case completed(T)
/// When a Swift Error is thrown
case errorThrown(Error)
func isIncomplete() -> Bool {
switch self {
case .incomplete: return true
default: return false
}
}
func isCompleted() -> Bool {
switch self {
case .completed: return true
default: return false
}
}
func toPollResult() -> PollResult<T> {
switch self {
case .incomplete: return .incomplete
case .timedOut: return .timedOut
case .completed(let value): return .completed(value)
case .errorThrown(let error): return .errorThrown(error)
}
}
}
private final class BlockingTask: Sendable {
private nonisolated(unsafe) var finished = false
private nonisolated(unsafe) var continuation: CheckedContinuation<Void, Never>? = nil
let sourceLocation: SourceLocation
private let lock = NSLock()
init(sourceLocation: SourceLocation) {
self.sourceLocation = sourceLocation
}
func run() async {
let continuation: CheckedContinuation<Void, Never>? = {
lock.lock()
let continuation = self.continuation
lock.unlock()
return continuation
}()
if let continuation {
continuation.resume()
}
await withTaskCancellationHandler {
await withCheckedContinuation {
lock.lock()
let shouldResume: Bool
if finished {
shouldResume = true
} else {
self.continuation = $0
shouldResume = false
}
lock.unlock()
if shouldResume {
$0.resume()
}
}
} onCancel: {
handleCancellation()
}
}
func complete() {
lock.lock()
let wasFinished = finished
finished = true
lock.unlock()
if wasFinished {
fail(
"waitUntil(...) expects its completion closure to be only called once",
location: sourceLocation
)
} else {
self.continuation?.resume()
self.continuation = nil
}
}
func handleCancellation() {
lock.lock()
let wasFinished = finished
lock.unlock()
guard wasFinished == false else {
return
}
continuation?.resume()
continuation = nil
}
}
final class ResultTracker<T: Sendable>: Sendable {
var result: AsyncPollResult<T> {
lock.lock()
defer { lock.unlock() }
return _result
}
private nonisolated(unsafe) var _result: AsyncPollResult<T> = .incomplete
private let lock = NSLock()
func finish(with result: AsyncPollResult<T>) {
lock.lock()
defer {
lock.unlock()
}
guard case .incomplete = _result else {
return
}
self._result = result
}
}
internal func performBlock(
timeout: NimbleTimeInterval,
leeway: NimbleTimeInterval,
sourceLocation: SourceLocation,
closure: @escaping @Sendable (@escaping @Sendable () -> Void) async throws -> Void
) async -> AsyncPollResult<Void> {
precondition(timeout > .seconds(0))
#if canImport(Testing)
#if swift(>=6.3)
Issue.record(
"waitUntil(...) becomes less reliable the more tasks and processes your system is running. " +
"This makes it unsuitable for use with Swift Testing. Please use Swift Testing's confirmation(...) API instead.",
severity: .warning,
sourceLocation: SourceLocation(
fileID: sourceLocation.fileID,
filePath: sourceLocation.filePath,
line: sourceLocation.line,
column: sourceLocation.column
)
)
#endif
#endif
return await withTaskGroup(of: Void.self, returning: AsyncPollResult<Void>.self) { taskGroup in
let blocker = BlockingTask(sourceLocation: sourceLocation)
let tracker = ResultTracker<Void>()
taskGroup.addTask {
await blocker.run()
}
let task = Task {
do {
try await closure {
blocker.complete()
tracker.finish(with: .completed(()))
}
} catch {
tracker.finish(with: .errorThrown(error))
}
}
taskGroup.addTask {
do {
try await Task.sleep(nanoseconds: (timeout + leeway).nanoseconds)
tracker.finish(with: .timedOut)
} catch {}
}
var result: AsyncPollResult<Void> = .incomplete
for await _ in taskGroup {
result = tracker.result
if case .incomplete = result {
continue
}
break
}
taskGroup.cancelAll()
task.cancel()
return result
}
}
internal func pollBlock(
pollInterval: NimbleTimeInterval,
timeoutInterval: NimbleTimeInterval,
sourceLocation: SourceLocation,
expression: @escaping () async throws -> PollStatus
) async -> AsyncPollResult<Bool> {
precondition(timeoutInterval > pollInterval)
precondition(pollInterval > .seconds(0))
let iterations = Int(exactly: (timeoutInterval / pollInterval).rounded(.up)) ?? Int.max
for iteration in 0..<iterations {
do {
if case .finished(let result) = try await expression() {
return .completed(result)
}
} catch {
return .errorThrown(error)
}
if iteration == (iterations - 1) {
break
}
do {
try await Task.sleep(nanoseconds: pollInterval.nanoseconds)
} catch {
return .errorThrown(error)
}
}
return .timedOut
}
#endif // #if !os(WASI)