While Task.detached(executorPreference:priority:operation:)
does not consume the taskExecutor
, Task.init(executorPreference:priority:operation:)
does consume it.
What causes this difference?
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension _Concurrency.Task where Failure == Swift.Never {
#if compiler(>=5.3) && $SendingArgsAndResults
@discardableResult
@_alwaysEmitIntoClient public init(executorPreference taskExecutor: consuming (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: sending @escaping () async -> Success) {
guard let taskExecutor else {
self = Self.init(priority: priority, operation: operation)
return
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: true,
inheritContext: true, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
self._task = task
}
#else
@discardableResult
@_alwaysEmitIntoClient public init(executorPreference taskExecutor: consuming (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: @escaping () async -> Success) {
guard let taskExecutor else {
self = Self.init(priority: priority, operation: operation)
return
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: true,
inheritContext: true, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
self._task = task
}
#endif
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension _Concurrency.Task where Failure == any Swift.Error {
#if compiler(>=5.3) && $SendingArgsAndResults
@discardableResult
@_alwaysEmitIntoClient public init(executorPreference taskExecutor: consuming (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: sending @escaping () async throws -> Success) {
guard let taskExecutor else {
self = Self.init(priority: priority, operation: operation)
return
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: true,
inheritContext: true, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
self._task = task
}
#else
@discardableResult
@_alwaysEmitIntoClient public init(executorPreference taskExecutor: consuming (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: @escaping () async throws -> Success) {
guard let taskExecutor else {
self = Self.init(priority: priority, operation: operation)
return
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: true,
inheritContext: true, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
self._task = task
}
#endif
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension _Concurrency.Task where Failure == Swift.Never {
#if compiler(>=5.3) && $SendingArgsAndResults
@discardableResult
@_alwaysEmitIntoClient public static func detached(executorPreference taskExecutor: (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: sending @escaping () async -> Success) -> _Concurrency.Task<Success, Failure> {
guard let taskExecutor else {
return Self.detached(priority: priority, operation: operation)
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
return Task(task)
}
#else
@discardableResult
@_alwaysEmitIntoClient public static func detached(executorPreference taskExecutor: (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: @escaping () async -> Success) -> _Concurrency.Task<Success, Failure> {
guard let taskExecutor else {
return Self.detached(priority: priority, operation: operation)
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
return Task(task)
}
#endif
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension _Concurrency.Task where Failure == any Swift.Error {
#if compiler(>=5.3) && $SendingArgsAndResults
@discardableResult
@_alwaysEmitIntoClient public static func detached(executorPreference taskExecutor: (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: sending @escaping () async throws -> Success) -> _Concurrency.Task<Success, Failure> {
guard let taskExecutor else {
return Self.detached(priority: priority, operation: operation)
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
return Task(task)
}
#else
@discardableResult
@_alwaysEmitIntoClient public static func detached(executorPreference taskExecutor: (any _Concurrency.TaskExecutor)?, priority: _Concurrency.TaskPriority? = nil, operation: @escaping () async throws -> Success) -> _Concurrency.Task<Success, Failure> {
guard let taskExecutor else {
return Self.detached(priority: priority, operation: operation)
}
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false,
isDiscardingTask: false)
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
let (task, _) = Builtin.createTask(
flags: flags,
initialTaskExecutorConsuming: taskExecutor,
operation: operation)
#else
let executorBuiltin: Builtin.Executor =
taskExecutor.asUnownedTaskExecutor().executor
let (task, _) = Builtin.createAsyncTaskWithExecutor(
flags, executorBuiltin, operation)
#endif
return Task(task)
}
#endif
}