Skip to content

Commit d8b4e3c

Browse files
committed
[Concurrency] add deprecated shim for Task.withGroup
1 parent 39e97c5 commit d8b4e3c

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ extension Task {
368368
@available(*, deprecated, message: "`Task.runDetached` was replaced by `detach` and will be removed shortly.")
369369
public func runDetached<T>(
370370
priority: Task.Priority = .unspecified,
371-
operation: __owned @Sendable @escaping () async -> T
372-
) -> Task.Handle<T, Never> {
371+
operation: __owned @Sendable @escaping () async throws -> T
372+
) -> Task.Handle<T, Error> {
373373
detach(priority: priority, operation: operation)
374374
}
375375

@@ -519,6 +519,22 @@ extension Task {
519519

520520
// ==== UnsafeCurrentTask ------------------------------------------------------
521521

522+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
523+
extension Task {
524+
525+
@available(*, deprecated, message: "`Task.unsafeCurrent` was replaced by `withUnsafeCurrentTask { task in ... }`, and will be removed soon.")
526+
public static var unsafeCurrent: UnsafeCurrentTask? {
527+
guard let _task = _getCurrentAsyncTask() else {
528+
return nil
529+
}
530+
// FIXME: This retain seems pretty wrong, however if we don't we WILL crash
531+
// with "destroying a task that never completed" in the task's destroy.
532+
// How do we solve this properly?
533+
_swiftRetain(_task)
534+
return UnsafeCurrentTask(_task)
535+
}
536+
}
537+
522538
/// Calls the given closure with the with the "current" task in which this
523539
/// function was invoked.
524540
///

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ import Swift
1515

1616
// ==== TaskGroup --------------------------------------------------------------
1717

18+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
19+
extension Task {
20+
@available(*, deprecated, message: "`Task.Group` was replaced by `ThrowingTaskGroup` and `TaskGroup` and will be removed shortly.")
21+
public typealias Group<TaskResult: Sendable> = ThrowingTaskGroup<TaskResult, Error>
22+
23+
@available(*, deprecated, message: "`Task.withGroup` was replaced by `withThrowingTaskGroup` and `withTaskGroup` and will be removed shortly.")
24+
public static func withGroup<TaskResult, BodyResult>(
25+
resultType: TaskResult.Type,
26+
returning returnType: BodyResult.Type = BodyResult.self,
27+
body: (inout Task.Group<TaskResult>) async throws -> BodyResult
28+
) async rethrows -> BodyResult {
29+
try await withThrowingTaskGroup(of: resultType) { group in
30+
try await body(&group)
31+
}
32+
}
33+
}
34+
35+
1836
/// Starts a new task group which provides a scope in which a dynamic number of
1937
/// tasks may be spawned.
2038
///
@@ -206,8 +224,19 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
206224
self._task = task
207225
self._group = group
208226
}
209-
210-
/// Add a child task to the group.
227+
228+
@available(*, deprecated, message: "`Task.Group.add` has been replaced by `TaskGroup.spawn` or `TaskGroup.spawnUnlessCancelled` and will be removed shortly.")
229+
public mutating func add(
230+
priority: Task.Priority = .unspecified,
231+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
232+
) async -> Bool {
233+
return try await self.spawnUnlessCancelled(priority: priority) {
234+
try! await operation()
235+
}
236+
}
237+
238+
239+
/// Add a child task to the group.
211240
///
212241
/// ### Error handling
213242
/// Operations are allowed to `throw`, in which case the `try await next()`
@@ -446,6 +475,16 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
446475
self._group = group
447476
}
448477

478+
@available(*, deprecated, message: "`Task.Group.add` has been replaced by `(Throwing)TaskGroup.spawn` or `(Throwing)TaskGroup.spawnUnlessCancelled` and will be removed shortly.")
479+
public mutating func add(
480+
priority: Task.Priority = .unspecified,
481+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
482+
) async -> Bool {
483+
return try await self.spawnUnlessCancelled(priority: priority) {
484+
try await operation()
485+
}
486+
}
487+
449488
/// Spawn, unconditionally, a child task in the group.
450489
///
451490
/// ### Error handling

0 commit comments

Comments
 (0)