Skip to content

Commit c4bd90f

Browse files
committed
Workaround the actor runtimes unwillingness to deal with priorities properly
rdar://79378627
1 parent cfd6913 commit c4bd90f

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ extension Task where Success == Never, Failure == Never {
257257
}
258258

259259
// Otherwise, query the system.
260-
return TaskPriority(rawValue: UInt8(_getCurrentThreadPriority()))
260+
return TaskPriority(rawValue: UInt8(0))
261261
}
262262
}
263263
}
@@ -266,10 +266,6 @@ extension Task where Success == Never, Failure == Never {
266266
extension TaskPriority {
267267
/// Downgrade user-interactive to user-initiated.
268268
var _downgradeUserInteractive: TaskPriority {
269-
if self == .userInteractive {
270-
return .userInitiated
271-
}
272-
273269
return self
274270
}
275271
}
@@ -519,7 +515,7 @@ extension Task where Failure == Never {
519515
// Set up the job flags for a new task.
520516
var flags = JobFlags()
521517
flags.kind = .task
522-
flags.priority = priority ?? .default
518+
flags.priority = priority ?? .unspecified
523519
flags.isFuture = true
524520

525521
// Create the asynchronous task future.
@@ -574,7 +570,7 @@ extension Task where Failure == Error {
574570
// Set up the job flags for a new task.
575571
var flags = JobFlags()
576572
flags.kind = .task
577-
flags.priority = priority ?? .default
573+
flags.priority = priority ?? .unspecified
578574
flags.isFuture = true
579575

580576
// Create the asynchronous task future.
@@ -694,7 +690,7 @@ public struct UnsafeCurrentTask {
694690
/// - SeeAlso: `TaskPriority`
695691
/// - SeeAlso: `Task.currentPriority`
696692
public var priority: TaskPriority {
697-
getJobFlags(_task).priority ?? .default
693+
getJobFlags(_task).priority ?? .unspecified
698694
}
699695
}
700696

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency) | %FileCheck %s
2+
3+
// REQUIRES: executable_test
4+
// REQUIRES: concurrency
5+
6+
import Foundation
7+
8+
@available(SwiftStdlib 5.5, *)
9+
actor Manager {
10+
static var shared = Manager()
11+
12+
func manage() async -> Int {
13+
print("manage")
14+
return 0
15+
}
16+
17+
func other() async -> Int{
18+
print("other")
19+
return 0
20+
}
21+
}
22+
23+
24+
@available(SwiftStdlib 5.5, *)
25+
func test() {
26+
detach {
27+
let x = await Manager.shared.manage()
28+
print(x)
29+
}
30+
detach {
31+
let x = await Manager.shared.other()
32+
print(x)
33+
}
34+
}
35+
36+
if #available(SwiftStdlib 5.5, *) {
37+
test()
38+
sleep(30)
39+
} else {
40+
print("manage")
41+
print("0")
42+
print("other")
43+
print("0")
44+
}
45+
// CHECK-DAG: manage
46+
// CHECK-DAG: 0
47+
// CHECK-DAG: other
48+
// CHECK-DAG: 0

test/Concurrency/Runtime/async_task_priority_current.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Dispatch
1313
@available(SwiftStdlib 5.5, *)
1414
func test_detach() async {
1515
let a1 = Task.currentPriority
16-
print("a1: \(a1)") // CHECK: TaskPriority(rawValue: 21)
16+
print("a1: \(a1)") // CHECK: TaskPriority(rawValue: 0)
1717

1818
// Note: remember to detach using a higher priority, otherwise a lower one
1919
// might be escalated by the get() and we could see `default` in the detached
@@ -24,7 +24,7 @@ func test_detach() async {
2424
}.get()
2525

2626
let a3 = Task.currentPriority
27-
print("a3: \(a3)") // CHECK: a3: TaskPriority(rawValue: 21)
27+
print("a3: \(a3)") // CHECK: a3: TaskPriority(rawValue: 0)
2828
}
2929

3030
@available(SwiftStdlib 5.5, *)

0 commit comments

Comments
 (0)