Skip to content

Commit 67ed437

Browse files
committed
[Concurrency] Have Task.currentPriority query the system as a fallback.
When `Task.currentPriority` is evaluated outside of an actual task, query the system to determine the priority at which the code is currently executing. This is the behavior that `async` specifies. Extend that to `currentPriority` rather than guessing `.default`.
1 parent d448a77 commit 67ed437

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@ extension Task {
4747

4848
/// Returns the `current` task's priority.
4949
///
50-
/// If no current `Task` is available, returns `Priority.default`.
50+
/// If no current `Task` is available, queries the system to determine the
51+
/// priority at which the current function is running. If the system cannot
52+
/// provide an appropriate priority, returns `Priority.default`.
5153
///
5254
/// - SeeAlso: `Task.Priority`
5355
/// - SeeAlso: `Task.priority`
5456
public static var currentPriority: Priority {
5557
withUnsafeCurrentTask { task in
56-
guard let task = task else {
57-
return Priority.default
58+
// If we are running on behalf of a task, use that task's priority.
59+
if let task = task {
60+
return task.priority
5861
}
5962

60-
return getJobFlags(task._task).priority
63+
// Otherwise, query the system.
64+
return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .default
6165
}
6266
}
6367

@@ -108,6 +112,28 @@ extension Task {
108112
}
109113
}
110114

115+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
116+
extension Task.Priority {
117+
/// Downgrade user-interactive to user-initiated.
118+
var _downgradeUserInteractive: Task.Priority {
119+
if self == .userInteractive {
120+
return .userInitiated
121+
}
122+
123+
return self
124+
}
125+
126+
/// Adjust the given priority (when it is unspecified) by the current
127+
/// priority Return the current priority that,
128+
var _inheritingContextualPriority: Task.Priority {
129+
if self != .unspecified {
130+
return self
131+
}
132+
133+
return Task.currentPriority._downgradeUserInteractive
134+
}
135+
}
136+
111137
// ==== Task Handle ------------------------------------------------------------
112138

113139
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@@ -478,29 +504,10 @@ public func async(
478504
priority: Task.Priority = .unspecified,
479505
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> Void
480506
) {
481-
// Determine the priority at which we should create this task
482-
let actualPriority: Task.Priority
483-
if priority == .unspecified {
484-
actualPriority = withUnsafeCurrentTask { task in
485-
// If we are running on behalf of a task,
486-
if let task = task {
487-
return task.priority
488-
}
489-
490-
return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .unspecified
491-
}
492-
} else {
493-
actualPriority = priority
494-
}
495-
496-
let adjustedPriority = actualPriority == .userInteractive
497-
? .userInitiated
498-
: actualPriority
499-
500507
// Set up the job flags for a new task.
501508
var flags = Task.JobFlags()
502509
flags.kind = .task
503-
flags.priority = actualPriority
510+
flags.priority = priority._inheritingContextualPriority
504511
flags.isFuture = true
505512

506513
// Create the asynchronous task future.

0 commit comments

Comments
 (0)