Skip to content

Commit 4a6e275

Browse files
committed
[Freestanding] Disable child task priority spec.
Under the task-to-thread model, specifying a priority doesn't make sense. Here, variations of addTask and addTaskUnlessCancelled are introduced which do not take a priority. Additionally, the original functions are made unavailable.
1 parent 6abd848 commit 4a6e275

File tree

3 files changed

+375
-0
lines changed

3 files changed

+375
-0
lines changed

stdlib/public/Concurrency/SourceCompatibilityShims.swift

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ extension Task where Failure == Never {
285285
}
286286
}
287287

288+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
288289
@available(SwiftStdlib 5.1, *)
289290
extension TaskGroup {
290291
@available(*, deprecated, renamed: "addTask(priority:operation:)")
@@ -334,7 +335,94 @@ extension TaskGroup {
334335
addTaskUnlessCancelled(priority: priority, operation: operation)
335336
}
336337
}
338+
#else
339+
@available(SwiftStdlib 5.1, *)
340+
extension TaskGroup {
341+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
342+
public mutating func add(
343+
priority: TaskPriority? = nil,
344+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
345+
) async -> Bool {
346+
fatalError("Unavailable in task-to-thread concurrency model")
347+
}
348+
349+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
350+
@_alwaysEmitIntoClient
351+
public mutating func add(
352+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
353+
) async -> Bool {
354+
return self.addTaskUnlessCancelled {
355+
await operation()
356+
}
357+
}
358+
359+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
360+
public mutating func spawn(
361+
priority: TaskPriority? = nil,
362+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
363+
) {
364+
fatalError("Unavailable in task-to-thread concurrency model")
365+
}
366+
367+
@available(*, deprecated, renamed: "addTask(operation:)")
368+
@_alwaysEmitIntoClient
369+
public mutating func spawn(
370+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
371+
) {
372+
addTask(operation: operation)
373+
}
374+
375+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
376+
public mutating func spawnUnlessCancelled(
377+
priority: TaskPriority? = nil,
378+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
379+
) -> Bool {
380+
fatalError("Unavailable in task-to-thread concurrency model")
381+
}
382+
383+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
384+
@_alwaysEmitIntoClient
385+
public mutating func spawnUnlessCancelled(
386+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
387+
) -> Bool {
388+
addTaskUnlessCancelled(operation: operation)
389+
}
390+
391+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
392+
public mutating func async(
393+
priority: TaskPriority? = nil,
394+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
395+
) {
396+
fatalError("Unavailable in task-to-thread concurrency model")
397+
}
398+
399+
@available(*, deprecated, renamed: "addTask(operation:)")
400+
@_alwaysEmitIntoClient
401+
public mutating func async(
402+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
403+
) {
404+
addTask(operation: operation)
405+
}
406+
407+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
408+
public mutating func asyncUnlessCancelled(
409+
priority: TaskPriority? = nil,
410+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
411+
) -> Bool {
412+
fatalError("Unavailable in task-to-thread concurrency model")
413+
}
414+
415+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
416+
@_alwaysEmitIntoClient
417+
public mutating func asyncUnlessCancelled(
418+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
419+
) -> Bool {
420+
addTaskUnlessCancelled(operation: operation)
421+
}
422+
}
423+
#endif
337424

425+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
338426
@available(SwiftStdlib 5.1, *)
339427
extension ThrowingTaskGroup {
340428
@available(*, deprecated, renamed: "addTask(priority:operation:)")
@@ -384,6 +472,92 @@ extension ThrowingTaskGroup {
384472
addTaskUnlessCancelled(priority: priority, operation: operation)
385473
}
386474
}
475+
#else
476+
@available(SwiftStdlib 5.1, *)
477+
extension ThrowingTaskGroup {
478+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
479+
public mutating func add(
480+
priority: TaskPriority? = nil,
481+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
482+
) async -> Bool {
483+
fatalError("Unavailable in task-to-thread concurrency model")
484+
}
485+
486+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
487+
@_alwaysEmitIntoClient
488+
public mutating func add(
489+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
490+
) async -> Bool {
491+
return self.addTaskUnlessCancelled {
492+
try await operation()
493+
}
494+
}
495+
496+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
497+
public mutating func spawn(
498+
priority: TaskPriority? = nil,
499+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
500+
) {
501+
fatalError("Unavailable in task-to-thread concurrency model")
502+
}
503+
504+
@available(*, deprecated, renamed: "addTask(operation:)")
505+
@_alwaysEmitIntoClient
506+
public mutating func spawn(
507+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
508+
) {
509+
addTask(operation: operation)
510+
}
511+
512+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
513+
public mutating func spawnUnlessCancelled(
514+
priority: TaskPriority? = nil,
515+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
516+
) -> Bool {
517+
fatalError("Unavailable in task-to-thread concurrency model")
518+
}
519+
520+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
521+
@_alwaysEmitIntoClient
522+
public mutating func spawnUnlessCancelled(
523+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
524+
) -> Bool {
525+
addTaskUnlessCancelled(operation: operation)
526+
}
527+
528+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
529+
public mutating func async(
530+
priority: TaskPriority? = nil,
531+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
532+
) {
533+
fatalError("Unavailable in task-to-thread concurrency model")
534+
}
535+
536+
@available(*, deprecated, renamed: "addTask(operation:)")
537+
@_alwaysEmitIntoClient
538+
public mutating func async(
539+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
540+
) {
541+
addTask(operation: operation)
542+
}
543+
544+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
545+
public mutating func asyncUnlessCancelled(
546+
priority: TaskPriority? = nil,
547+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
548+
) -> Bool {
549+
fatalError("Unavailable in task-to-thread concurrency model")
550+
}
551+
552+
@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
553+
@_alwaysEmitIntoClient
554+
public mutating func asyncUnlessCancelled(
555+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
556+
) -> Bool {
557+
addTaskUnlessCancelled(operation: operation)
558+
}
559+
}
560+
#endif
387561

388562
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
389563
@available(SwiftStdlib 5.1, *)

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
225225
self._group = group
226226
}
227227

228+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
228229
/// Adds a child task to the group.
229230
///
230231
/// - Parameters:
@@ -287,6 +288,80 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
287288
fatalError("Unsupported Swift compiler")
288289
#endif
289290
}
291+
#else
292+
@available(SwiftStdlib 5.7, *)
293+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
294+
public mutating func addTask(
295+
priority: TaskPriority? = nil,
296+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
297+
) {
298+
fatalError("Unavailable in task-to-thread concurrency model")
299+
}
300+
301+
/// Adds a child task to the group.
302+
///
303+
/// - Parameters:
304+
/// - operation: The operation to execute as part of the task group.
305+
@_alwaysEmitIntoClient
306+
public mutating func addTask(
307+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
308+
) {
309+
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
310+
let flags = taskCreateFlags(
311+
priority: nil, isChildTask: true, copyTaskLocals: false,
312+
inheritContext: false, enqueueJob: true,
313+
addPendingGroupTaskUnconditionally: true
314+
)
315+
316+
// Create the task in this group.
317+
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
318+
#else
319+
fatalError("Unsupported Swift compiler")
320+
#endif
321+
}
322+
323+
@available(SwiftStdlib 5.7, *)
324+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
325+
public mutating func addTaskUnlessCancelled(
326+
priority: TaskPriority? = nil,
327+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
328+
) -> Bool {
329+
fatalError("Unavailable in task-to-thread concurrency model")
330+
}
331+
332+
/// Adds a child task to the group, unless the group has been canceled.
333+
///
334+
/// - Parameters:
335+
/// - operation: The operation to execute as part of the task group.
336+
/// - Returns: `true` if the child task was added to the group;
337+
/// otherwise `false`.
338+
@_alwaysEmitIntoClient
339+
public mutating func addTaskUnlessCancelled(
340+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
341+
) -> Bool {
342+
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
343+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
344+
345+
guard canAdd else {
346+
// the group is cancelled and is not accepting any new work
347+
return false
348+
}
349+
350+
let flags = taskCreateFlags(
351+
priority: nil, isChildTask: true, copyTaskLocals: false,
352+
inheritContext: false, enqueueJob: true,
353+
addPendingGroupTaskUnconditionally: false
354+
)
355+
356+
// Create the task in this group.
357+
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
358+
359+
return true
360+
#else
361+
fatalError("Unsupported Swift compiler")
362+
#endif
363+
}
364+
#endif
290365

291366
/// Wait for the next child task to complete,
292367
/// and return the value it returned.
@@ -475,11 +550,13 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
475550
while let _ = try await next() { }
476551
}
477552

553+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
478554
/// Adds a child task to the group.
479555
///
480556
/// This method doesn't throw an error, even if the child task does.
481557
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
482558
///
559+
/// - Parameters:
483560
/// - overridingPriority: The priority of the operation task.
484561
/// Omit this parameter or pass `.unspecified`
485562
/// to set the child task's priority to the priority of the group.
@@ -542,6 +619,86 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
542619
fatalError("Unsupported Swift compiler")
543620
#endif
544621
}
622+
#else
623+
@available(SwiftStdlib 5.7, *)
624+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
625+
public mutating func addTask(
626+
priority: TaskPriority? = nil,
627+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
628+
) {
629+
fatalError("Unavailable in task-to-thread concurrency model")
630+
}
631+
632+
/// Adds a child task to the group.
633+
///
634+
/// This method doesn't throw an error, even if the child task does.
635+
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
636+
///
637+
/// - Parameters:
638+
/// - operation: The operation to execute as part of the task group.
639+
@_alwaysEmitIntoClient
640+
public mutating func addTask(
641+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
642+
) {
643+
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
644+
let flags = taskCreateFlags(
645+
priority: nil, isChildTask: true, copyTaskLocals: false,
646+
inheritContext: false, enqueueJob: true,
647+
addPendingGroupTaskUnconditionally: true
648+
)
649+
650+
// Create the task in this group.
651+
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
652+
#else
653+
fatalError("Unsupported Swift compiler")
654+
#endif
655+
}
656+
657+
@available(SwiftStdlib 5.7, *)
658+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
659+
public mutating func addTaskUnlessCancelled(
660+
priority: TaskPriority? = nil,
661+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
662+
) -> Bool {
663+
fatalError("Unavailable in task-to-thread concurrency model")
664+
}
665+
666+
/// Adds a child task to the group, unless the group has been canceled.
667+
///
668+
/// This method doesn't throw an error, even if the child task does.
669+
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
670+
///
671+
/// - Parameters:
672+
/// - operation: The operation to execute as part of the task group.
673+
/// - Returns: `true` if the child task was added to the group;
674+
/// otherwise `false`.
675+
@_alwaysEmitIntoClient
676+
public mutating func addTaskUnlessCancelled(
677+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
678+
) -> Bool {
679+
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
680+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
681+
682+
guard canAdd else {
683+
// the group is cancelled and is not accepting any new work
684+
return false
685+
}
686+
687+
let flags = taskCreateFlags(
688+
priority: nil, isChildTask: true, copyTaskLocals: false,
689+
inheritContext: false, enqueueJob: true,
690+
addPendingGroupTaskUnconditionally: false
691+
)
692+
693+
// Create the task in this group.
694+
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
695+
696+
return true
697+
#else
698+
fatalError("Unsupported Swift compiler")
699+
#endif
700+
}
701+
#endif
545702

546703
/// Wait for the next child task to complete,
547704
/// and return the value it returned or rethrow the error it threw.

0 commit comments

Comments
 (0)