Skip to content

[Freestanding] Disable child task priority spec. #61120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions stdlib/public/Concurrency/SourceCompatibilityShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ extension Task where Failure == Never {
}
}

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@available(SwiftStdlib 5.1, *)
extension TaskGroup {
@available(*, deprecated, renamed: "addTask(priority:operation:)")
Expand Down Expand Up @@ -334,7 +335,94 @@ extension TaskGroup {
addTaskUnlessCancelled(priority: priority, operation: operation)
}
}
#else
@available(SwiftStdlib 5.1, *)
extension TaskGroup {
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func add(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) async -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func add(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) async -> Bool {
return self.addTaskUnlessCancelled {
await operation()
}
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func spawn(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTask(operation:)")
@_alwaysEmitIntoClient
public mutating func spawn(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
addTask(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func spawnUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func spawnUnlessCancelled(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
addTaskUnlessCancelled(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func async(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTask(operation:)")
@_alwaysEmitIntoClient
public mutating func async(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
addTask(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func asyncUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func asyncUnlessCancelled(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
addTaskUnlessCancelled(operation: operation)
}
}
#endif

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@available(SwiftStdlib 5.1, *)
extension ThrowingTaskGroup {
@available(*, deprecated, renamed: "addTask(priority:operation:)")
Expand Down Expand Up @@ -384,6 +472,92 @@ extension ThrowingTaskGroup {
addTaskUnlessCancelled(priority: priority, operation: operation)
}
}
#else
@available(SwiftStdlib 5.1, *)
extension ThrowingTaskGroup {
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func add(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) async -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func add(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) async -> Bool {
return self.addTaskUnlessCancelled {
try await operation()
}
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func spawn(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTask(operation:)")
@_alwaysEmitIntoClient
public mutating func spawn(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
addTask(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func spawnUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func spawnUnlessCancelled(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
addTaskUnlessCancelled(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func async(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTask(operation:)")
@_alwaysEmitIntoClient
public mutating func async(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
addTask(operation: operation)
}

@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func asyncUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

@available(*, deprecated, renamed: "addTaskUnlessCancelled(operation:)")
@_alwaysEmitIntoClient
public mutating func asyncUnlessCancelled(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
addTaskUnlessCancelled(operation: operation)
}
}
#endif

@available(SwiftStdlib 5.1, *)
@available(*, deprecated, message: "please use UnsafeContinuation<..., Error>")
Expand Down
157 changes: 157 additions & 0 deletions stdlib/public/Concurrency/TaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
self._group = group
}

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
/// Adds a child task to the group.
///
/// - Parameters:
Expand Down Expand Up @@ -302,6 +303,80 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
fatalError("Unsupported Swift compiler")
#endif
}
#else
@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func addTask(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

/// Adds a child task to the group.
///
/// - Parameters:
/// - operation: The operation to execute as part of the task group.
@_alwaysEmitIntoClient
public mutating func addTask(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
let flags = taskCreateFlags(
priority: nil, isChildTask: true, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: true
)

// Create the task in this group.
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
#else
fatalError("Unsupported Swift compiler")
#endif
}

@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func addTaskUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

/// Adds a child task to the group, unless the group has been canceled.
///
/// - Parameters:
/// - operation: The operation to execute as part of the task group.
/// - Returns: `true` if the child task was added to the group;
/// otherwise `false`.
@_alwaysEmitIntoClient
public mutating func addTaskUnlessCancelled(
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)

guard canAdd else {
// the group is cancelled and is not accepting any new work
return false
}

let flags = taskCreateFlags(
priority: nil, isChildTask: true, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false
)

// Create the task in this group.
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)

return true
#else
fatalError("Unsupported Swift compiler")
#endif
}
#endif

/// Wait for the next child task to complete,
/// and return the value it returned.
Expand Down Expand Up @@ -490,11 +565,13 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
while let _ = try await next() { }
}

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
/// Adds a child task to the group.
///
/// This method doesn't throw an error, even if the child task does.
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
///
/// - Parameters:
/// - overridingPriority: The priority of the operation task.
/// Omit this parameter or pass `.unspecified`
/// to set the child task's priority to the priority of the group.
Expand Down Expand Up @@ -557,6 +634,86 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
fatalError("Unsupported Swift compiler")
#endif
}
#else
@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTask(operation:)")
public mutating func addTask(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
fatalError("Unavailable in task-to-thread concurrency model")
}

/// Adds a child task to the group.
///
/// This method doesn't throw an error, even if the child task does.
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
///
/// - Parameters:
/// - operation: The operation to execute as part of the task group.
@_alwaysEmitIntoClient
public mutating func addTask(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
let flags = taskCreateFlags(
priority: nil, isChildTask: true, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: true
)

// Create the task in this group.
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)
#else
fatalError("Unsupported Swift compiler")
#endif
}

@available(SwiftStdlib 5.7, *)
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model", renamed: "addTaskUnlessCancelled(operation:)")
public mutating func addTaskUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
fatalError("Unavailable in task-to-thread concurrency model")
}

/// Adds a child task to the group, unless the group has been canceled.
///
/// This method doesn't throw an error, even if the child task does.
/// Instead, the corresponding call to `ThrowingTaskGroup.next()` rethrows that error.
///
/// - Parameters:
/// - operation: The operation to execute as part of the task group.
/// - Returns: `true` if the child task was added to the group;
/// otherwise `false`.
@_alwaysEmitIntoClient
public mutating func addTaskUnlessCancelled(
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)

guard canAdd else {
// the group is cancelled and is not accepting any new work
return false
}

let flags = taskCreateFlags(
priority: nil, isChildTask: true, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false
)

// Create the task in this group.
_ = Builtin.createAsyncTaskInGroup(flags, _group, operation)

return true
#else
fatalError("Unsupported Swift compiler")
#endif
}
#endif

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