@@ -73,13 +73,21 @@ extension Task {
73
73
74
74
/// Returns the `current` task's priority.
75
75
///
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`.
77
79
///
78
80
/// - SeeAlso: `Task.Priority`
79
81
/// - SeeAlso: `Task.priority`
80
82
public static var currentPriority : Priority {
81
83
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
83
91
}
84
92
}
85
93
@@ -141,6 +149,28 @@ extension Task {
141
149
}
142
150
}
143
151
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
+
144
174
// ==== Task Handle ------------------------------------------------------------
145
175
146
176
@available ( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * )
@@ -525,29 +555,10 @@ public func async(
525
555
priority: Task . Priority = . unspecified,
526
556
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping ( ) async -> Void
527
557
) {
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
-
547
558
// Set up the job flags for a new task.
548
559
var flags = Task . JobFlags ( )
549
560
flags. kind = . task
550
- flags. priority = actualPriority
561
+ flags. priority = priority . _inheritingContextualPriority
551
562
flags. isFuture = true
552
563
553
564
// Create the asynchronous task future.
0 commit comments