2021-08-06 22:11:32 +02:00
|
|
|
#include "thread_priority.h"
|
|
|
|
|
|
|
|
|
|
#include "logger.h"
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-08 00:14:27 +01:00
|
|
|
void setThreadPriority(std::thread &th, thread_priority_t priority)
|
2021-08-06 22:11:32 +02:00
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
2022-12-08 22:42:01 +01:00
|
|
|
if (SetThreadPriority(th.native_handle(), priority) == 0)
|
2021-08-06 22:11:32 +02:00
|
|
|
logger->error("Could not set thread priority!");
|
|
|
|
|
#else
|
|
|
|
|
sched_param sch_params;
|
2023-08-08 15:30:26 +02:00
|
|
|
int policy = 0;
|
|
|
|
|
pthread_getschedparam(th.native_handle(), &policy, &sch_params);
|
2021-08-06 22:11:32 +02:00
|
|
|
sch_params.sched_priority = priority;
|
|
|
|
|
if (pthread_setschedparam(th.native_handle(), SCHED_RR, &sch_params))
|
|
|
|
|
logger->error("Could not set thread priority!");
|
|
|
|
|
#endif
|
2023-08-08 15:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setLowestThreadPriority(std::thread &th)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (SetThreadPriority(th.native_handle(), -2) == 0)
|
|
|
|
|
logger->error("Could not set thread priority!");
|
2023-08-08 16:24:36 +02:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
sched_param sch_params;
|
|
|
|
|
int policy = 0;
|
|
|
|
|
pthread_getschedparam(th.native_handle(), &policy, &sch_params);
|
|
|
|
|
sch_params.sched_priority = PRIORITY_LOWEST;
|
|
|
|
|
if (pthread_setschedparam(th.native_handle(), SCHED_RR, &sch_params))
|
|
|
|
|
logger->error("Could not set thread priority!");
|
2023-08-08 15:30:26 +02:00
|
|
|
#else
|
|
|
|
|
sched_param sch_params;
|
|
|
|
|
int policy = 0;
|
|
|
|
|
pthread_getschedparam(th.native_handle(), &policy, &sch_params);
|
|
|
|
|
if (pthread_setschedparam(th.native_handle(), SCHED_IDLE, &sch_params))
|
|
|
|
|
logger->error("Could not set thread priority!");
|
|
|
|
|
#endif
|
2021-08-06 22:11:32 +02:00
|
|
|
}
|