Skip to content

Commit

Permalink
Thread: Update macOS thread priority calculation
Browse files Browse the repository at this point in the history
The Apple threading documentation [^1] says the following:

> The second argument to pthread_setschedparam is the desired policy,
  which can currently be one of SCHED_FIFO (first in, first out),
  SCHED_RR (round-robin), or SCHED_OTHER. The SCHED_OTHER policy is
  generally used for extra policies that are specific to a given
  operating system, and should thus be avoided when writing portable
  code.

This appears to differ from the policy semantics on Linux and BSD, where
FIFO and RR are both explicitly real-time policies.

Therefore, on Linux/BSD we only enable the RR policy if the requested
priority is 8 or higher. Meanwhile, on macOS, we map all thread
priorities (0 - 10) onto the RR policy with an appropriate priority.

[^1]: https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html
  • Loading branch information
reuk authored and tpoole committed Dec 20, 2021
1 parent 0bac0e7 commit 02b0b75
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 8 additions & 3 deletions modules/juce_core/native/juce_posix_SharedCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,12 @@ void JUCE_CALLTYPE Thread::setCurrentThreadName (const String& name)
bool Thread::setThreadPriority (void* handle, int priority)
{
constexpr auto maxInputPriority = 10;
constexpr auto lowestRealtimePriority = 8;

#if JUCE_LINUX || JUCE_BSD
constexpr auto lowestRrPriority = 8;
#else
constexpr auto lowestRrPriority = 0;
#endif

struct sched_param param;
int policy;
Expand All @@ -971,7 +976,7 @@ bool Thread::setThreadPriority (void* handle, int priority)
if (pthread_getschedparam ((pthread_t) handle, &policy, &param) != 0)
return false;

policy = priority < lowestRealtimePriority ? SCHED_OTHER : SCHED_RR;
policy = priority < lowestRrPriority ? SCHED_OTHER : SCHED_RR;

const auto minPriority = sched_get_priority_min (policy);
const auto maxPriority = sched_get_priority_max (policy);
Expand All @@ -981,7 +986,7 @@ bool Thread::setThreadPriority (void* handle, int priority)
if (policy == SCHED_OTHER)
return 0;

return jmap (priority, lowestRealtimePriority, maxInputPriority, minPriority, maxPriority);
return jmap (priority, lowestRrPriority, maxInputPriority, minPriority, maxPriority);
}();

return pthread_setschedparam ((pthread_t) handle, policy, &param) == 0;
Expand Down
1 change: 0 additions & 1 deletion modules/juce_opengl/opengl/juce_OpenGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class OpenGLContext::CachedImage : public CachedComponentImage,
if (nativeContext != nullptr)
{
renderThread = std::make_unique<ThreadPool> (1);
renderThread->setThreadPriorities (9);
resume();
}
}
Expand Down

0 comments on commit 02b0b75

Please sign in to comment.