Skip to content

Commit 5174f8f

Browse files
committed
implemented
1 parent 36ddf74 commit 5174f8f

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

stdlib/public/Concurrency/TaskGroup.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,6 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
414414

415415
/// Returns *assumed* new status, including the just performed +1.
416416
GroupStatus statusMarkWaitingAssumeAcquire() {
417-
// fprintf(stderr, "[%s:%d](%s) statusMarkWaitingAssumeAcquire, load....\n", __FILE_NAME__, __LINE__, __FUNCTION__);
418-
// fprintf(stderr, "[%s:%d](%s) statusMarkWaitingAssumeAcquire = %s\n", __FILE_NAME__, __LINE__, __FUNCTION__,
419-
// statusLoadRelaxed().to_string(this).c_str());
420417
auto old = status.fetch_or(GroupStatus::waiting, std::memory_order_acquire);
421418
return GroupStatus{old | GroupStatus::waiting};
422419
}

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
670670
}
671671

672672
/// Wait for all of the group's remaining tasks to complete.
673+
///
674+
/// - Throws: only during
673675
@_alwaysEmitIntoClient
674676
public mutating func waitForAll() async throws {
675677
try await self.awaitAllRemainingTasks()

test/Concurrency/Runtime/async_taskgroup_throw_rethrow.swift

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,120 @@
88
// REQUIRES: concurrency_runtime
99
// UNSUPPORTED: back_deployment_runtime
1010

11-
struct Boom: Error {}
12-
struct IgnoredBoom: Error {}
11+
struct Boom: Error {
12+
let id: String
13+
14+
init(file: String = #fileID, line: UInt = #line) {
15+
self.id = "\(file):\(line)"
16+
}
17+
init(id: String) {
18+
self.id = id
19+
}
20+
}
1321

22+
struct IgnoredBoom: Error {}
1423
func echo(_ i: Int) async -> Int { i }
15-
func boom() async throws -> Int { throw Boom() }
24+
25+
func test_taskGroup_throws_rethrows() async {
26+
print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_throws_rethrows
27+
do {
28+
let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self) { group in
29+
group.addTask { await echo(1) }
30+
group.addTask { await echo(2) }
31+
group.addTask { throw Boom() }
32+
33+
do {
34+
while let r = try await group.next() {
35+
print("next: \(r)")
36+
}
37+
} catch {
38+
// CHECK: error caught and rethrown in group: Boom(
39+
print("error caught and rethrown in group: \(error)")
40+
throw error
41+
}
42+
43+
print("should have thrown")
44+
return 0
45+
}
46+
47+
print("Expected error to be thrown, but got: \(got)")
48+
} catch {
49+
// CHECK: rethrown: Boom(
50+
print("rethrown: \(error)")
51+
}
52+
}
53+
54+
func test_taskGroup_noThrow_ifNotAwaitedThrowingTask() async {
55+
print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_noThrow_ifNotAwaitedThrowingTask
56+
let got = await withThrowingTaskGroup(of: Int.self, returning: Int.self) { group in
57+
group.addTask { await echo(1) }
58+
guard let r = try! await group.next() else {
59+
return 0
60+
}
61+
62+
group.addTask { throw Boom() }
63+
// don't consume this task, so we're not throwing here
64+
65+
return r
66+
}
67+
68+
print("Expected no error to be thrown, got: \(got)") // CHECK: Expected no error to be thrown, got: 1
69+
}
1670

1771
func test_taskGroup_discardResults_automaticallyRethrows() async {
18-
print("==== \(#function) ------")
72+
print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_discardResults_automaticallyRethrows
1973
do {
2074
let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self,
2175
discardResults: true) { group in
2276
group.addTask { await echo(1) }
23-
group.addTask { try await boom() }
77+
group.addTask { throw Boom() }
2478
// add a throwing task, but don't consume it explicitly
2579
// since we're in discard results mode, all will be awaited and the first error it thrown
26-
2780
return 13
2881
}
2982

3083
print("Expected error to be thrown, but got: \(got)")
3184
} catch {
32-
// CHECK: rethrown: Boom()
85+
// CHECK: rethrown: Boom(
3386
print("rethrown: \(error)")
3487
}
3588
}
3689

90+
func test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst() async {
91+
print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst
92+
do {
93+
let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self,
94+
discardResults: true) { group in
95+
group.addTask { await echo(1) }
96+
group.addTask { throw Boom(id: "first") }
97+
// add a throwing task, but don't consume it explicitly
98+
// since we're in discard results mode, all will be awaited and the first error it thrown
99+
100+
do {
101+
try await group.waitForAll()
102+
} catch {
103+
// CHECK: caught: Boom(id: "first")
104+
print("caught: \(error)")
105+
}
106+
107+
group.addTask { throw Boom(id: "second") }
108+
109+
return 4
110+
}
111+
112+
print("Expected error to be thrown, but got: \(got)")
113+
} catch {
114+
// CHECK: rethrown: Boom(id: "second")
115+
print("rethrown: \(error)")
116+
}
117+
}
37118

38119
@available(SwiftStdlib 5.1, *)
39120
@main struct Main {
40121
static func main() async {
41-
// await test_taskGroup_throws_rethrows()
42-
// await test_taskGroup_noThrow_ifNotAwaitedThrowingTask()
122+
await test_taskGroup_throws_rethrows()
123+
await test_taskGroup_noThrow_ifNotAwaitedThrowingTask()
43124
await test_taskGroup_discardResults_automaticallyRethrows()
125+
await test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst()
44126
}
45127
}

0 commit comments

Comments
 (0)