1313import Swift
1414@_implementationOnly import SwiftConcurrencyInternalShims
1515
16- // ==== Task.immediate --------- ------------------------------------------------
16+ // ==== Task.startSynchronously ------------------------------------------------
1717
1818% METHOD_VARIANTS = [
1919% 'THROWING',
2020% 'NON_THROWING',
2121% ]
22- % for METHOD_VARIANT in METHOD_VARIANTS:
22+ % for THROWING_VARIANT in METHOD_VARIANTS:
2323
24- % IS_THROWING = METHOD_VARIANT == 'THROWING'
24+ % IS_THROWING = THROWING_VARIANT == 'THROWING'
2525% if IS_THROWING:
2626% FAILURE_TYPE = 'Error'
2727% THROWS = 'throws '
@@ -51,8 +51,38 @@ extension Task where Failure == ${FAILURE_TYPE} {
5151 ) -> Task<Success, ${FAILURE_TYPE}> {
5252 immediate(name: name, priority: priority, operation: operation)
5353 }
54+ }
55+
56+ % end
57+
58+ // ==== Task.immediate(Detached) ---------------------------------------------------------
59+
60+ % METHOD_VARIANTS = [
61+ % ('immediate', 'THROWING'),
62+ % ('immediate', 'NON_THROWING'),
63+ % ('immediateDetached', 'THROWING'),
64+ % ('immediateDetached', 'NON_THROWING'),
65+ % ]
66+ % for (METHOD_NAME, THROWING_VARIANT) in METHOD_VARIANTS:
67+
68+ % IS_THROWING = THROWING_VARIANT == 'THROWING'
69+ % IS_DETACHED = 'Detached' in METHOD_NAME
70+ % if IS_THROWING:
71+ % FAILURE_TYPE = 'Error'
72+ % THROWS = 'throws '
73+ % else:
74+ % FAILURE_TYPE = 'Never'
75+ % THROWS = ''
76+ % end
5477
78+ @available(SwiftStdlib 6.2, *)
79+ extension Task where Failure == ${FAILURE_TYPE} {
80+
81+ % if IS_DETACHED:
5582 /// Create and immediately start running a new task in the context of the calling thread/task.
83+ % else:
84+ /// Create and immediately start running a new detached task in the context of the calling thread/task.
85+ % end # IS_DETACHED
5686 ///
5787 /// This function _starts_ the created task on the calling context.
5888 /// The task will continue executing on the caller's context until it suspends,
@@ -65,8 +95,12 @@ extension Task where Failure == ${FAILURE_TYPE} {
6595 /// a synchronous manner.
6696 ///
6797 /// Other than the execution semantics discussed above, the created task
68- /// is semantically equivalent to its basic version which can be
69- /// created using ``Task/init``.
98+ /// is semantically equivalent to a task created using
99+ % if IS_DETACHED:
100+ /// the ``Task/detached`` function.
101+ % else:
102+ /// the ``Task/init`` initializer.
103+ % end
70104 ///
71105 /// - Parameters:
72106 /// - name: The high-level human-readable name given for this task
@@ -81,7 +115,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
81115 @available(SwiftStdlib 6.2, *)
82116 @_alwaysEmitIntoClient
83117 @discardableResult
84- public static func immediate (
118+ public static func ${METHOD_NAME} (
85119 name: String? = nil,
86120 priority: TaskPriority? = nil,
87121 executorPreference taskExecutor: consuming (any TaskExecutor)? = nil,
@@ -98,14 +132,14 @@ extension Task where Failure == ${FAILURE_TYPE} {
98132 if let builtinSerialExecutor {
99133 _taskIsCurrentExecutor(executor: builtinSerialExecutor, flags: flagsMustNotCrash)
100134 } else {
101- true // if there is not target executor, we can run synchronously
135+ true // if there is no target executor, we can run synchronously
102136 }
103137
104138 let flags = taskCreateFlags(
105139 priority: priority,
106140 isChildTask: false,
107- copyTaskLocals: true,
108- inheritContext: true,
141+ copyTaskLocals: ${' true' if not IS_DETACHED else 'false /* detached */'} ,
142+ inheritContext: ${' true' if not IS_DETACHED else 'false /* detached */'} ,
109143 enqueueJob: !canRunSynchronously,
110144 addPendingGroupTaskUnconditionally: false,
111145 isDiscardingTask: false,
@@ -369,9 +403,9 @@ extension ${GROUP_TYPE} {
369403% 'THROWING',
370404% 'NON_THROWING',
371405% ]
372- % for METHOD_VARIANT in METHOD_VARIANTS:
406+ % for THROWING_VARIANT in METHOD_VARIANTS:
373407
374- % IS_THROWING = METHOD_VARIANT == 'THROWING'
408+ % IS_THROWING = THROWING_VARIANT == 'THROWING'
375409% if IS_THROWING:
376410% FAILURE_TYPE = 'Error'
377411% THROWS = 'throws '
0 commit comments