Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux: fix thread priority #14252

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
20 changes: 20 additions & 0 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ DYNAMIC_IMPORT_RENAME("Kernel32.dll", SetThreadDescriptionImport, "SetThreadDesc
#ifdef __linux__
#include <sys/timerfd.h>
#include <unistd.h>
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif

#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
Expand Down Expand Up @@ -2926,6 +2928,24 @@ void thread_ctrl::set_native_priority(int priority)
{
sig_log.error("SetThreadPriority() failed: %s", fmt::win_error{GetLastError(), nullptr});
}
#elif defined(__linux__)
// available niceness for root: -20~19
id_t threadpid = gettid();
uid_t euid = geteuid();

if (euid == 0)
{
int linuxprio = 0;
if (priority > 0)
linuxprio = -6;
else if (priority < 0)
linuxprio = 6;

if (int err = setpriority(PRIO_PROCESS, threadpid, linuxprio))
{
sig_log.error("setpriority(%d, %d) failed: %d", threadpid, linuxprio, err);
}
}
#else
int policy;
struct sched_param param;
Expand Down