@@ -44,11 +44,43 @@ extension Task {
44
44
// ==== Task Priority ----------------------------------------------------------
45
45
46
46
extension Task {
47
+ /// Task priority may inform decisions an `Executor` makes about how and when
48
+ /// to schedule tasks submitted to it.
49
+ ///
50
+ /// ### Priority scheduling
51
+ /// An executor MAY utilize priority information to attempt running higher
52
+ /// priority tasks first, and then continuing to serve lower priority tasks.
53
+ ///
54
+ /// The exact semantics of how priority is treated are left up to each
55
+ /// platform and `Executor` implementation.
56
+ ///
57
+ /// ### Priority inheritance
58
+ /// Child tasks automatically inherit their parent task's priority.
59
+ ///
60
+ /// Detached tasks (created by `Task.runDetached`) DO NOT inherit task priority,
61
+ /// as they are "detached" from their parent tasks after all.
62
+ ///
63
+ /// ### Priority elevation
64
+ /// In some situations the priority of a task must be elevated ("raised"):
65
+ ///
66
+ /// - if a `Task` running on behalf of an actor, and a new higher-priority
67
+ /// task is enqueued to the actor, its current task must be temporarily
68
+ /// elevated to the priority of the enqueued task, in order to allow the new
69
+ /// task to be processed at--effectively-- the priority it was enqueued with.
70
+ /// - this DOES NOT affect `Task.currentPriority()`.
71
+ /// - if a task is created with a `Task.Handle`, and a higher-priority task
72
+ /// calls the `await handle.get()` function the priority of this task must be
73
+ /// permanently increased until the task completes.
74
+ /// - this DOES affect `Task.currentPriority()`.
75
+ ///
76
+ /// TODO: Define the details of task priority; It is likely to be a concept
77
+ /// similar to Darwin Dispatch's QoS; bearing in mind that priority is not as
78
+ /// much of a thing on other platforms (i.e. server side Linux systems).
47
79
public struct Priority : Comparable {
48
80
public static let `default` : Task . Priority = . init( ) // TODO: replace with actual values
49
81
50
82
// TODO: specifics of implementation are not decided yet
51
- let __value : Int = 0
83
+ private let __value : Int = 0
52
84
53
85
public static func < ( lhs: Self , rhs: Self ) -> Bool {
54
86
lhs. __value < rhs. __value
@@ -60,33 +92,37 @@ extension Task {
60
92
61
93
extension Task {
62
94
63
- /// A task handle refers to an in-flight `Task`, allowing for (potentially)
64
- /// awaiting for its result or potentially canceling it.
95
+ /// A task handle refers to an in-flight `Task`,
96
+ /// allowing for potentially awaiting for its result or canceling it.
97
+ ///
98
+ /// It is not a programming error to drop a handle without awaiting or canceling it,
99
+ /// i.e. the task will run regardless of the handle still being present or not.
100
+ /// Dropping a handle however means losing the ability to await on the task's result
101
+ /// and losing the ability to cancel it.
65
102
public final class Handle < Success, Failure: Error > {
103
+ /// Wait for the task to complete, returning (or throwing) its result.
104
+ ///
105
+ /// ### Priority
106
+ /// If the task has not completed yet, its priority will be elevated to the
107
+ /// priority of the current task. Note that this may not be as effective as
108
+ /// creating the task with the "right" priority to in the first place.
109
+ public func get( ) async throws -> Success {
110
+ fatalError ( " \( #function) not implemented yet. " )
111
+ }
66
112
67
- /// Wait for the task to complete, returning (or throwing) its result .
113
+ /// Attempt to cancel the task .
68
114
///
69
- /// ### Priority
70
- /// If the task has not completed yet, its priority will be elevated to the
71
- /// priority of the current task. Note that this may not be as effective as
72
- /// creating the task with the "right" priority to in the first place.
73
- public func get( ) async throws -> Success {
74
- fatalError ( " \( #function) not implemented yet. " )
75
- }
76
-
77
- /// Attempt to cancel the task.
78
- ///
79
- /// Whether this function has any effect is task-dependent.
80
- ///
81
- /// For a task to respect cancellation it must cooperatively check for it
82
- /// while running. Many tasks will check for cancellation before beginning
83
- /// their "actual work", however this is not a requirement nor is it guaranteed
84
- /// how and when tasks check for cancellation in general.
85
- public func cancel( ) {
86
- fatalError ( " \( #function) not implemented yet. " )
115
+ /// Whether this function has any effect is task-dependent.
116
+ ///
117
+ /// For a task to respect cancellation it must cooperatively check for it
118
+ /// while running. Many tasks will check for cancellation before beginning
119
+ /// their "actual work", however this is not a requirement nor is it guaranteed
120
+ /// how and when tasks check for cancellation in general.
121
+ public func cancel( ) {
122
+ fatalError ( " \( #function) not implemented yet. " )
123
+ }
87
124
}
88
125
}
89
- }
90
126
91
127
extension Task . Handle where Failure == Never {
92
128
/// Wait for the task to complete, returning its result.
0 commit comments