From 3ed1e2c373ecc4487f0c4ed2e29ddb50681b0111 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 28 Oct 2024 01:22:14 -0400 Subject: [PATCH] Set EngineWorker thread priority on Windows. --- src/engine/Engine.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 26013afd..00b50f0c 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -163,15 +163,23 @@ struct EngineWorker { pthread_attr_t attr; pthread_attr_init(&attr); - // TODO Set this on each OS - // Note this requires root on Linux - int policy = SCHED_RR; + int policy = SCHED_OTHER; +#if defined ARCH_LIN || defined ARCH_WIN + // SCHED_FIFO and SCHED_RR require root or cap_sys_nice on Linux. + // WinPthreads doesn't use this policy setting. +#endif pthread_attr_setschedpolicy(&attr, policy); int minPriority = sched_get_priority_min(policy); int maxPriority = sched_get_priority_max(policy); - // TODO Set this on each OS - int priority = 15; + int priority = 0; +#if defined ARCH_LIN + // Only 0 priority allowed for SCHED_OTHER on Linux. +#elif defined ARCH_WIN + // Should be 15, same priority as RTAUDIO_SCHEDULE_REALTIME on WASAPI. + // Maps to THREAD_PRIORITY_TIME_CRITICAL on Windows. + priority = maxPriority; +#endif priority = std::min(std::max(priority, minPriority), maxPriority); sched_param param; param.sched_priority = priority;