[Concurrency] Set thread base priority when running escalated Tasks (#84895)

This commit is contained in:
Bryce Wilson
2025-11-19 16:58:08 -08:00
committed by GitHub
parent 86fb7acc56
commit b30f63530a
8 changed files with 134 additions and 28 deletions

View File

@@ -29,6 +29,13 @@
#include "bitset"
#include "queue" // TODO: remove and replace with our own mpsc
// Does the runtime integrate with libdispatch?
#if defined(SWIFT_CONCURRENCY_USES_DISPATCH)
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH SWIFT_CONCURRENCY_USES_DISPATCH
#else
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH 0
#endif
// Does the runtime provide priority escalation support?
#ifndef SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION
#if SWIFT_CONCURRENCY_ENABLE_DISPATCH && \
@@ -422,7 +429,22 @@ public:
///
/// Generally this should be done immediately after updating
/// ActiveTask.
void flagAsRunning();
///
/// When Dispatch is used for the default executor:
/// * If the return value is non-zero, it must be passed
/// to swift_dispatch_thread_reset_override_self
/// before returning to the executor.
/// * If the return value is zero, it may be ignored or passed to
/// the aforementioned function (which will ignore values of zero).
/// The current implementation will always return zero
/// if you call flagAsRunning again before calling
/// swift_dispatch_thread_reset_override_self with the
/// initial value. This supports suspending and immediately
/// resuming a Task without returning up the callstack.
///
/// For all other default executors, flagAsRunning
/// will return zero which may be ignored.
uint32_t flagAsRunning();
/// Flag that this task is now suspended with information about what it is
/// waiting on.

View File

@@ -38,13 +38,6 @@
#define SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL 0
#endif
// Does the runtime integrate with libdispatch?
#if defined(SWIFT_CONCURRENCY_USES_DISPATCH)
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH SWIFT_CONCURRENCY_USES_DISPATCH
#else
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH 0
#endif
namespace swift {
class DefaultActor;
class TaskOptionRecord;

View File

@@ -48,6 +48,27 @@ swift_dispatch_thread_override_self(qos_class_t override_qos) {
return 0;
}
static inline uint32_t
swift_dispatch_thread_override_self_with_base(qos_class_t override_qos, qos_class_t base_qos) {
if (__builtin_available(macOS 27.0, iOS 27.0, tvOS 27.0, watchOS 27.0, *)) {
return dispatch_thread_override_self_with_base(override_qos, base_qos);
} else if (__builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)) {
// If we don't have the ability to set our base qos correctly, at least set the override
// We want to return 0 here because we have nothing to reset in this case
(void) dispatch_thread_override_self(override_qos);
}
return 0;
}
static inline void
swift_dispatch_thread_reset_override_self(uint32_t opaque) {
if (__builtin_available(macOS 27.0, iOS 27.0, tvOS 27.0, watchOS 27.0, *)) {
dispatch_thread_reset_override_self(opaque);
}
}
static inline int
swift_dispatch_lock_override_start_with_debounce(dispatch_lock_t *lock_addr,
dispatch_tid_t expected_thread, qos_class_t override_to_apply) {