From 56fc869a48a033c06b327a220df9437229201683 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 7 Jan 2018 19:10:35 +0100 Subject: [PATCH] Set realtime priority for bridge audio threads --- source/backend/engine/CarlaEngineBridge.cpp | 3 +- source/libjack/libjack.cpp | 5 +- source/utils/CarlaThread.hpp | 55 +++++++++++++++------ 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/source/backend/engine/CarlaEngineBridge.cpp b/source/backend/engine/CarlaEngineBridge.cpp index 70a5daa66..cbcb2ab53 100644 --- a/source/backend/engine/CarlaEngineBridge.cpp +++ b/source/backend/engine/CarlaEngineBridge.cpp @@ -215,8 +215,7 @@ public: fShmNonRtServerControl.commitWrite(); } - // TODO - startThread(/*Thread::realtimeAudioPriority*/); + startThread(true); return true; } diff --git a/source/libjack/libjack.cpp b/source/libjack/libjack.cpp index ff01e4923..c68e1c5de 100644 --- a/source/libjack/libjack.cpp +++ b/source/libjack/libjack.cpp @@ -168,7 +168,7 @@ public: jack_carla_interposed_action(1, fSetupHints, (void*)carla_interposed_callback); jack_carla_interposed_action(2, fSessionManager, nullptr); - fNonRealtimeThread.startThread(); + fNonRealtimeThread.startThread(false); } ~CarlaJackAppClient() noexcept override @@ -1024,8 +1024,7 @@ void CarlaJackAppClient::runNonRealtimeThread() fMidiOutBuffers[i].isInput = false; } - // TODO - fRealtimeThread.startThread(/*Thread::realtimeAudioPriority*/); + fRealtimeThread.startThread(true); fLastPingTime = getCurrentTimeMilliseconds(); carla_stdout("Carla Jack Client Ready!"); diff --git a/source/utils/CarlaThread.hpp b/source/utils/CarlaThread.hpp index 137362ef5..b17c5d5f7 100644 --- a/source/utils/CarlaThread.hpp +++ b/source/utils/CarlaThread.hpp @@ -86,33 +86,60 @@ public: /* * Start the thread. */ - bool startThread() noexcept + bool startThread(const bool withRealtimePriority = false) noexcept { // check if already running CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true); + pthread_t handle; + + pthread_attr_t attr; + pthread_attr_init(&attr); + + struct sched_param sched_param; + carla_zeroStruct(sched_param); + + if (withRealtimePriority) + { + sched_param.sched_priority = 80; + + if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0 && + pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0 && +#ifndef CARLA_OS_WIN + (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) == 0 || + pthread_attr_setschedpolicy(&attr, SCHED_RR) == 0) && +#endif + pthread_attr_setschedparam(&attr, &sched_param) == 0) + { + carla_stdout("CarlaThread with realtime priority successful"); + } + else + { + carla_stdout("CarlaThread with realtime priority failed, going with normal priority instead"); + pthread_attr_destroy(&attr); + pthread_attr_init(&attr); + } + } + const CarlaMutexLocker cml(fLock); fShouldExit = false; - pthread_t handle; + const bool ok = pthread_create(&handle, &attr, _entryPoint, this) == 0; + pthread_attr_destroy(&attr); - if (pthread_create(&handle, nullptr, _entryPoint, this) == 0) - { + CARLA_SAFE_ASSERT_RETURN(ok, false); #ifdef PTW32_DLLPORT - CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false); + CARLA_SAFE_ASSERT_RETURN(handle.p != nullptr, false); #else - CARLA_SAFE_ASSERT_RETURN(handle != 0, false); + CARLA_SAFE_ASSERT_RETURN(handle != 0, false); #endif - pthread_detach(handle); - _copyFrom(handle); - - // wait for thread to start - fSignal.wait(); - return true; - } + pthread_detach(handle); + _copyFrom(handle); - return false; + // wait for thread to start + fSignal.wait(); + return true; } /*