@@ -194,19 +194,44 @@ extension Task {
194
194
}
195
195
}
196
196
197
- /// Indicates that the task should stop running .
197
+ /// Cancels this task.
198
198
///
199
- /// Task cancellation is cooperative:
200
- /// a task that supports cancellation
201
- /// checks whether it has been canceled at various points during its work.
199
+ /// Cancelling a task has three primary effects:
202
200
///
203
- /// Calling this method on a task that doesn't support cancellation
204
- /// has no effect.
205
- /// Likewise, if the task has already run
206
- /// past the last point where it would stop early,
207
- /// calling this method has no effect.
201
+ /// - It flags the task as cancelled.
202
+ /// - It causes any active cancellation handlers on the task to run (once).
203
+ /// - It cancels any child tasks and task groups of the task, including
204
+ /// those created in the future. If those tasks have cancellation handlers,
205
+ /// they also are triggered.
206
+ ///
207
+ /// Task cancellation is cooperative and idempotent.
208
+ ///
209
+ /// Cancelling a task does not automatically cause arbitrary functions on the task
210
+ /// to stop running or throw errors. A function _may_ choose to react
211
+ /// to cancellation by ending its work early, and it is conventional to
212
+ /// signal that to callers by throwing CancellationError. However,
213
+ /// a function that doesn't specifically check for cancellation will
214
+ /// run to completion normally even if the task it is running on is
215
+ /// cancelled. (Of course, it may still end early if it calls something
216
+ /// else that handles cancellation by throwing and then doesn't
217
+ /// handle the error.)
218
+ ///
219
+ /// It is safe to cancel a task from any task or thread. It is safe for
220
+ /// multiple tasks or threads to cancel the same task at the same
221
+ /// time. Cancelling a task that has already been cancelled has no
222
+ /// additional effect.
223
+ ///
224
+ /// `cancel` may need to acquire locks and synchronously run
225
+ /// arbitrary cancellation-handler code associated with the
226
+ /// cancelled task. To reduce the risk of deadlock, it is
227
+ /// recommended that callers release any locks they might be
228
+ /// holding before they call cancel.
229
+ ///
230
+ /// If the task has already run past the last point where it could have
231
+ /// performed a cancellation check, cancelling it may have no observable effects.
208
232
///
209
233
/// - SeeAlso: `Task.checkCancellation()`
234
+ /// - SeeAlso: `withTaskCancellationHandler(operation:onCancel:isolation:)`
210
235
public func cancel( ) {
211
236
Builtin . cancelAsyncTask ( _task)
212
237
}
0 commit comments