Linux: fix thread priority

This commit is contained in:
hoholee12 2023-07-23 22:52:23 +09:00
parent 70b124cfa0
commit 8163aaf7de
5 changed files with 35 additions and 12 deletions

View file

@ -3029,19 +3029,26 @@ void thread_ctrl::set_native_priority(int priority)
sig_log.error("SetThreadPriority() failed: %s", fmt::win_error{GetLastError(), nullptr});
}
#else
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, &param);
// available niceness for nonroot: 0~19
int linuxprio = 0;
id_t threadpid = gettid();
if (priority > 0)
param.sched_priority = sched_get_priority_max(policy);
if (priority < 0)
param.sched_priority = sched_get_priority_min(policy);
linuxprio = 0;
else if (priority < 0)
linuxprio = 19;
if (int err = pthread_setschedparam(pthread_self(), policy, &param))
// nonroot cannot increase niceness value
if (getpriority(PRIO_PROCESS, threadpid) < linuxprio)
{
sig_log.error("pthread_setschedparam() failed: %d", err);
if (int err = setpriority(PRIO_PROCESS, threadpid, linuxprio))
{
sig_log.error("setpriority(%d, %d) failed: %d", threadpid, linuxprio, err);
}
else
{
sig_log.success("setpriority(%d, %d) successful.", threadpid, linuxprio);
}
}
#endif
}