Skip to content

Commit 110dff5

Browse files
authored
Merge pull request #34837 from ktoso/wip-task-current-prio
2 parents e066d43 + 261f0d2 commit 110dff5

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

include/swift/ABI/Task.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ class alignas(2 * alignof(void*)) Job {
127127
return Flags.isAsyncTask();
128128
}
129129

130+
JobPriority getPriority() const {
131+
return Flags.getPriority();
132+
}
133+
130134
/// Run this job.
131135
void run(ExecutorRef currentExecutor);
132136
};

include/swift/Runtime/Concurrency.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
173173
bool swift_task_removeStatusRecord(AsyncTask *task,
174174
TaskStatusRecord *record);
175175

176+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
177+
JobPriority
178+
swift_task_getPriority(AsyncTask* task);
179+
176180
/// This should have the same representation as an enum like this:
177181
/// enum NearestTaskDeadline {
178182
/// case none

stdlib/public/Concurrency/Task.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,7 @@ void swift::swift_task_future_wait(
351351
void swift::swift_task_run(AsyncTask *taskToRun) {
352352
taskToRun->run(ExecutorRef::noPreference());
353353
}
354+
355+
JobPriority swift::swift_task_getPriority(AsyncTask *task) {
356+
return task->getPriority();
357+
}

stdlib/public/Concurrency/Task.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ extension Task {
4444
/// This function returns instantly and will never suspend.
4545
/* @instantaneous */
4646
public static func currentPriority() async -> Priority {
47-
fatalError("\(#function) not implemented yet.")
47+
let task = Builtin.getCurrentAsyncTask()
48+
return getPriority(task)
4849
}
4950

5051
/// Task priority may inform decisions an `Executor` makes about how and when
@@ -80,6 +81,7 @@ extension Task {
8081
/// similar to Darwin Dispatch's QoS; bearing in mind that priority is not as
8182
/// much of a thing on other platforms (i.e. server side Linux systems).
8283
public enum Priority: Int, Comparable {
84+
// Values must be same as defined by the internal `JobPriority`.
8385
case userInteractive = 0x21
8486
case userInitiated = 0x19
8587
case `default` = 0x15
@@ -414,6 +416,9 @@ extension Task {
414416
@_silgen_name("swift_task_run")
415417
public func runTask(_ task: __owned Builtin.NativeObject)
416418

419+
@_silgen_name("swift_task_getPriority")
420+
public func getPriority(_ task: __owned Builtin.NativeObject) -> Task.Priority
421+
417422
public func runAsync(_ asyncFun: @escaping () async -> ()) {
418423
let childTask = Builtin.createAsyncTask(0, nil, asyncFun)
419424
runTask(childTask.0)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency) | %FileCheck %s --dump-input always
2+
// REQUIRES: executable_test
3+
// REQUIRES: concurrency
4+
// REQUIRES: OS=macosx
5+
6+
import Dispatch
7+
8+
// ==== ------------------------------------------------------------------------
9+
// MARK: "Infrastructure" for the tests
10+
11+
extension DispatchQueue {
12+
func async<R>(operation: @escaping () async -> R) -> Task.Handle<R> {
13+
let handle = Task.runDetached(operation: operation)
14+
15+
// Run the task
16+
_ = { self.async { handle.run() } }() // force invoking the non-async version
17+
18+
return handle
19+
}
20+
}
21+
22+
// ==== ------------------------------------------------------------------------
23+
// MARK: Tests
24+
25+
func test_getPriority() {
26+
_ = DispatchQueue.main.async { () async in
27+
let p = await Task.currentPriority()
28+
// CHECK: priority: userInteractive
29+
print("priority: \(p)")
30+
assert(p == Task.Priority.userInteractive)
31+
exit(0)
32+
}
33+
}
34+
35+
test_getPriority()
36+
37+
dispatchMain()

0 commit comments

Comments
 (0)