Skip to content

Commit fe54140

Browse files
committed
[Concurrency] TaskGroup.with... APIs using #isolation
move tests to existing actor_withCancellationHandler.swift file
1 parent b9d21d8 commit fe54140

File tree

6 files changed

+111
-40
lines changed

6 files changed

+111
-40
lines changed

stdlib/public/Concurrency/Clock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension Clock {
7777
}
7878

7979
@available(SwiftStdlib 5.7, *)
80-
@_unsafeInheritExecutor
80+
@_unsafeInheritExecutor // for ABI compatibility
8181
@usableFromInline
8282
internal func measure(
8383
_ work: () async throws -> Void

stdlib/public/Concurrency/DiscardingTaskGroup.swift

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,37 @@ import Swift
6868
///
6969
/// - SeeAlso: ``withThrowingDiscardingTaskGroup(returning:body:)
7070
@available(SwiftStdlib 5.9, *)
71+
@backDeployed(before: SwiftStdlib 6.0)
7172
@inlinable
72-
@_unsafeInheritExecutor
7373
public func withDiscardingTaskGroup<GroupResult>(
74+
returning returnType: GroupResult.Type = GroupResult.self,
75+
isolation: isolated (any Actor)? = #isolation,
76+
body: (inout DiscardingTaskGroup) async -> GroupResult
77+
) async -> GroupResult {
78+
#if compiler(>=5.5) && $BuiltinCreateTaskGroupWithFlags
79+
let flags = taskGroupCreateFlags(
80+
discardResults: true
81+
)
82+
83+
let _group = Builtin.createTaskGroupWithFlags(flags, GroupResult.self)
84+
var group = DiscardingTaskGroup(group: _group)
85+
defer { Builtin.destroyTaskGroup(_group) }
86+
87+
let result = await body(&group)
88+
89+
try! await group.awaitAllRemainingTasks() // try!-safe, cannot throw since this is a non throwing group
90+
91+
return result
92+
#else
93+
fatalError("Swift compiler is incompatible with this SDK version")
94+
#endif
95+
}
96+
97+
@available(SwiftStdlib 5.9, *)
98+
@usableFromInline
99+
@_unsafeInheritExecutor // for ABI compatibility
100+
@_silgen_name("$ss23withDiscardingTaskGroup9returning4bodyxxm_xs0bcD0VzYaXEtYalF")
101+
internal func __abi_withDiscardingTaskGroup<GroupResult>(
74102
returning returnType: GroupResult.Type = GroupResult.self,
75103
body: (inout DiscardingTaskGroup) async -> GroupResult
76104
) async -> GroupResult {
@@ -709,9 +737,46 @@ extension DiscardingTaskGroup: Sendable { }
709737
/// }
710738
/// ```
711739
@available(SwiftStdlib 5.9, *)
740+
@backDeployed(before: SwiftStdlib 6.0)
712741
@inlinable
713-
@_unsafeInheritExecutor
714742
public func withThrowingDiscardingTaskGroup<GroupResult>(
743+
returning returnType: GroupResult.Type = GroupResult.self,
744+
isolation: isolated (any Actor)? = #isolation,
745+
body: (inout ThrowingDiscardingTaskGroup<Error>) async throws -> GroupResult
746+
) async throws -> GroupResult {
747+
#if compiler(>=5.5) && $BuiltinCreateTaskGroupWithFlags
748+
let flags = taskGroupCreateFlags(
749+
discardResults: true
750+
)
751+
752+
let _group = Builtin.createTaskGroupWithFlags(flags, GroupResult.self)
753+
var group = ThrowingDiscardingTaskGroup<Error>(group: _group)
754+
defer { Builtin.destroyTaskGroup(_group) }
755+
756+
let result: GroupResult
757+
do {
758+
result = try await body(&group)
759+
} catch {
760+
group.cancelAll()
761+
762+
try await group.awaitAllRemainingTasks(bodyError: error)
763+
764+
throw error
765+
}
766+
767+
try await group.awaitAllRemainingTasks(bodyError: nil)
768+
769+
return result
770+
#else
771+
fatalError("Swift compiler is incompatible with this SDK version")
772+
#endif
773+
}
774+
775+
@available(SwiftStdlib 5.9, *)
776+
@usableFromInline
777+
@_unsafeInheritExecutor // for ABI compatibility
778+
@_silgen_name("$ss31withThrowingDiscardingTaskGroup9returning4bodyxxm_xs0bcdE0Vys5Error_pGzYaKXEtYaKlF")
779+
internal func __abi_withThrowingDiscardingTaskGroup<GroupResult>(
715780
returning returnType: GroupResult.Type = GroupResult.self,
716781
body: (inout ThrowingDiscardingTaskGroup<Error>) async throws -> GroupResult
717782
) async throws -> GroupResult {

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,36 @@ import Swift
6363
/// such as returning the work completed so far, returning an empty result, or returning `nil`.
6464
/// For tasks that need to handle cancellation by throwing an error,
6565
/// use the `withThrowingTaskGroup(of:returning:body:)` method instead.
66+
@available(SwiftStdlib 5.1, *)
67+
@backDeployed(before: SwiftStdlib 6.0, *)
68+
@inlinable
69+
public func withTaskGroup<ChildTaskResult, GroupResult>(
70+
of childTaskResultType: ChildTaskResult.Type,
71+
returning returnType: GroupResult.Type = GroupResult.self,
72+
isolation: isolated (any Actor)? = #isolation,
73+
body: (inout TaskGroup<ChildTaskResult>) async -> GroupResult
74+
) async -> GroupResult {
75+
#if compiler(>=5.5) && $BuiltinTaskGroupWithArgument
76+
77+
let _group = Builtin.createTaskGroup(ChildTaskResult.self)
78+
var group = TaskGroup<ChildTaskResult>(group: _group)
79+
80+
// Run the withTaskGroup body.
81+
let result = await body(&group)
82+
83+
await group.awaitAllRemainingTasks()
84+
85+
Builtin.destroyTaskGroup(_group)
86+
return result
87+
88+
#else
89+
fatalError("Swift compiler is incompatible with this SDK version")
90+
#endif
91+
}
92+
6693
@available(SwiftStdlib 5.1, *)
6794
@_silgen_name("$ss13withTaskGroup2of9returning4bodyq_xm_q_mq_ScGyxGzYaXEtYar0_lF")
68-
@_unsafeInheritExecutor
95+
@_unsafeInheritExecutor // for ABI compatibility
6996
@inlinable
7097
public func withTaskGroup<ChildTaskResult, GroupResult>(
7198
of childTaskResultType: ChildTaskResult.Type,
@@ -80,7 +107,6 @@ public func withTaskGroup<ChildTaskResult, GroupResult>(
80107
// Run the withTaskGroup body.
81108
let result = await body(&group)
82109

83-
// TODO(concurrency): should get isolation from param from withThrowingTaskGroup
84110
await group.awaitAllRemainingTasks()
85111

86112
Builtin.destroyTaskGroup(_group)
@@ -184,15 +210,13 @@ public func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
184210
// Run the withTaskGroup body.
185211
let result = try await body(&group)
186212

187-
// TODO(concurrency): should get isolation from param from withThrowingTaskGroup
188213
await group.awaitAllRemainingTasks()
189214
Builtin.destroyTaskGroup(_group)
190215

191216
return result
192217
} catch {
193218
group.cancelAll()
194219

195-
// TODO(concurrency): should get isolation from param from withThrowingTaskGroup
196220
await group.awaitAllRemainingTasks()
197221
Builtin.destroyTaskGroup(_group)
198222

test/Concurrency/actors_inherited_isolation_closures.swift

Lines changed: 0 additions & 32 deletions
This file was deleted.

test/abi/macOS/arm64/concurrency.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,17 @@ Added: _$sScf13checkIsolatedyyFTj
317317
Added: _$sScf13checkIsolatedyyFTq
318318

319319
// #isolated adoption in multiple APIs
320-
// Swift.TaskLocal.withValueImpl<A>(_: __owned A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
321320
// withTaskCancellationHandler gains #isolated
322321
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlF
323322
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlFTu
323+
// TaskGroup APIs gain #isolated
324+
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalF
325+
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalFTu
326+
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lF
327+
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lFTu
328+
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlF
329+
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlFTu
330+
// Swift.TaskLocal.withValueImpl<A>(_: __owned A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
324331
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlF
325332
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
326333
// Swift.TaskLocal.withValue<A>(_: A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1

test/abi/macOS/x86_64/concurrency.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ Added: _$sScf13checkIsolatedyyFTq
320320
// withTaskCancellationHandler gains #isolated
321321
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlF
322322
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlFTu
323+
// TaskGroup APIs gain #isolated
324+
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalF
325+
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalFTu
326+
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lF
327+
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lFTu
328+
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlF
329+
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlFTu
323330
// Swift.TaskLocal.withValueImpl<A>(_: __owned A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
324331
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlF
325332
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlFTu

0 commit comments

Comments
 (0)