Skip to content

Commit 0160fc3

Browse files
committed
[Concurrency] adopt #isolation in last remaining APIs
1 parent 58c5a76 commit 0160fc3

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public func withTaskExecutorPreference<T, Failure>(
161161

162162
@_unavailableInEmbedded
163163
@available(SwiftStdlib 6.0, *)
164-
@_unsafeInheritExecutor // calling withTaskExecutor MUST NOT perform the "usual" hop to global
164+
@_unsafeInheritExecutor // for ABI compatibility
165165
@_silgen_name("$ss26withTaskExecutorPreference_9operationxSch_pSg_xyYaYbKXEtYaKs8SendableRzlF")
166166
public func __abi__withTaskExecutorPreference<T: Sendable>(
167167
_ taskExecutor: (any TaskExecutor)?,

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,46 @@ public func withTaskGroup<ChildTaskResult, GroupResult>(
193193
/// which gives you a chance to handle the individual error
194194
/// or to let the group rethrow the error.
195195
@available(SwiftStdlib 5.1, *)
196-
@_silgen_name("$ss21withThrowingTaskGroup2of9returning4bodyq_xm_q_mq_Scgyxs5Error_pGzYaKXEtYaKr0_lF")
197-
@_unsafeInheritExecutor
196+
@backDeployed(before: SwiftStdlib 6.0)
198197
@inlinable
199198
public func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
199+
of childTaskResultType: ChildTaskResult.Type,
200+
returning returnType: GroupResult.Type = GroupResult.self,
201+
isolation: isolated (any Actor)? = #isolation,
202+
body: (inout ThrowingTaskGroup<ChildTaskResult, Error>) async throws -> GroupResult
203+
) async rethrows -> GroupResult {
204+
#if compiler(>=5.5) && $BuiltinTaskGroupWithArgument
205+
206+
let _group = Builtin.createTaskGroup(ChildTaskResult.self)
207+
var group = ThrowingTaskGroup<ChildTaskResult, Error>(group: _group)
208+
209+
do {
210+
// Run the withTaskGroup body.
211+
let result = try await body(&group)
212+
213+
await group.awaitAllRemainingTasks()
214+
Builtin.destroyTaskGroup(_group)
215+
216+
return result
217+
} catch {
218+
group.cancelAll()
219+
220+
await group.awaitAllRemainingTasks()
221+
Builtin.destroyTaskGroup(_group)
222+
223+
throw error
224+
}
225+
226+
#else
227+
fatalError("Swift compiler is incompatible with this SDK version")
228+
#endif
229+
}
230+
231+
@available(SwiftStdlib 5.1, *)
232+
@_silgen_name("$ss21withThrowingTaskGroup2of9returning4bodyq_xm_q_mq_Scgyxs5Error_pGzYaKXEtYaKr0_lF")
233+
@_unsafeInheritExecutor // for ABI compatibility
234+
@usableFromInline
235+
internal func withThrowingTaskGroup<ChildTaskResult, GroupResult>(
200236
of childTaskResultType: ChildTaskResult.Type,
201237
returning returnType: GroupResult.Type = GroupResult.self,
202238
body: (inout ThrowingTaskGroup<ChildTaskResult, Error>) async throws -> GroupResult

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
260260

261261
@inlinable
262262
@discardableResult
263-
@_unsafeInheritExecutor
263+
@_unsafeInheritExecutor // internal for backwards compatibility; though may be able to be removed safely?
264264
@available(SwiftStdlib 5.1, *)
265265
@backDeployed(before: SwiftStdlib 5.9)
266266
internal func withValueImpl<R>(_ valueDuringOperation: __owned Value,

test/Concurrency/actor_withCancellationHandler.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// REQUIRES: concurrency
88
// REQUIRES: asserts
99

10-
actor foo {
10+
actor Foo {
1111
var t: Task<Void, Error>?
1212

1313
func access() {}
@@ -22,3 +22,25 @@ actor foo {
2222
}
2323
}
2424
}
25+
26+
actor Container {
27+
var num: Int = 0 // expected-note{{mutation of this property is only permitted within the actor}}
28+
func test() async {
29+
30+
// no warnings:
31+
await withTaskCancellationHandler {
32+
num += 1
33+
} onCancel: {
34+
// nothing
35+
}
36+
}
37+
38+
func errors() async {
39+
await withTaskCancellationHandler {
40+
num += 1 // no error, this runs synchronously on caller context
41+
} onCancel: {
42+
// this should error because cancellation is invoked concurrently
43+
num += 10 // expected-error{{actor-isolated property 'num' can not be mutated from a Sendable closure}}
44+
}
45+
}
46+
}

test/abi/macOS/arm64/concurrency.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,15 @@ Added: _$sScf13checkIsolatedyyFTq
320320
// withTaskCancellationHandler gains #isolated
321321
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlF
322322
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlFTu
323-
// TaskGroup APIs gain #isolated
323+
// TaskGroup.with... APIs gain #isolated
324324
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalF
325325
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalFTu
326326
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lF
327327
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lFTu
328328
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlF
329329
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlFTu
330+
Added: _$ss21withThrowingTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_Scgyxs5Error_pGzYaKXEtYaKs8SendableRzr0_lF
331+
Added: _$ss21withThrowingTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_Scgyxs5Error_pGzYaKXEtYaKs8SendableRzr0_lFTu
330332
// Swift.TaskLocal.withValueImpl<A>(_: __owned A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
331333
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlF
332334
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlFTu

test/abi/macOS/x86_64/concurrency.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,15 @@ Added: _$sScf13checkIsolatedyyFTq
320320
// withTaskCancellationHandler gains #isolated
321321
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlF
322322
Added: _$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlFTu
323-
// TaskGroup APIs gain #isolated
323+
// TaskGroup.with... APIs gain #isolated
324324
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalF
325325
Added: _$ss23withDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcD0VzYaXEtYalFTu
326326
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lF
327327
Added: _$ss13withTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_ScGyxGzYaXEtYas8SendableRzr0_lFTu
328328
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlF
329329
Added: _$ss31withThrowingDiscardingTaskGroup9returning9isolation4bodyxxm_ScA_pSgYixs0bcdE0Vys5Error_pGzYaKXEtYaKlFTu
330+
Added: _$ss21withThrowingTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_Scgyxs5Error_pGzYaKXEtYaKs8SendableRzr0_lF
331+
Added: _$ss21withThrowingTaskGroup2of9returning9isolation4bodyq_xm_q_mScA_pSgYiq_Scgyxs5Error_pGzYaKXEtYaKs8SendableRzr0_lFTu
330332
// Swift.TaskLocal.withValueImpl<A>(_: __owned A, operation: () async throws -> A1, isolation: isolated Swift.Actor?, file: Swift.String, line: Swift.UInt) async throws -> A1
331333
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlF
332334
Added: _$ss9TaskLocalC13withValueImpl_9operation9isolation4file4lineqd__xn_qd__yYaKXEScA_pSgYiSSSutYaKlFTu

test/api-digester/stability-concurrency-abi.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ Func withCheckedThrowingContinuation(function:_:) has mangled name changing from
8585
Func withCheckedThrowingContinuation(function:_:) has parameter 0 type change from Swift.String to (any _Concurrency.Actor)?
8686
Func withCheckedThrowingContinuation(function:_:) has parameter 1 type change from (_Concurrency.CheckedContinuation<τ_0_0, any Swift.Error>) -> () to Swift.String
8787

88+
// #isolation adoption for cancellation handlers; old APIs are kept ABI compatible
89+
Func withTaskCancellationHandler(operation:onCancel:) has been renamed to Func withTaskCancellationHandler(operation:onCancel:isolation:)
90+
Func withTaskCancellationHandler(operation:onCancel:) has mangled name changing from '_Concurrency.withTaskCancellationHandler<A>(operation: () async throws -> A, onCancel: @Sendable () -> ()) async throws -> A' to '_Concurrency.withTaskCancellationHandler<A>(operation: () async throws -> A, onCancel: @Sendable () -> (), isolation: isolated Swift.Optional<Swift.Actor>) async throws -> A'
91+
8892
// #isolated was adopted and the old methods kept: $ss31withCheckedThrowingContinuation8function_xSS_yScCyxs5Error_pGXEtYaKlF
8993
Func withCheckedContinuation(function:_:) has been renamed to Func withCheckedContinuation(isolation:function:_:)
9094
Func withCheckedContinuation(function:_:) has mangled name changing from '_Concurrency.withCheckedContinuation<A>(function: Swift.String, _: (Swift.CheckedContinuation<A, Swift.Never>) -> ()) async -> A' to '_Concurrency.withCheckedContinuation<A>(isolation: isolated Swift.Optional<Swift.Actor>, function: Swift.String, _: (Swift.CheckedContinuation<A, Swift.Never>) -> ()) async -> A'

0 commit comments

Comments
 (0)