Skip to content

Commit b354a3e

Browse files
committed
[Concurrency] lower availability reqs of addTask apis with names
1 parent e94adde commit b354a3e

File tree

5 files changed

+137
-38
lines changed

5 files changed

+137
-38
lines changed

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,6 @@ extension Task where Failure == Never {
232232
priority: TaskPriority? = nil,
233233
operation: sending @escaping () async -> Success
234234
) {
235-
guard let taskExecutor else {
236-
self = Self.init(name: name, priority: priority, operation: operation)
237-
return
238-
}
239-
240235
// Set up the job flags for a new task.
241236
let flags = taskCreateFlags(
242237
priority: priority,
@@ -269,15 +264,21 @@ extension Task where Failure == Never {
269264
initialTaskExecutorConsuming: taskExecutor,
270265
operation: operation).0
271266
}
272-
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
273-
274-
if task == nil {
267+
#else // $BuiltinCreateAsyncTaskOwnedTaskExecutor
268+
if task == nil, let taskExecutor {
275269
let executorBuiltin: Builtin.Executor =
276270
taskExecutor.asUnownedTaskExecutor().executor
277271
task = Builtin.createAsyncTaskWithExecutor(
278272
flags, executorBuiltin, operation).0
279273
}
274+
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
275+
276+
if task == nil {
277+
assert(taskExecutor == nil)
278+
task = Builtin.createAsyncTask(flags, operation).0
279+
}
280280

281+
assert(task != nil, "Task must have been initialized by now")
281282
self._task = task!
282283
}
283284
}
@@ -320,11 +321,6 @@ extension Task where Failure == Error {
320321
priority: TaskPriority? = nil,
321322
operation: sending @escaping () async throws -> Success
322323
) {
323-
guard let taskExecutor else {
324-
self = Self.init(name: name, priority: priority, operation: operation)
325-
return
326-
}
327-
328324
// Set up the job flags for a new task.
329325
let flags = taskCreateFlags(
330326
priority: priority,
@@ -357,15 +353,21 @@ extension Task where Failure == Error {
357353
initialTaskExecutorConsuming: taskExecutor,
358354
operation: operation).0
359355
}
360-
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
361-
362-
if task == nil {
356+
#else // $BuiltinCreateAsyncTaskOwnedTaskExecutor
357+
if task == nil, let taskExecutor {
363358
let executorBuiltin: Builtin.Executor =
364359
taskExecutor.asUnownedTaskExecutor().executor
365360
task = Builtin.createAsyncTaskWithExecutor(
366361
flags, executorBuiltin, operation).0
367362
}
363+
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
364+
365+
if task == nil {
366+
assert(taskExecutor == nil)
367+
task = Builtin.createAsyncTask(flags, operation).0
368+
}
368369

370+
assert(task != nil, "Task must have been initialized by now")
369371
self._task = task!
370372
}
371373
}
@@ -407,10 +409,6 @@ extension Task where Failure == Never {
407409
priority: TaskPriority? = nil,
408410
operation: sending @escaping () async -> Success
409411
) -> Task<Success, Failure> {
410-
guard let taskExecutor else {
411-
return Self.detached(name: name, priority: priority, operation: operation)
412-
}
413-
414412
// Set up the job flags for a new task.
415413
let flags = taskCreateFlags(
416414
priority: priority,
@@ -443,15 +441,21 @@ extension Task where Failure == Never {
443441
initialTaskExecutorConsuming: taskExecutor,
444442
operation: operation).0
445443
}
446-
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
447-
448-
if task == nil {
444+
#else // $BuiltinCreateAsyncTaskOwnedTaskExecutor
445+
if task == nil, let taskExecutor {
449446
let executorBuiltin: Builtin.Executor =
450447
taskExecutor.asUnownedTaskExecutor().executor
451448
task = Builtin.createAsyncTaskWithExecutor(
452449
flags, executorBuiltin, operation).0
453450
}
451+
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
452+
453+
if task == nil {
454+
assert(taskExecutor == nil)
455+
task = Builtin.createAsyncTask(flags, operation).0
456+
}
454457

458+
assert(task != nil, "Task must have been initialized by now")
455459
return Task(task!)
456460
}
457461
}
@@ -525,21 +529,18 @@ extension Task where Failure == Error {
525529
initialTaskExecutorConsuming: taskExecutor,
526530
operation: operation).0
527531
}
532+
#else // $BuiltinCreateAsyncTaskOwnedTaskExecutor
533+
if task == nil, let taskExecutor {
534+
let executorBuiltin: Builtin.Executor =
535+
taskExecutor.asUnownedTaskExecutor().executor
536+
task = Builtin.createAsyncTaskWithExecutor(
537+
flags, executorBuiltin, operation).0
538+
}
528539
#endif // $BuiltinCreateAsyncTaskOwnedTaskExecutor
529540

530541
if task == nil {
531-
if let taskExecutor {
532-
let executorBuiltin: Builtin.Executor =
533-
taskExecutor.asUnownedTaskExecutor().executor
534-
task = Builtin.createAsyncTaskWithExecutor(
535-
flags, executorBuiltin, operation).0
536-
} else {
537-
assert(taskExecutor == nil)
538-
task = Builtin.createTask(
539-
flags: flags,
540-
// no initialSerialExecutor since not isolated(any)
541-
operation: operation).0
542-
}
542+
assert(taskExecutor == nil)
543+
task = Builtin.createAsyncTask(flags, operation).0
543544
}
544545

545546
return Task(task!)

stdlib/public/Concurrency/TaskGroup+addTask.swift.gyb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Swift
3131
% 'ThrowingDiscardingTaskGroup'
3232
% ],
3333
% [
34-
% '@available(SwiftStdlib 6.2, *)',
34+
% '@available(SwiftStdlib 6.0, *)', # because task executor
3535
% '@_unavailableInEmbedded', # since TaskExecutor is not available on embedded
3636
% ],
3737
% ['addTask', 'addTaskUnlessCancelled'],
@@ -43,6 +43,26 @@ import Swift
4343
% 'operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult'
4444
% ],
4545
% ),
46+
% (
47+
% '', # no #if condition
48+
% [
49+
% 'TaskGroup',
50+
% 'ThrowingTaskGroup',
51+
% 'DiscardingTaskGroup',
52+
% 'ThrowingDiscardingTaskGroup'
53+
% ],
54+
% [
55+
% # use availability of task group we're declaring within since decl is @AEIC
56+
% '@_unavailableInEmbedded', # since TaskExecutor is not available on embedded
57+
% ],
58+
% ['addTask', 'addTaskUnlessCancelled'],
59+
% [
60+
% 'name: String?',
61+
% 'priority: TaskPriority? = nil',
62+
% # throws and ChildTaskResult will be adjusted per task group type
63+
% 'operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult'
64+
% ],
65+
% ),
4666
% # -----------------------------------------------------------------------
4767
% # === Added TaskExecutor
4868
% (
@@ -266,7 +286,9 @@ extension ${TYPE} {
266286
flags: flags,
267287
initialSerialExecutor: builtinSerialExecutor,
268288
taskGroup: _group,
289+
% if HAS_TASK_EXECUTOR:
269290
initialTaskExecutorConsuming: taskExecutor,
291+
% end
270292
taskName: nameBytes.baseAddress!._rawValue,
271293
operation: operation).0
272294
}
@@ -282,7 +304,9 @@ extension ${TYPE} {
282304
flags: flags,
283305
initialSerialExecutor: builtinSerialExecutor,
284306
taskGroup: _group,
307+
% if HAS_TASK_EXECUTOR:
285308
initialTaskExecutorConsuming: taskExecutor,
309+
% end
286310
operation: operation).0
287311
#else
288312
// legacy branch for the non-consuming task executor
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// RUN: %target-swift-frontend -emit-sil -verify -o /dev/null -verify %s
2+
3+
4+
5+
struct Boom: Error {}
6+
7+
@available(SwiftStdlib 6.2, *)
8+
func testName() {
9+
_ = Task.name
10+
}
11+
12+
@available(SwiftStdlib 6.0, *)
13+
func taskExecutor() async {
14+
Task(name: "name", executorPreference: nil) { }
15+
Task(name: "name", executorPreference: nil) { throw Boom() }
16+
17+
Task.detached(name: "name", executorPreference: nil) { throw Boom() }
18+
19+
await withTaskGroup(of: Void.self) { group in
20+
group.addTask(name: "name", executorPreference: nil) {
21+
()
22+
}
23+
}
24+
await withThrowingTaskGroup(of: Void.self) { group in
25+
group.addTask(name: "name", executorPreference: nil) {
26+
()
27+
}
28+
}
29+
30+
await withDiscardingTaskGroup { group in
31+
group.addTask(name: "name", executorPreference: nil) {
32+
()
33+
}
34+
}
35+
try! await withThrowingDiscardingTaskGroup { group in
36+
group.addTask(name: "name", executorPreference: nil) {
37+
()
38+
}
39+
}
40+
}
41+
42+
@available(SwiftStdlib 5.1, *)
43+
func backDeployedNames() async {
44+
Task(name: "name") { }
45+
Task(name: "name") { throw Boom() }
46+
47+
Task.detached(name: "name") { }
48+
Task.detached(name: "name") { throw Boom() }
49+
50+
await withTaskGroup(of: Void.self) { group in
51+
group.addTask(name: "name") {
52+
()
53+
}
54+
}
55+
await withThrowingTaskGroup(of: Void.self) { group in
56+
group.addTask(name: "name") {
57+
()
58+
}
59+
}
60+
}
61+
62+
@available(SwiftStdlib 5.9, *)
63+
func backDeployedDiscarding() async {
64+
await withDiscardingTaskGroup { group in
65+
group.addTask(name: "name") {
66+
()
67+
}
68+
}
69+
try! await withThrowingDiscardingTaskGroup { group in
70+
group.addTask(name: "name") {
71+
()
72+
}
73+
}
74+
}

test/Concurrency/transfernonsendable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ extension MyActor {
17671767
_ = sc
17681768

17691769
Task { // expected-tns-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1770-
// expected-tns-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
1770+
// expected-tns-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(priority:operation:)' risks causing races in between local and caller code}}
17711771
_ = sc
17721772
}
17731773

test/Concurrency/transfernonsendable_typed_errors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension MyActor {
6060
_ = sc
6161

6262
Task { // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
63-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
63+
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(priority:operation:)' risks causing races in between local and caller code}}
6464
_ = sc
6565
}
6666

0 commit comments

Comments
 (0)