@@ -194,33 +194,44 @@ extension Task {
194
194
}
195
195
}
196
196
197
- /// Marks this task as canceled .
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.
202
- /// You determine what action a canceled task takes,
203
- /// and whether cancellation does anything to that task.
204
- /// The task can return early or throw an error
205
- /// to stop running early.
199
+ /// Cancelling a task has three primary effects:
206
200
///
207
- /// Calling this method has only one effect:
208
- /// setting the canceled flag on the task or its child tasks.
209
- /// Unless the tasks react to this flag,
210
- /// cancellation doesn't change the behavior of the tasks.
211
- /// Specifically, tasks are never forcefully interrupted or stopped,
212
- /// and are always allowed to execute code until they return or throw an error.
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.
213
206
///
214
- /// Likewise, if the task has already run
215
- /// past the last point where it would stop early,
216
- /// calling this method has no effect.
207
+ /// Task cancellation is cooperative and idempotent.
217
208
///
218
- /// It is safe to call `cancel` from any task or thread.
219
- /// The `cancel` call is idempotent, and may be called multiple times,
220
- /// and cancellation handlers will trigger only a single (first)
221
- /// time the task is cancelled.
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.
222
232
///
223
233
/// - SeeAlso: `Task.checkCancellation()`
234
+ /// - SeeAlso: `withTaskCancellationHandler(operation:onCancel:isolation:)`
224
235
public func cancel( ) {
225
236
Builtin . cancelAsyncTask ( _task)
226
237
}
0 commit comments