Skip to content

Conversation

@Bryce-MW
Copy link
Contributor

When using Dispatch as the default global executor, the threads that Tasks are run on have base priorities corresponding to the current priority of the Task at the time of enqueue. Some APIs outside of Swift look at the base priority of the thread (e.g qos_class_self). The thread's base priority may be higher than the base priority of the Task. Swift Concurrency APIs that use the Task's base priority do not have this issue.

This change informs Dispatch of the Task's base priority before running the Task giving it a chance to update the base priority of the thread. Dispatch returns a value that it requires us to give back after execution. I assert in cases where we do not expect to need to change the priority that a zero value is returned (since this indicates no override was done).

In addition, it seems that SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION wasn't being enabled because it checks SWIFT_CONCURRENCY_ENABLE_DISPATCH which isn't set until later (in Concurrency.h which includes Task.h before the definition).

rdar://88155873

@Bryce-MW Bryce-MW force-pushed the bryce-update-base-priority branch from e2344fb to a4bdd3d Compare November 4, 2025 21:24
@ktoso
Copy link
Contributor

ktoso commented Nov 5, 2025

@swift-ci please smoke test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small requests, but looks good logic wise, thanks @Bryce-MW !

@ktoso
Copy link
Contributor

ktoso commented Nov 5, 2025

@swift-ci please smoke test

@Bryce-MW Bryce-MW force-pushed the bryce-update-base-priority branch from a4bdd3d to 0d8812b Compare November 15, 2025 01:30
@Bryce-MW
Copy link
Contributor Author

@swift-ci please smoke test

@ktoso
Copy link
Contributor

ktoso commented Nov 15, 2025

Sadly a real error: /home/build-user/swift/stdlib/public/Concurrency/Task.cpp:157:7: error: use of undeclared identifier 'contextInitialized'

@Bryce-MW Bryce-MW force-pushed the bryce-update-base-priority branch from 0d8812b to 90456a2 Compare November 16, 2025 23:12
@Bryce-MW
Copy link
Contributor Author

Should build and the test succeeds in my testing. I will test the runtime further but at least this fixes the issue that I was trying to fix.

@Bryce-MW
Copy link
Contributor Author

@swift-ci please smoke test

@ktoso
Copy link
Contributor

ktoso commented Nov 17, 2025

swiftlang/swift-docc#1351

@swift-ci please smoke test Windows

@ktoso
Copy link
Contributor

ktoso commented Nov 17, 2025

docc seems to have broken windows CI, attempting to test the fix with this build ^

@Bryce-MW
Copy link
Contributor Author

My local testing hasn't found any issues so far

@Bryce-MW
Copy link
Contributor Author

@swift-ci please smoke test Windows

qosClass: DispatchQoS.QoSClass(rawValue: qos_class_self())!,
relativePriority: 0)
expectEqual(initialQos, DispatchQoS.utility)
let childTask = Task {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nitpick, that's not a "child" task, we can fix in followup tho

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, thank you!

@ktoso ktoso merged commit b30f635 into swiftlang:main Nov 20, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants