-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTaskSleep.swift
354 lines (303 loc) · 12 KB
/
TaskSleep.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Swift
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@available(SwiftStdlib 5.1, *)
@_unavailableInEmbedded
extension Task where Success == Never, Failure == Never {
@available(*, deprecated, renamed: "Task.sleep(nanoseconds:)")
/// Suspends the current task for at least the given duration
/// in nanoseconds.
///
/// This function doesn't block the underlying thread.
public static func sleep(_ duration: UInt64) async {
return await Builtin.withUnsafeContinuation {
(continuation: Builtin.RawUnsafeContinuation) -> Void in
let job = _taskCreateNullaryContinuationJob(
priority: Int(Task.currentPriority.rawValue),
continuation: continuation)
if #available(SwiftStdlib 6.2, *) {
#if !$Embedded
if let executor = Task.currentSchedulableExecutor {
executor.enqueue(ExecutorJob(context: job),
after: .nanoseconds(duration),
clock: .continuous)
return
}
#endif
}
// If there is no current schedulable executor, fall back to
// _enqueueJobGlobalWithDelay()
_enqueueJobGlobalWithDelay(duration, job)
}
}
/// The type of continuation used in the implementation of
/// sleep(nanoseconds:).
typealias SleepContinuation = UnsafeContinuation<(), Error>
/// Describes the state of a sleep() operation.
@unsafe enum SleepState {
/// The sleep continuation has not yet begun.
case notStarted
// The sleep continuation has been created and is available here.
case activeContinuation(SleepContinuation)
/// The sleep has finished.
case finished
/// The sleep was canceled.
case cancelled
/// The sleep was canceled before it even got started.
case cancelledBeforeStarted
/// Decode sleep state from the word of storage.
init(word: Builtin.Word) {
switch UInt(word) & 0x03 {
case 0:
let continuationBits = UInt(word) & ~0x03
if continuationBits == 0 {
unsafe self = unsafe .notStarted
} else {
let continuation = unsafe unsafeBitCast(
continuationBits, to: SleepContinuation.self)
unsafe self = unsafe .activeContinuation(continuation)
}
case 1:
unsafe self = unsafe .finished
case 2:
unsafe self = unsafe .cancelled
case 3:
unsafe self = unsafe .cancelledBeforeStarted
default:
fatalError("Bitmask failure")
}
}
/// Decode sleep state by loading from the given pointer
init(loading wordPtr: UnsafeMutablePointer<Builtin.Word>) {
unsafe self.init(word: Builtin.atomicload_seqcst_Word(wordPtr._rawValue))
}
/// Encode sleep state into a word of storage.
var word: UInt {
switch unsafe self {
case .notStarted:
return 0
case .activeContinuation(let continuation):
let continuationBits = unsafe unsafeBitCast(continuation, to: UInt.self)
return continuationBits
case .finished:
return 1
case .cancelled:
return 2
case .cancelledBeforeStarted:
return 3
}
}
}
/// A simple wrapper for a pointer to heap allocated storage of a `SleepState`
/// value. This wrapper is `Sendable` because it facilitates atomic load and
/// exchange operations on the underlying storage. However, this wrapper is also
/// _unsafe_ because the owner must manually deallocate the token once it is no
/// longer needed.
@unsafe struct UnsafeSleepStateToken: @unchecked Sendable {
let wordPtr: UnsafeMutablePointer<Builtin.Word>
/// Allocates the underlying storage and sets the value to `.notStarted`.
init() {
unsafe wordPtr = .allocate(capacity: 1)
unsafe Builtin.atomicstore_seqcst_Word(
wordPtr._rawValue, SleepState.notStarted.word._builtinWordValue)
}
/// Atomically loads the current state.
func load() -> SleepState {
return unsafe SleepState(word: Builtin.atomicload_seqcst_Word(wordPtr._rawValue))
}
/// Attempts to atomically set the stored value to `desired` if the current
/// value is equal to `expected`. Returns true if the exchange was successful.
func exchange(expected: SleepState, desired: SleepState) -> Bool {
let (_, won) = unsafe Builtin.cmpxchg_seqcst_seqcst_Word(
wordPtr._rawValue,
expected.word._builtinWordValue,
desired.word._builtinWordValue)
return Bool(_builtinBooleanLiteral: won)
}
/// Deallocates the underlying storage.
func deallocate() {
unsafe wordPtr.deallocate()
}
}
/// Called when the sleep(nanoseconds:) operation woke up without being
/// canceled.
static func onSleepWake(_ token: UnsafeSleepStateToken) {
while true {
let state = unsafe token.load()
switch unsafe state {
case .notStarted:
fatalError("Cannot wake before we even started")
case .activeContinuation(let continuation):
// We have an active continuation, so try to transition to the
// "finished" state.
if unsafe token.exchange(expected: state, desired: .finished) {
// The sleep finished, so invoke the continuation: we're done.
unsafe continuation.resume()
return
}
// Try again!
continue
case .finished:
fatalError("Already finished normally, can't do that again")
case .cancelled:
// The task was cancelled, which means the continuation was
// called by the cancellation handler. We need to deallocate the token
// because it was left over for this task to complete.
unsafe token.deallocate()
return
case .cancelledBeforeStarted:
// Nothing to do;
return
}
}
}
/// Called when the sleep(nanoseconds:) operation has been canceled before
/// the sleep completed.
static func onSleepCancel(_ token: UnsafeSleepStateToken) {
while true {
let state = unsafe token.load()
switch unsafe state {
case .notStarted:
// We haven't started yet, so try to transition to the cancelled-before
// started state.
if unsafe token.exchange(expected: state, desired: .cancelledBeforeStarted) {
return
}
// Try again!
continue
case .activeContinuation(let continuation):
// We have an active continuation, so try to transition to the
// "cancelled" state.
if unsafe token.exchange(expected: state, desired: .cancelled) {
// We recorded the task cancellation before the sleep finished, so
// invoke the continuation with the cancellation error.
unsafe continuation.resume(throwing: _Concurrency.CancellationError())
return
}
// Try again!
continue
case .finished, .cancelled, .cancelledBeforeStarted:
// The operation already finished, so there is nothing more to do.
return
}
}
}
/// Suspends the current task for at least the given duration
/// in nanoseconds.
///
/// If the task is canceled before the time ends,
/// this function throws `CancellationError`.
///
/// This function doesn't block the underlying thread.
public static func sleep(nanoseconds duration: UInt64) async throws {
// Create a token which will initially have the value "not started", which
// means the continuation has neither been created nor completed.
let token = unsafe UnsafeSleepStateToken()
do {
// Install a cancellation handler to resume the continuation by
// throwing CancellationError.
try await withTaskCancellationHandler {
let _: () = try unsafe await withUnsafeThrowingContinuation { continuation in
while true {
let state = unsafe token.load()
switch unsafe state {
case .notStarted:
// Try to swap in the continuation state.
let newState = unsafe SleepState.activeContinuation(continuation)
if unsafe !token.exchange(expected: state, desired: newState) {
// Keep trying!
continue
}
// Create a task that resumes the continuation normally if it
// finishes first. Enqueue it directly with the delay, so it fires
// when we're done sleeping.
let sleepTaskFlags = taskCreateFlags(
priority: nil, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: false,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false, isSynchronousStart: false)
let (sleepTask, _) = Builtin.createAsyncTask(sleepTaskFlags) {
unsafe onSleepWake(token)
}
let job = Builtin.convertTaskToJob(sleepTask)
if #available(SwiftStdlib 6.2, *) {
#if !$Embedded
if let executor = Task.currentSchedulableExecutor {
executor.enqueue(ExecutorJob(context: job),
after: .nanoseconds(duration),
clock: .continuous)
return
}
#endif
}
// If there is no current schedulable executor, fall back to
// _enqueueJobGlobalWithDelay()
_enqueueJobGlobalWithDelay(duration, job)
return
case .activeContinuation, .finished:
fatalError("Impossible to have multiple active continuations")
case .cancelled:
fatalError("Impossible to have cancelled before we began")
case .cancelledBeforeStarted:
// Finish the continuation normally. We'll throw later, after
// we clean up.
unsafe continuation.resume()
return
}
}
}
} onCancel: {
unsafe onSleepCancel(token)
}
// Determine whether we got cancelled before we even started.
let cancelledBeforeStarted: Bool
switch unsafe token.load() {
case .notStarted, .activeContinuation, .cancelled:
fatalError("Invalid state for non-cancelled sleep task")
case .cancelledBeforeStarted:
cancelledBeforeStarted = true
case .finished:
cancelledBeforeStarted = false
}
// We got here without being cancelled, so deallocate the storage for
// the flag word and continuation.
unsafe token.deallocate()
// If we got cancelled before we even started, through the cancellation
// error now.
if cancelledBeforeStarted {
throw _Concurrency.CancellationError()
}
} catch {
// The task was cancelled; propagate the error. The "on wake" task is
// responsible for deallocating the flag word and continuation, if it's
// still running.
throw error
}
}
}
#else
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
extension Task where Success == Never, Failure == Never {
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public static func sleep(_ duration: UInt64) async {
fatalError("Unavailable in task-to-thread concurrency model")
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
public static func sleep(nanoseconds duration: UInt64) async throws {
fatalError("Unavailable in task-to-thread concurrency model")
}
}
#endif