Skip to content

Commit 04dd840

Browse files
committed
[SE-0304] Replace the async operations in (Throwing)TaskGroup with addTask
1 parent 102c001 commit 04dd840

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

stdlib/public/Concurrency/SourceCompatibilityShims.swift

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,65 +190,101 @@ extension Task where Failure == Never {
190190

191191
@available(SwiftStdlib 5.5, *)
192192
extension TaskGroup {
193-
@available(*, deprecated, renamed: "async(priority:operation:)")
193+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
194194
@_alwaysEmitIntoClient
195195
public mutating func add(
196196
priority: TaskPriority? = nil,
197197
operation: __owned @Sendable @escaping () async -> ChildTaskResult
198198
) async -> Bool {
199-
return self.asyncUnlessCancelled(priority: priority) {
199+
return self.addTaskUnlessCancelled(priority: priority) {
200200
await operation()
201201
}
202202
}
203203

204-
@available(*, deprecated, renamed: "async(priority:operation:)")
204+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
205205
@_alwaysEmitIntoClient
206206
public mutating func spawn(
207207
priority: TaskPriority? = nil,
208208
operation: __owned @Sendable @escaping () async -> ChildTaskResult
209209
) {
210-
async(priority: priority, operation: operation)
210+
addTask(priority: priority, operation: operation)
211211
}
212212

213-
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
213+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(priority:operation:)")
214214
@_alwaysEmitIntoClient
215215
public mutating func spawnUnlessCancelled(
216216
priority: TaskPriority? = nil,
217217
operation: __owned @Sendable @escaping () async -> ChildTaskResult
218218
) -> Bool {
219-
asyncUnlessCancelled(priority: priority, operation: operation)
219+
addTaskUnlessCancelled(priority: priority, operation: operation)
220+
}
221+
222+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
223+
@_alwaysEmitIntoClient
224+
public mutating func async(
225+
priority: TaskPriority? = nil,
226+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
227+
) {
228+
addTask(priority: priority, operation: operation)
229+
}
230+
231+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(priority:operation:)")
232+
@_alwaysEmitIntoClient
233+
public mutating func asyncUnlessCancelled(
234+
priority: TaskPriority? = nil,
235+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
236+
) -> Bool {
237+
addTaskUnlessCancelled(priority: priority, operation: operation)
220238
}
221239
}
222240

223241
@available(SwiftStdlib 5.5, *)
224242
extension ThrowingTaskGroup {
225-
@available(*, deprecated, renamed: "async(priority:operation:)")
243+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
226244
@_alwaysEmitIntoClient
227245
public mutating func add(
228246
priority: TaskPriority? = nil,
229247
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
230248
) async -> Bool {
231-
return self.asyncUnlessCancelled(priority: priority) {
249+
return self.addTaskUnlessCancelled(priority: priority) {
232250
try await operation()
233251
}
234252
}
235253

236-
@available(*, deprecated, renamed: "async(priority:operation:)")
254+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
237255
@_alwaysEmitIntoClient
238256
public mutating func spawn(
239257
priority: TaskPriority? = nil,
240258
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
241259
) {
242-
async(priority: priority, operation: operation)
260+
addTask(priority: priority, operation: operation)
243261
}
244262

245-
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
263+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(priority:operation:)")
246264
@_alwaysEmitIntoClient
247265
public mutating func spawnUnlessCancelled(
248266
priority: TaskPriority? = nil,
249267
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
250268
) -> Bool {
251-
asyncUnlessCancelled(priority: priority, operation: operation)
269+
addTaskUnlessCancelled(priority: priority, operation: operation)
270+
}
271+
272+
@available(*, deprecated, renamed: "addTask(priority:operation:)")
273+
@_alwaysEmitIntoClient
274+
public mutating func async(
275+
priority: TaskPriority? = nil,
276+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
277+
) {
278+
addTask(priority: priority, operation: operation)
279+
}
280+
281+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(priority:operation:)")
282+
@_alwaysEmitIntoClient
283+
public mutating func asyncUnlessCancelled(
284+
priority: TaskPriority? = nil,
285+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
286+
) -> Bool {
287+
addTaskUnlessCancelled(priority: priority, operation: operation)
252288
}
253289
}
254290

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public struct TaskGroup<ChildTaskResult> {
208208
/// - `true` if the operation was added to the group successfully,
209209
/// `false` otherwise (e.g. because the group `isCancelled`)
210210
@_alwaysEmitIntoClient
211-
public mutating func async(
211+
public mutating func addTask(
212212
priority: TaskPriority? = nil,
213213
operation: __owned @Sendable @escaping () async -> ChildTaskResult
214214
) {
@@ -242,7 +242,7 @@ public struct TaskGroup<ChildTaskResult> {
242242
/// - `true` if the operation was added to the group successfully,
243243
/// `false` otherwise (e.g. because the group `isCancelled`)
244244
@_alwaysEmitIntoClient
245-
public mutating func asyncUnlessCancelled(
245+
public mutating func addTaskUnlessCancelled(
246246
priority: TaskPriority? = nil,
247247
operation: __owned @Sendable @escaping () async -> ChildTaskResult
248248
) -> Bool {
@@ -440,7 +440,7 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
440440
/// - `true` if the operation was added to the group successfully,
441441
/// `false` otherwise (e.g. because the group `isCancelled`)
442442
@_alwaysEmitIntoClient
443-
public mutating func async(
443+
public mutating func addTask(
444444
priority: TaskPriority? = nil,
445445
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
446446
) {
@@ -474,7 +474,7 @@ public struct ThrowingTaskGroup<ChildTaskResult, Failure: Error> {
474474
/// - `true` if the operation was added to the group successfully,
475475
/// `false` otherwise (e.g. because the group `isCancelled`)
476476
@_alwaysEmitIntoClient
477-
public mutating func asyncUnlessCancelled(
477+
public mutating func addTaskUnlessCancelled(
478478
priority: TaskPriority? = nil,
479479
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
480480
) -> Bool {

test/Concurrency/async_task_groups.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func asyncThrowsOnCancel() async throws -> Int {
2222
@available(SwiftStdlib 5.5, *)
2323
func test_taskGroup_add() async throws -> Int {
2424
try await withThrowingTaskGroup(of: Int.self) { group in
25-
group.async {
25+
group.addTask {
2626
await asyncFunc()
2727
}
2828

29-
group.async {
29+
group.addTask {
3030
await asyncFunc()
3131
}
3232

@@ -51,9 +51,9 @@ func boom() async throws -> Int { throw Boom() }
5151
func first_allMustSucceed() async throws {
5252

5353
let first: Int = try await withThrowingTaskGroup(of: Int.self) { group in
54-
group.async { await work() }
55-
group.async { await work() }
56-
group.async { try await boom() }
54+
group.addTask { await work() }
55+
group.addTask { await work() }
56+
group.addTask { try await boom() }
5757

5858
if let first = try await group.next() {
5959
return first
@@ -72,9 +72,9 @@ func first_ignoreFailures() async throws {
7272
@Sendable func boom() async throws -> Int { throw Boom() }
7373

7474
let first: Int = try await withThrowingTaskGroup(of: Int.self) { group in
75-
group.async { await work() }
76-
group.async { await work() }
77-
group.async {
75+
group.addTask { await work() }
76+
group.addTask { await work() }
77+
group.addTask {
7878
do {
7979
return try await boom()
8080
} catch {
@@ -121,7 +121,7 @@ func test_taskGroup_quorum_thenCancel() async {
121121
func gatherQuorum(followers: [Follower]) async -> Bool {
122122
try! await withThrowingTaskGroup(of: Vote.self) { group in
123123
for follower in followers {
124-
group.async { try await follower.vote() }
124+
group.addTask { try await follower.vote() }
125125
}
126126

127127
defer {
@@ -192,7 +192,7 @@ extension Collection where Self: Sendable, Element: Sendable, Self.Index: Sendab
192192
var submitted = 0
193193

194194
func submitNext() async throws {
195-
group.async { [submitted,i] in
195+
group.addTask { [submitted,i] in
196196
let value = try await transform(self[i])
197197
return SendableTuple2(submitted, value)
198198
}

0 commit comments

Comments
 (0)