Skip to content

Commit d5caa48

Browse files
committed
task names review update
1 parent a74a445 commit d5caa48

File tree

6 files changed

+237
-378
lines changed

6 files changed

+237
-378
lines changed

Runtimes/Core/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_subdirectory(InternalShims)
22

3+
gyb_expand(Task+init.swift.gyb Task+init.swift)
34
gyb_expand(TaskGroup+addTask.swift.gyb TaskGroup+addTask.swift)
45
gyb_expand(Task+startSynchronously.swift.gyb Task+startSynchronously.swift)
56

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
212212
${SWIFT_RUNTIME_CONCURRENCY_NONEMBEDDED_SWIFT_SOURCES}
213213

214214
GYB_SOURCES
215+
Task+init.swift.gyb
215216
TaskGroup+addTask.swift.gyb
216217
Task+startSynchronously.swift.gyb
217218

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,14 @@ extension Task where Failure == Never {
237237
}
238238
// Set up the job flags for a new task.
239239
let flags = taskCreateFlags(
240-
priority: priority, isChildTask: false, copyTaskLocals: true,
241-
inheritContext: true, enqueueJob: true,
240+
priority: priority,
241+
isChildTask: false,
242+
copyTaskLocals: true,
243+
inheritContext: true,
244+
enqueueJob: true,
242245
addPendingGroupTaskUnconditionally: false,
243-
isDiscardingTask: false, isSynchronousStart: false)
246+
isDiscardingTask: false,
247+
isSynchronousStart: false)
244248

245249
#if $BuiltinCreateAsyncTaskOwnedTaskExecutor
246250
let (task, _) = Builtin.createTask(
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Swift
14+
15+
// ==== Task.init ------------------------------------------------
16+
17+
% for (METHOD_VARIANTS, ALL_AVAILABILITY, PARAMS) in [
18+
% (
19+
% [ # METHOD_VARIANT
20+
% '',
21+
% 'THROWING',
22+
% ],
23+
% [ # ALL_AVAILABILITY
24+
% '@_alwaysEmitIntoClient',
25+
% '@available(SwiftStdlib 5.1, *)',
26+
% ],
27+
% [ # PARAMS
28+
% 'name: String?',
29+
% 'priority: TaskPriority? = nil',
30+
% '@_inheritActorContext @_implicitSelfCapture operation: sending @escaping @isolated(any) () async throws -> Success',
31+
% ]),
32+
% # ====
33+
% (
34+
% [ # METHOD_VARIANT
35+
% 'DETACHED',
36+
% 'DETACHED THROWING',
37+
% ],
38+
% [
39+
% '@_alwaysEmitIntoClient',
40+
% '@available(SwiftStdlib 5.1, *)',
41+
% ],
42+
% [ # PARAMS
43+
% 'name: String?',
44+
% 'priority: TaskPriority? = nil',
45+
% 'operation: sending @escaping @isolated(any) () async throws -> Success',
46+
% ]),
47+
% ]:
48+
% for METHOD_VARIANT in METHOD_VARIANTS:
49+
50+
% IS_DETACHED = 'DETACHED' in METHOD_VARIANT
51+
% IS_THROWING = 'THROWING' in METHOD_VARIANT
52+
% if IS_THROWING:
53+
% FAILURE_TYPE = 'Error'
54+
% else:
55+
% FAILURE_TYPE = 'Never'
56+
% end
57+
58+
%
59+
% def adjust_params_for_kind(params):
60+
% res = []
61+
% for p in params:
62+
% np = p
63+
% if not IS_THROWING:
64+
% np = np.replace("throws", "")
65+
% res.append(np)
66+
% return res
67+
%
68+
%
69+
% HAS_TASK_PRIORITY = any('priority:' in param for param in PARAMS)
70+
% HAS_TASK_NAME = any('name:' in param for param in PARAMS)
71+
% HAS_TASK_EXECUTOR = any('taskExecutor:' in param for param in PARAMS)
72+
73+
% if IS_DETACHED:
74+
% ARROW_RETURN_TYPE = f'-> Task<Success, {FAILURE_TYPE}>'
75+
% else:
76+
% ARROW_RETURN_TYPE = '' # init does not spell out return type
77+
% end
78+
79+
% # ====================================================================================================================
80+
@available(SwiftStdlib 5.1, *)
81+
extension Task where Failure == ${FAILURE_TYPE} {
82+
83+
% # --------------------------------------------------------------------------------------------------------------------
84+
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
85+
@discardableResult
86+
@_alwaysEmitIntoClient
87+
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
88+
% if IS_DETACHED:
89+
public static func detached(
90+
% else:
91+
public init(
92+
% end
93+
${",\n ".join(adjust_params_for_kind(PARAMS))}
94+
) ${ARROW_RETURN_TYPE}{
95+
fatalError("Unavailable in task-to-thread concurrency model.")
96+
}
97+
98+
% # --------------------------------------------------------------------------------------------------------------------
99+
#elseif $Embedded
100+
@discardableResult
101+
@_alwaysEmitIntoClient
102+
@available(SwiftStdlib 5.1, *)
103+
% if IS_DETACHED:
104+
public static func detached(
105+
% else:
106+
public init(
107+
% end
108+
${",\n ".join(adjust_params_for_kind(PARAMS))}
109+
) ${ARROW_RETURN_TYPE}{
110+
// Set up the job flags for a new task.
111+
let flags = taskCreateFlags(
112+
priority: priority,
113+
isChildTask: false,
114+
copyTaskLocals: ${'true' if not IS_DETACHED else 'false /* detached */'},
115+
inheritContext: true,
116+
enqueueJob: true,
117+
addPendingGroupTaskUnconditionally: false,
118+
isDiscardingTask: false,
119+
isSynchronousStart: false)
120+
121+
// Create the asynchronous task.
122+
let (task, _) = Builtin.createAsyncTask(flags, operation)
123+
124+
self._task = task
125+
}
126+
127+
% # --------------------------------------------------------------------------------------------------------------------
128+
#else
129+
/// Runs the given nonthrowing operation asynchronously
130+
/// as part of a new top-level task.
131+
///
132+
/// Don't use a detached task if it's possible
133+
/// to model the operation using structured concurrency features like child tasks.
134+
/// Child tasks inherit the parent task's priority and task-local storage,
135+
/// and canceling a parent task automatically cancels all of its child tasks.
136+
/// You need to handle these considerations manually with a detached task.
137+
///
138+
/// You need to keep a reference to the detached task
139+
/// if you want to cancel it by calling the `Task.cancel()` method.
140+
/// Discarding your reference to a detached task
141+
/// doesn't implicitly cancel that task,
142+
/// it only makes it impossible for you to explicitly cancel the task.
143+
///
144+
/// - Parameters:
145+
% if HAS_TASK_NAME:
146+
/// - name: Human readable name of the task.
147+
% end
148+
/// - priority: The priority of the task.
149+
/// - operation: The operation to perform.
150+
///
151+
/// - Returns: A reference to the task.
152+
${"\n ".join(ALL_AVAILABILITY)}
153+
@discardableResult
154+
% if IS_DETACHED:
155+
public static func detached(
156+
% else:
157+
public init(
158+
% end
159+
${",\n ".join(adjust_params_for_kind(PARAMS))}
160+
) ${ARROW_RETURN_TYPE}{
161+
162+
// Set up the job flags for a new task.
163+
let flags = taskCreateFlags(
164+
priority: priority,
165+
isChildTask: false,
166+
copyTaskLocals: ${'true' if not IS_DETACHED else 'false'},
167+
inheritContext: false,
168+
enqueueJob: true,
169+
addPendingGroupTaskUnconditionally: false,
170+
isDiscardingTask: false,
171+
isSynchronousStart: false)
172+
173+
// Create the asynchronous task.
174+
let builtinSerialExecutor =
175+
unsafe Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
176+
177+
var task: Builtin.NativeObject?
178+
#if $BuiltinCreateAsyncTaskName
179+
if let name {
180+
task =
181+
unsafe name.utf8CString.withUnsafeBufferPointer { nameBytes in
182+
Builtin.createTask(
183+
flags: flags,
184+
initialSerialExecutor: builtinSerialExecutor,
185+
taskName: nameBytes.baseAddress!._rawValue,
186+
operation: operation).0
187+
}
188+
}
189+
#endif
190+
if task == nil {
191+
// either no task name was set, or names are unsupported
192+
task = Builtin.createTask(
193+
flags: flags,
194+
initialSerialExecutor: builtinSerialExecutor,
195+
operation: operation).0
196+
}
197+
198+
% if IS_DETACHED:
199+
return Task(task!)
200+
% else:
201+
self._task = task!
202+
% end
203+
}
204+
205+
#endif
206+
} // extension Task ...
207+
208+
% end
209+
% end

0 commit comments

Comments
 (0)