Skip to content

Commit ce3f595

Browse files
committed
[Concurrency] Further overload removals for Task.init
1 parent b354a3e commit ce3f595

File tree

1 file changed

+43
-82
lines changed

1 file changed

+43
-82
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 43 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -580,70 +580,6 @@ func taskCreateFlags(
580580
return bits
581581
}
582582

583-
// ==== Task Creation ----------------------------------------------------------
584-
585-
@available(SwiftStdlib 5.1, *)
586-
extension Task where Failure == Never {
587-
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
588-
@discardableResult
589-
@_alwaysEmitIntoClient
590-
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
591-
public init(
592-
priority: TaskPriority? = nil,
593-
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success
594-
) {
595-
fatalError("Unavailable in task-to-thread concurrency model.")
596-
}
597-
#else
598-
/// Runs the given nonthrowing operation asynchronously
599-
/// as part of a new top-level task on behalf of the current actor.
600-
///
601-
/// Use this function when creating asynchronous work
602-
/// that operates on behalf of the synchronous function that calls it.
603-
/// Like `Task.detached(priority:operation:)`,
604-
/// this function creates a separate, top-level task.
605-
/// Unlike `Task.detached(priority:operation:)`,
606-
/// the task created by `Task.init(priority:operation:)`
607-
/// inherits the priority and actor context of the caller,
608-
/// so the operation is treated more like an asynchronous extension
609-
/// to the synchronous operation.
610-
///
611-
/// You need to keep a reference to the task
612-
/// if you want to cancel it by calling the `Task.cancel()` method.
613-
/// Discarding your reference to a detached task
614-
/// doesn't implicitly cancel that task,
615-
/// it only makes it impossible for you to explicitly cancel the task.
616-
///
617-
/// - Parameters:
618-
/// - priority: The priority of the task.
619-
/// Pass `nil` to use the priority from `Task.currentPriority`.
620-
/// - operation: The operation to perform.
621-
@discardableResult
622-
@_alwaysEmitIntoClient
623-
public init(
624-
priority: TaskPriority? = nil,
625-
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success
626-
) {
627-
// Set up the job flags for a new task.
628-
let flags = taskCreateFlags(
629-
priority: priority, isChildTask: false, copyTaskLocals: true,
630-
inheritContext: true, enqueueJob: true,
631-
addPendingGroupTaskUnconditionally: false,
632-
isDiscardingTask: false, isSynchronousStart: false)
633-
634-
// Create the asynchronous task.
635-
let builtinSerialExecutor =
636-
unsafe Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
637-
638-
let (task, _) = Builtin.createTask(flags: flags,
639-
initialSerialExecutor: builtinSerialExecutor,
640-
operation: operation)
641-
642-
self._task = task
643-
}
644-
#endif
645-
}
646-
647583
@available(SwiftStdlib 5.1, *)
648584
extension Task where Failure == Never {
649585
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
@@ -662,10 +598,10 @@ extension Task where Failure == Never {
662598
@_alwaysEmitIntoClient
663599
@available(SwiftStdlib 5.1, *)
664600
public init( // Task.init where Failure == Never (Embedded)
665-
name: String? = nil, // ignored
601+
name: String? = nil,
666602
// TaskExecutor is unavailable in embedded
667603
priority: TaskPriority? = nil,
668-
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping () async -> Success
604+
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async -> Success
669605
) {
670606
// Set up the job flags for a new task.
671607
let flags = taskCreateFlags(
@@ -678,9 +614,31 @@ extension Task where Failure == Never {
678614
isDiscardingTask: false,
679615
isSynchronousStart: false)
680616

681-
let (task, _) = Builtin.createAsyncTask(flags, operation)
617+
let builtinSerialExecutor =
618+
unsafe Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
682619

683-
self._task = task
620+
var task: Builtin.NativeObject?
621+
#if $BuiltinCreateAsyncTaskName
622+
if let name {
623+
task =
624+
unsafe name.utf8CString.withUnsafeBufferPointer { nameBytes in
625+
Builtin.createTask(
626+
flags: flags,
627+
initialSerialExecutor: builtinSerialExecutor,
628+
taskName: nameBytes.baseAddress!._rawValue,
629+
operation: operation).0
630+
}
631+
} // let name
632+
#endif // $BuiltinCreateAsyncTaskName
633+
634+
if task == nil {
635+
task = Builtin.createTask(
636+
flags: flags,
637+
initialSerialExecutor: builtinSerialExecutor,
638+
operation: operation).0
639+
}
640+
641+
self._task = task!
684642
}
685643
#else
686644
/// Runs the given nonthrowing operation asynchronously
@@ -817,25 +775,28 @@ extension Task where Failure == Error {
817775

818776
var task: Builtin.NativeObject?
819777
#if $BuiltinCreateAsyncTaskName
820-
if let name {
821-
task =
822-
unsafe name.utf8CString.withUnsafeBufferPointer { nameBytes in
823-
Builtin.createTask(
824-
flags: flags,
825-
initialSerialExecutor: builtinSerialExecutor,
826-
taskName: nameBytes.baseAddress!._rawValue,
827-
operation: operation).0
828-
}
829-
}
778+
// if let name {
779+
// task =
780+
// unsafe name.utf8CString.withUnsafeBufferPointer { nameBytes in
781+
// Builtin.createTask(
782+
// flags: flags,
783+
// initialSerialExecutor: builtinSerialExecutor,
784+
// taskName: nameBytes.baseAddress!._rawValue,
785+
// operation: operation).0
786+
// }
787+
// }
830788
#endif
831789

832790
if task == nil {
833791
// either no task name was set, or names are unsupported
834-
task = Builtin.createTask(
835-
flags: flags,
836-
initialSerialExecutor: builtinSerialExecutor,
837-
operation: operation).0
792+
// task = Builtin.createTask(
793+
// flags: flags,
794+
// initialSerialExecutor: builtinSerialExecutor,
795+
// operation: operation).0
838796
}
797+
798+
799+
839800

840801
self._task = task!
841802
}

0 commit comments

Comments
 (0)