From fbf4be05b2be31fe0daf37ec00f1f706a8cac91b Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 8 Nov 2022 16:49:46 +0000 Subject: [PATCH] Android: Set thread priority using user-provided value --- modules/juce_core/native/juce_android_Threads.cpp | 14 ++++++++++++-- modules/juce_core/threads/juce_Thread.cpp | 8 ++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/juce_core/native/juce_android_Threads.cpp b/modules/juce_core/native/juce_android_Threads.cpp index e69b3bd9d2..fcd8d9fb0c 100644 --- a/modules/juce_core/native/juce_android_Threads.cpp +++ b/modules/juce_core/native/juce_android_Threads.cpp @@ -354,6 +354,13 @@ pthread_t juce_createRealtimeAudioThread (void* (*entry) (void*), void* userPtr) extern JavaVM* androidJNIJavaVM; +static auto setPriorityOfThisThread (Thread::Priority p) +{ + return setpriority (PRIO_PROCESS, + (id_t) gettid(), + ThreadPriorities::getNativePriority (p)) == 0; +} + bool Thread::createNativeThread (Priority) { if (isRealtime()) @@ -372,6 +379,8 @@ bool Thread::createNativeThread (Priority) { auto* myself = static_cast (userData); + setPriorityOfThisThread (myself->priority); + juce_threadEntryPoint (myself); if (androidJNIJavaVM != nullptr) @@ -404,14 +413,15 @@ Thread::Priority Thread::getPriority() const return ThreadPriorities::getJucePriority (native); } -bool Thread::setPriority (Priority) +bool Thread::setPriority (Priority priorityIn) { jassert (Thread::getCurrentThreadId() == getThreadId()); if (isRealtime()) return false; - return setpriority (PRIO_PROCESS, (id_t) gettid(), ThreadPriorities::getNativePriority (priority)) == 0; + const auto priorityToUse = priority = priorityIn; + return setPriorityOfThisThread (priorityToUse) == 0; } //============================================================================== diff --git a/modules/juce_core/threads/juce_Thread.cpp b/modules/juce_core/threads/juce_Thread.cpp index 2c970bd2b0..5b870311c4 100644 --- a/modules/juce_core/threads/juce_Thread.cpp +++ b/modules/juce_core/threads/juce_Thread.cpp @@ -81,10 +81,6 @@ void Thread::threadEntryPoint() const CurrentThreadHolder::Ptr currentThreadHolder (getCurrentThreadHolder()); currentThreadHolder->value = this; - #if JUCE_ANDROID - setPriority (priority); - #endif - if (threadName.isNotEmpty()) setCurrentThreadName (threadName); @@ -130,10 +126,10 @@ bool Thread::startThreadInternal (Priority threadPriority) shouldExit = false; // 'priority' is essentially useless on Linux as only realtime - // has any options but we need to set this here to satsify + // has any options but we need to set this here to satisfy // later queries, otherwise we get inconsistent results across // platforms. - #if JUCE_LINUX || JUCE_BSD + #if JUCE_ANDROID || JUCE_LINUX || JUCE_BSD priority = threadPriority; #endif