Skip to content

Commit 5f8726b

Browse files
authored
Merge pull request swiftlang#37950 from aschwaighofer/workaround_runtime_bug_5.5
[5.5] Workaround the actor runtimes unwillingness to deal with priorities properly
2 parents 52459b1 + b6fc2fa commit 5f8726b

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

stdlib/public/Concurrency/Task.swift

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

287287
// Otherwise, query the system.
288-
return TaskPriority(rawValue: UInt8(_getCurrentThreadPriority()))
288+
return TaskPriority(rawValue: UInt8(0))
289289
}
290290
}
291291
}
@@ -294,10 +294,6 @@ extension Task where Success == Never, Failure == Never {
294294
extension TaskPriority {
295295
/// Downgrade user-interactive to user-initiated.
296296
var _downgradeUserInteractive: TaskPriority {
297-
if self == .userInteractive {
298-
return .userInitiated
299-
}
300-
301297
return self
302298
}
303299
}
@@ -547,7 +543,7 @@ extension Task where Failure == Never {
547543
// Set up the job flags for a new task.
548544
var flags = JobFlags()
549545
flags.kind = .task
550-
flags.priority = priority ?? .default
546+
flags.priority = priority ?? .unspecified
551547
flags.isFuture = true
552548

553549
// Create the asynchronous task future.
@@ -602,7 +598,7 @@ extension Task where Failure == Error {
602598
// Set up the job flags for a new task.
603599
var flags = JobFlags()
604600
flags.kind = .task
605-
flags.priority = priority ?? .default
601+
flags.priority = priority ?? .unspecified
606602
flags.isFuture = true
607603

608604
// Create the asynchronous task future.
@@ -758,7 +754,7 @@ public struct UnsafeCurrentTask {
758754
/// - SeeAlso: `TaskPriority`
759755
/// - SeeAlso: `Task.currentPriority`
760756
public var priority: TaskPriority {
761-
getJobFlags(_task).priority ?? .default
757+
getJobFlags(_task).priority ?? .unspecified
762758
}
763759
}
764760

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