diff --git a/modules/juce_core/threads/juce_Thread.cpp b/modules/juce_core/threads/juce_Thread.cpp index c31f56cb55..e0f8e7dc12 100644 --- a/modules/juce_core/threads/juce_Thread.cpp +++ b/modules/juce_core/threads/juce_Thread.cpp @@ -139,16 +139,11 @@ void Thread::startThread (int priority) if (threadHandle.get() == nullptr) { - auto isRealtime = (priority == realtimeAudioPriority); - #if JUCE_ANDROID - isAndroidRealtimeThread = isRealtime; + isAndroidRealtimeThread = (priority == realtimeAudioPriority); #endif - if (isRealtime) - priority = 9; - - threadPriority = priority; + threadPriority = getAdjustedPriority (priority); startThread(); } else @@ -257,10 +252,7 @@ void Thread::removeListener (Listener* listener) //============================================================================== bool Thread::setPriority (int newPriority) { - bool isRealtime = (newPriority == realtimeAudioPriority); - - if (isRealtime) - newPriority = 9; + newPriority = getAdjustedPriority (newPriority); // NB: deadlock possible if you try to set the thread prio from the thread itself, // so using setCurrentThreadPriority instead in that case. @@ -270,6 +262,8 @@ bool Thread::setPriority (int newPriority) const ScopedLock sl (startStopLock); #if JUCE_ANDROID + bool isRealtime = (newPriority == realtimeAudioPriority); + // you cannot switch from or to an Android realtime thread once the // thread is already running! jassert (isThreadRunning() && (isRealtime == isAndroidRealtimeThread)); @@ -288,7 +282,7 @@ bool Thread::setPriority (int newPriority) bool Thread::setCurrentThreadPriority (const int newPriority) { - return setThreadPriority ({}, newPriority); + return setThreadPriority ({}, getAdjustedPriority (newPriority)); } void Thread::setAffinityMask (const uint32 newAffinityMask) @@ -296,6 +290,11 @@ void Thread::setAffinityMask (const uint32 newAffinityMask) affinityMask = newAffinityMask; } +int Thread::getAdjustedPriority (int newPriority) +{ + return jlimit (0, 10, newPriority == realtimeAudioPriority ? 9 : newPriority); +} + //============================================================================== bool Thread::wait (const int timeOutMilliseconds) const { diff --git a/modules/juce_core/threads/juce_Thread.h b/modules/juce_core/threads/juce_Thread.h index 07776ed6e4..1f39e1e519 100644 --- a/modules/juce_core/threads/juce_Thread.h +++ b/modules/juce_core/threads/juce_Thread.h @@ -407,6 +407,7 @@ private: void killThread(); void threadEntryPoint(); static bool setThreadPriority (void*, int); + static int getAdjustedPriority (int); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Thread) };