@@ -18,32 +18,31 @@ import Swift
18
18
/// An asynchronous task (just "Task" hereafter) is the analogue of a thread for
19
19
/// asynchronous functions. All asynchronous functions run as part of some task.
20
20
///
21
+ /// A task can only be interacted with by code running "in" the task,
22
+ /// by invoking the appropriate context sensitive static functions which operate
23
+ /// on the "current" task. Because all such functions are `async` they can only
24
+ /// be invoked as part of an existing task, and therefore are guaranteed to be
25
+ /// effective.
26
+ ///
21
27
/// A task's execution can be seen as a series of periods where the task was
22
28
/// running. Each such period ends at a suspension point or -- finally -- the
23
29
/// completion of the task.
24
30
///
25
31
/// These partial periods towards the task's completion are `PartialAsyncTask`.
26
32
/// Partial tasks are generally not interacted with by end-users directly,
27
33
/// unless implementing a scheduler.
28
- public struct Task {
34
+ public enum Task {
29
35
}
30
36
31
- // ==== Current Task - ----------------------------------------------------------
37
+ // ==== Task Priority ----------------------------------------------------------
32
38
33
39
extension Task {
34
- /// Returns the currently executing `Task`.
35
- ///
36
- /// As invoking this function is only possible from an asynchronous context
37
- /// it is always able to return the current `Task` in which we are currently
38
- /// running.
39
- public static func current( ) async -> Task {
40
- fatalError ( " \( #function) not implemented yet. " ) // TODO: needs a built-in function
41
- }
42
- }
43
40
44
- // ==== Task Priority ----------------------------------------------------------
41
+ /// Returns the current task's priority.
42
+ public static func currentPriority( ) async -> Priority {
43
+ fatalError ( " \( #function) not implemented yet. " )
44
+ }
45
45
46
- extension Task {
47
46
/// Task priority may inform decisions an `Executor` makes about how and when
48
47
/// to schedule tasks submitted to it.
49
48
///
@@ -61,7 +60,7 @@ extension Task {
61
60
/// as they are "detached" from their parent tasks after all.
62
61
///
63
62
/// ### Priority elevation
64
- /// In some situations the priority of a task must be elevated ("raised"):
63
+ /// In some situations the priority of a task must be elevated (or "escalated", "raised"):
65
64
///
66
65
/// - if a `Task` running on behalf of an actor, and a new higher-priority
67
66
/// task is enqueued to the actor, its current task must be temporarily
@@ -99,7 +98,7 @@ extension Task {
99
98
/// i.e. the task will run regardless of the handle still being present or not.
100
99
/// Dropping a handle however means losing the ability to await on the task's result
101
100
/// and losing the ability to cancel it.
102
- public final class Handle < Success, Failure : Error > {
101
+ public final class Handle < Success> {
103
102
/// Wait for the task to complete, returning (or throwing) its result.
104
103
///
105
104
/// ### Priority
@@ -108,7 +107,12 @@ extension Task {
108
107
/// creating the task with the "right" priority to in the first place.
109
108
///
110
109
/// ### Cancellation
111
- /// If the awaited on task gets cancelled the `get()` will throw a cancellation error.
110
+ /// If the awaited on task gets cancelled externally the `get()` will throw
111
+ /// a cancellation error.
112
+ ///
113
+ /// If the task gets cancelled internally, e.g. by checking for cancellation
114
+ /// and throwing a specific error or using `checkCancellation` the error
115
+ /// thrown out of the task will be re-thrown here.
112
116
public func get( ) async throws -> Success {
113
117
fatalError ( " \( #function) not implemented yet. " )
114
118
}
@@ -148,21 +152,21 @@ extension Task {
148
152
///
149
153
/// Canceling a task must be performed explicitly via `handle.cancel()`.
150
154
///
151
- /// - Parameters:
152
- /// - priority: priority of the task TODO: reword and define more explicitly once we have priorities well-defined
153
- /// - operation:
154
- /// - Returns: handle to the task, allowing to `await handle.get()` on the
155
- /// tasks result or `cancel` it.
156
- ///
157
155
/// - Note: it is generally preferable to use child tasks rather than detached
158
156
/// tasks. Child tasks automatically carry priorities, task-local state,
159
157
/// deadlines and have other benefits resulting from the structured
160
158
/// concurrency concepts that they model. Consider using detached tasks only
161
159
/// when strictly necessary and impossible to model operations otherwise.
160
+ ///
161
+ /// - Parameters:
162
+ /// - priority: priority of the task TODO: reword and define more explicitly once we have priorities well-defined
163
+ /// - operation: the operation to execute
164
+ /// - Returns: handle to the task, allowing to `await handle.get()` on the
165
+ /// tasks result or `cancel` it.
162
166
public static func runDetached< T> (
163
167
priority: Priority = . default,
164
168
operation: ( ) async -> T
165
- ) -> Handle < T , Never > {
169
+ ) -> Handle < T > {
166
170
fatalError ( " \( #function) not implemented yet. " )
167
171
}
168
172
@@ -184,22 +188,22 @@ extension Task {
184
188
///
185
189
/// Canceling a task must be performed explicitly via `handle.cancel()`.
186
190
///
187
- /// - Parameters:
188
- /// - priority: priority of the task TODO: reword and define more explicitly once we have priorities well-defined
189
- /// - operation:
190
- /// - Returns: handle to the task, allowing to `await handle.get()` on the
191
- /// tasks result or `cancel` it. If the operation fails the handle will
192
- /// throw the error the operation has thrown when awaited on.
193
- ///
194
191
/// - Note: it is generally preferable to use child tasks rather than detached
195
192
/// tasks. Child tasks automatically carry priorities, task-local state,
196
193
/// deadlines and have other benefits resulting from the structured
197
194
/// concurrency concepts that they model. Consider using detached tasks only
198
195
/// when strictly necessary and impossible to model operations otherwise.
196
+ ///
197
+ /// - Parameters:
198
+ /// - priority: priority of the task TODO: reword and define more explicitly once we have priorities well-defined
199
+ /// - operation: the operation to execute
200
+ /// - Returns: handle to the task, allowing to `await handle.get()` on the
201
+ /// tasks result or `cancel` it. If the operation fails the handle will
202
+ /// throw the error the operation has thrown when awaited on.
199
203
public static func runDetached< T> (
200
204
priority: Priority = . default,
201
205
operation: ( ) async throws -> T
202
- ) -> Handle < T , Error > {
206
+ ) -> Handle < T > {
203
207
fatalError ( " \( #function) not implemented yet. " )
204
208
}
205
209
}
0 commit comments