13
13
import Swift
14
14
@_implementationOnly import SwiftConcurrencyInternalShims
15
15
16
- // ==== Task.immediate --------- ------------------------------------------------
16
+ // ==== Task.startSynchronously ------------------------------------------------
17
17
18
18
% METHOD_VARIANTS = [
19
19
% 'THROWING',
20
20
% 'NON_THROWING',
21
21
% ]
22
- % for METHOD_VARIANT in METHOD_VARIANTS:
22
+ % for THROWING_VARIANT in METHOD_VARIANTS:
23
23
24
- % IS_THROWING = METHOD_VARIANT == 'THROWING'
24
+ % IS_THROWING = THROWING_VARIANT == 'THROWING'
25
25
% if IS_THROWING:
26
26
% FAILURE_TYPE = 'Error'
27
27
% THROWS = 'throws '
@@ -51,8 +51,38 @@ extension Task where Failure == ${FAILURE_TYPE} {
51
51
) -> Task<Success, ${FAILURE_TYPE}> {
52
52
immediate(name: name, priority: priority, operation: operation)
53
53
}
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
54
77
78
+ @available(SwiftStdlib 6.2, *)
79
+ extension Task where Failure == ${FAILURE_TYPE} {
80
+
81
+ % if IS_DETACHED:
55
82
/// 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
56
86
///
57
87
/// This function _starts_ the created task on the calling context.
58
88
/// The task will continue executing on the caller's context until it suspends,
@@ -65,8 +95,12 @@ extension Task where Failure == ${FAILURE_TYPE} {
65
95
/// a synchronous manner.
66
96
///
67
97
/// 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
70
104
///
71
105
/// - Parameters:
72
106
/// - name: The high-level human-readable name given for this task
@@ -81,7 +115,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
81
115
@available(SwiftStdlib 6.2, *)
82
116
@_alwaysEmitIntoClient
83
117
@discardableResult
84
- public static func immediate (
118
+ public static func ${METHOD_NAME} (
85
119
name: String? = nil,
86
120
priority: TaskPriority? = nil,
87
121
executorPreference taskExecutor: consuming (any TaskExecutor)? = nil,
@@ -98,14 +132,14 @@ extension Task where Failure == ${FAILURE_TYPE} {
98
132
if let builtinSerialExecutor {
99
133
_taskIsCurrentExecutor(executor: builtinSerialExecutor, flags: flagsMustNotCrash)
100
134
} 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
102
136
}
103
137
104
138
let flags = taskCreateFlags(
105
139
priority: priority,
106
140
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 */'} ,
109
143
enqueueJob: !canRunSynchronously,
110
144
addPendingGroupTaskUnconditionally: false,
111
145
isDiscardingTask: false,
@@ -369,9 +403,9 @@ extension ${GROUP_TYPE} {
369
403
% 'THROWING',
370
404
% 'NON_THROWING',
371
405
% ]
372
- % for METHOD_VARIANT in METHOD_VARIANTS:
406
+ % for THROWING_VARIANT in METHOD_VARIANTS:
373
407
374
- % IS_THROWING = METHOD_VARIANT == 'THROWING'
408
+ % IS_THROWING = THROWING_VARIANT == 'THROWING'
375
409
% if IS_THROWING:
376
410
% FAILURE_TYPE = 'Error'
377
411
% THROWS = 'throws '
0 commit comments