Skip to content

Commit cf7176f

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`. (cherry picked from commit 67ed437)
1 parent 111b78e commit cf7176f

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,21 @@ extension Task {
7373

7474
/// Returns the `current` task's priority.
7575
///
76-
/// If no current `Task` is available, returns `Priority.default`.
76+
/// If no current `Task` is available, queries the system to determine the
77+
/// priority at which the current function is running. If the system cannot
78+
/// provide an appropriate priority, returns `Priority.default`.
7779
///
7880
/// - SeeAlso: `Task.Priority`
7981
/// - SeeAlso: `Task.priority`
8082
public static var currentPriority: Priority {
8183
withUnsafeCurrentTask { task in
82-
task?.priority ?? Priority.default
84+
// If we are running on behalf of a task, use that task's priority.
85+
if let task = task {
86+
return task.priority
87+
}
88+
89+
// Otherwise, query the system.
90+
return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .default
8391
}
8492
}
8593

@@ -141,6 +149,28 @@ extension Task {
141149
}
142150
}
143151

152+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
153+
extension Task.Priority {
154+
/// Downgrade user-interactive to user-initiated.
155+
var _downgradeUserInteractive: Task.Priority {
156+
if self == .userInteractive {
157+
return .userInitiated
158+
}
159+
160+
return self
161+
}
162+
163+
/// Adjust the given priority (when it is unspecified) by the current
164+
/// priority Return the current priority that,
165+
var _inheritingContextualPriority: Task.Priority {
166+
if self != .unspecified {
167+
return self
168+
}
169+
170+
return Task.currentPriority._downgradeUserInteractive
171+
}
172+
}
173+
144174
// ==== Task Handle ------------------------------------------------------------
145175

146176
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@@ -525,29 +555,10 @@ public func async(
525555
priority: Task.Priority = .unspecified,
526556
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> Void
527557
) {
528-
// Determine the priority at which we should create this task
529-
let actualPriority: Task.Priority
530-
if priority == .unspecified {
531-
actualPriority = withUnsafeCurrentTask { task in
532-
// If we are running on behalf of a task,
533-
if let task = task {
534-
return task.priority
535-
}
536-
537-
return Task.Priority(rawValue: _getCurrentThreadPriority()) ?? .unspecified
538-
}
539-
} else {
540-
actualPriority = priority
541-
}
542-
543-
let adjustedPriority = actualPriority == .userInteractive
544-
? .userInitiated
545-
: actualPriority
546-
547558
// Set up the job flags for a new task.
548559
var flags = Task.JobFlags()
549560
flags.kind = .task
550-
flags.priority = actualPriority
561+
flags.priority = priority._inheritingContextualPriority
551562
flags.isFuture = true
552563

553564
// Create the asynchronous task future.

0 commit comments

Comments
 (0)