@@ -47,17 +47,21 @@ extension Task {
47
47
48
48
/// Returns the `current` task's priority.
49
49
///
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`.
51
53
///
52
54
/// - SeeAlso: `Task.Priority`
53
55
/// - SeeAlso: `Task.priority`
54
56
public static var currentPriority : Priority {
55
57
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
58
61
}
59
62
60
- return getJobFlags ( task. _task) . priority
63
+ // Otherwise, query the system.
64
+ return Task . Priority ( rawValue: _getCurrentThreadPriority ( ) ) ?? . default
61
65
}
62
66
}
63
67
@@ -108,6 +112,28 @@ extension Task {
108
112
}
109
113
}
110
114
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
+
111
137
// ==== Task Handle ------------------------------------------------------------
112
138
113
139
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
@@ -478,29 +504,10 @@ public func async(
478
504
priority: Task . Priority = . unspecified,
479
505
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping ( ) async -> Void
480
506
) {
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
-
500
507
// Set up the job flags for a new task.
501
508
var flags = Task . JobFlags ( )
502
509
flags. kind = . task
503
- flags. priority = actualPriority
510
+ flags. priority = priority . _inheritingContextualPriority
504
511
flags. isFuture = true
505
512
506
513
// Create the asynchronous task future.
0 commit comments