#pragma once
#include <optional>
#include <pthread.h>
#include <thread>
#include <utility>
namespace ableton
{
namespace platforms
{
namespace linux_
{
struct ThreadPriority
{
void setHigh()
{
if (!moOriginal)
{
capture();
}
sched_param high{};
high.sched_priority = 35;
pthread_setschedparam(pthread_self(), SCHED_FIFO, &high);
}
void reset()
{
if (moOriginal)
{
pthread_setschedparam(pthread_self(), moOriginal->first, &moOriginal->second);
moOriginal = std::nullopt;
}
}
private:
void capture()
{
int policy;
sched_param params;
const auto result = pthread_getschedparam(pthread_self(), &policy, ¶ms);
if (result == 0)
{
moOriginal = std::make_pair(policy, params);
}
}
std::optional<std::pair<int, sched_param>> moOriginal;
};
struct ThreadFactory
{
template <typename Callable, typename... Args>
static std::thread makeThread(std::string name, Callable&& f, Args&&... args)
{
auto thread = std::thread(std::forward<Callable>(f), std::forward<Args>(args)...);
pthread_setname_np(thread.native_handle(), name.c_str());
return thread;
}
};
} } }