Skip to content

Commit 29b9251

Browse files
committed
task group documentation fixes; cancellation and priority param name
1 parent dc22978 commit 29b9251

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

stdlib/public/Concurrency/DiscardingTaskGroup.swift

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public func withDiscardingTaskGroup<GroupResult>(
122122
/// - when an error is thrown out of the `withThrowingDiscardingTaskGroup(...) { }` closure,
123123
/// - when the ``Task`` running this task group is cancelled.
124124
///
125-
/// Since a `TaskGroup` is a structured concurrency primitive, cancellation is
125+
/// Since a `DiscardingTaskGroup` is a structured concurrency primitive, cancellation is
126126
/// automatically propagated through all of its child-tasks (and their child
127127
/// tasks).
128128
///
@@ -161,6 +161,13 @@ public struct DiscardingTaskGroup {
161161
let _: Void? = try await _taskGroupWaitAll(group: _group, bodyError: nil)
162162
}
163163

164+
/// Adds a child task to the group.
165+
///
166+
/// - Parameters:
167+
/// - priority: The priority of the operation task.
168+
/// Omit this parameter or pass `.unspecified`
169+
/// to set the child task's priority to the priority of the group.
170+
/// - operation: The operation to execute as part of the task group.
164171
@_alwaysEmitIntoClient
165172
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
166173
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
@@ -187,6 +194,15 @@ public struct DiscardingTaskGroup {
187194
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
188195
}
189196

197+
/// Adds a child task to the group, unless the group has been canceled.
198+
///
199+
/// - Parameters:
200+
/// - priority: The priority of the operation task.
201+
/// Omit this parameter or pass `.unspecified`
202+
/// to set the child task's priority to the priority of the group.
203+
/// - operation: The operation to execute as part of the task group.
204+
/// - Returns: `true` if the child task was added to the group;
205+
/// otherwise `false`.
190206
@_alwaysEmitIntoClient
191207
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
192208
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
@@ -235,6 +251,12 @@ public struct DiscardingTaskGroup {
235251
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
236252
}
237253

254+
/// Adds a child task to the group, unless the group has been canceled.
255+
///
256+
/// - Parameters:
257+
/// - operation: The operation to execute as part of the task group.
258+
/// - Returns: `true` if the child task was added to the group;
259+
/// otherwise `false`.
238260
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
239261
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
240262
#endif
@@ -265,14 +287,46 @@ public struct DiscardingTaskGroup {
265287
#endif
266288
}
267289

290+
/// A Boolean value that indicates whether the group has any remaining tasks.
291+
///
292+
/// At the start of the body of a `withDiscardingTaskGroup(of:returning:body:)` call,
293+
/// the task group is always empty.
294+
///
295+
/// It's guaranteed to be empty when returning from that body
296+
/// because a task group waits for all child tasks to complete before returning.
297+
///
298+
/// - Returns: `true` if the group has no pending tasks; otherwise `false`.
268299
public var isEmpty: Bool {
269300
_taskGroupIsEmpty(_group)
270301
}
271302

303+
/// Cancel all of the remaining tasks in the group.
304+
///
305+
/// If you add a task to a group after canceling the group,
306+
/// that task is canceled immediately after being added to the group.
307+
///
308+
/// Immediately cancelled child tasks should therefore cooperatively check for and
309+
/// react to cancellation, e.g. by throwing an `CancellationError` at their
310+
/// earliest convenience, or otherwise handling the cancellation.
311+
///
312+
/// There are no restrictions on where you can call this method.
313+
/// Code inside a child task or even another task can cancel a group,
314+
/// however one should be very careful to not keep a reference to the
315+
/// group longer than the `with...TaskGroup(...) { ... }` method body is executing.
316+
///
317+
/// - SeeAlso: `Task.isCancelled`
318+
/// - SeeAlso: `DiscardingTaskGroup.isCancelled`
272319
public func cancelAll() {
273320
_taskGroupCancelAll(group: _group)
274321
}
275322

323+
/// A Boolean value that indicates whether the group was canceled.
324+
///
325+
/// To cancel a group, call the `DiscardingTaskGroup.cancelAll()` method.
326+
///
327+
/// If the task that's currently running this group is canceled,
328+
/// the group is also implicitly canceled,
329+
/// which is also reflected in this property's value.
276330
public var isCancelled: Bool {
277331
return _taskGroupIsCancelled(group: _group)
278332
}
@@ -575,7 +629,7 @@ public struct ThrowingDiscardingTaskGroup<Failure: Error> {
575629
/// group longer than the `with...TaskGroup(...) { ... }` method body is executing.
576630
///
577631
/// - SeeAlso: `Task.isCancelled`
578-
/// - SeeAlso: `ThrowingTaskGroup.isCancelled`
632+
/// - SeeAlso: `ThrowingDiscardingTaskGroup.isCancelled`
579633
public func cancelAll() {
580634
_taskGroupCancelAll(group: _group)
581635
}

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
259259
/// Adds a child task to the group.
260260
///
261261
/// - Parameters:
262-
/// - overridingPriority: The priority of the operation task.
262+
/// - priority: The priority of the operation task.
263263
/// Omit this parameter or pass `.unspecified`
264264
/// to set the child task's priority to the priority of the group.
265265
/// - operation: The operation to execute as part of the task group.
@@ -333,7 +333,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
333333
fatalError("Unsupported Swift compiler")
334334
#endif
335335
}
336-
#else
336+
#else // if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
337337
@available(SwiftStdlib 5.7, *)
338338
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
339339
public mutating func addTask(

0 commit comments

Comments
 (0)