Skip to content

Commit 802f33b

Browse files
committed
Thread: Avoid setting realtime priority on Thread instances by default on POSIX systems
1 parent 1de7622 commit 802f33b

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

BREAKING-CHANGES.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ JUCE breaking changes
44
Develop
55
=======
66

7+
Change
8+
------
9+
Thread::setPriority() will no longer set a realtime scheduling policy for all
10+
threads with non-zero priorities on POSIX systems.
11+
12+
Possible Issues
13+
---------------
14+
Threads that implicitly relied on using a realtime policy will no longer
15+
request a realtime policy if their priority is 7 or lower.
16+
17+
Workaround
18+
----------
19+
For threads that require a realtime policy on POSIX systems, request a priority
20+
of 8 or higher by calling Thread::setPriority() or
21+
Thread::setCurrentThreadPriority().
22+
23+
Rationale
24+
---------
25+
By default, new Thread instances have a priority of 5. Previously, non-zero
26+
priorities corresponded to realtime scheduling policies, meaning that new
27+
Threads would use the realtime scheduling policy unless they explicitly
28+
requested a priority of 0. However, most threads do not and should not require
29+
realtime scheduling. Setting a realtime policy on all newly-created threads may
30+
degrade performance, as multiple realtime threads will end up fighting for
31+
limited resources.
32+
33+
734
Change
835
------
936
The JUCE_GLSL_VERSION preprocessor definition has been removed.

modules/juce_core/native/juce_posix_SharedCode.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,22 +953,31 @@ void JUCE_CALLTYPE Thread::setCurrentThreadName (const String& name)
953953

954954
bool Thread::setThreadPriority (void* handle, int priority)
955955
{
956+
constexpr auto maxInputPriority = 10;
957+
constexpr auto lowestRealtimePriority = 8;
958+
956959
struct sched_param param;
957960
int policy;
958-
priority = jlimit (0, 10, priority);
959961

960962
if (handle == nullptr)
961963
handle = (void*) pthread_self();
962964

963965
if (pthread_getschedparam ((pthread_t) handle, &policy, &param) != 0)
964966
return false;
965967

966-
policy = priority == 0 ? SCHED_OTHER : SCHED_RR;
968+
policy = priority < lowestRealtimePriority ? SCHED_OTHER : SCHED_RR;
969+
970+
const auto minPriority = sched_get_priority_min (policy);
971+
const auto maxPriority = sched_get_priority_max (policy);
972+
973+
param.sched_priority = [&]
974+
{
975+
if (policy == SCHED_OTHER)
976+
return 0;
967977

968-
const int minPriority = sched_get_priority_min (policy);
969-
const int maxPriority = sched_get_priority_max (policy);
978+
return jmap (priority, lowestRealtimePriority, maxInputPriority, minPriority, maxPriority);
979+
}();
970980

971-
param.sched_priority = ((maxPriority - minPriority) * priority) / 10 + minPriority;
972981
return pthread_setschedparam ((pthread_t) handle, policy, &param) == 0;
973982
}
974983

0 commit comments

Comments
 (0)