Skip to content

Commit 579c89c

Browse files
committed
[Concurrency] More documentation of Task.Priority
1 parent d6adac3 commit 579c89c

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,43 @@ extension Task {
4444
// ==== Task Priority ----------------------------------------------------------
4545

4646
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).
4779
public struct Priority: Comparable {
4880
public static let `default`: Task.Priority = .init() // TODO: replace with actual values
4981

5082
// TODO: specifics of implementation are not decided yet
51-
let __value: Int = 0
83+
private let __value: Int = 0
5284

5385
public static func < (lhs: Self, rhs: Self) -> Bool {
5486
lhs.__value < rhs.__value
@@ -60,33 +92,37 @@ extension Task {
6092

6193
extension Task {
6294

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.
65102
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+
}
66112

67-
/// Wait for the task to complete, returning (or throwing) its result.
113+
/// Attempt to cancel the task.
68114
///
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+
}
87124
}
88125
}
89-
}
90126

91127
extension Task.Handle where Failure == Never {
92128
/// Wait for the task to complete, returning its result.

test/Concurrency/async_tasks.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,5 @@ func test_detached_throwing() async -> String {
6666
// ==== Current Task -----------------------------------------------------------
6767

6868
func test_current_task() async {
69-
let task = await Task.current() // yay, we know "in" what task we're executing
70-
}
71-
72-
func test_current_task_notasync() {
73-
let task = Task.current()
69+
_ = await Task.current() // yay, we know "in" what task we're executing
7470
}

0 commit comments

Comments
 (0)