2025-09-26 12:36:56 -04:00
|
|
|
#include "ioprio.hpp"
|
|
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
2025-09-26 12:36:56 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#else
|
2025-11-01 18:04:28 -05:00
|
|
|
#pragma message "ioprio not supported on this platform"
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
2025-09-28 01:01:26 -04:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
IOPRIO_WHO_PROCESS = 1
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-26 12:36:56 -04:00
|
|
|
thread_local int ioprio::SetFrom::thread_prio = -1;
|
|
|
|
|
bool _enabled = false;
|
|
|
|
|
|
|
|
|
|
int
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::get(const int who_)
|
2025-09-26 12:36:56 -04:00
|
|
|
{
|
|
|
|
|
#ifdef SYS_ioprio_get
|
|
|
|
|
int rv;
|
2025-09-29 00:17:51 -04:00
|
|
|
const int which = IOPRIO_WHO_PROCESS;
|
2025-09-26 12:36:56 -04:00
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
rv = syscall(SYS_ioprio_get,which,who_);
|
2025-09-26 12:36:56 -04:00
|
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
|
#else
|
2025-09-29 00:17:51 -04:00
|
|
|
return -EOPNOTSUPP;
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::set(const int who_,
|
2025-09-26 12:36:56 -04:00
|
|
|
const int ioprio_)
|
|
|
|
|
{
|
|
|
|
|
#ifdef SYS_ioprio_set
|
|
|
|
|
int rv;
|
2025-09-29 00:17:51 -04:00
|
|
|
const int which = IOPRIO_WHO_PROCESS;
|
2025-09-26 12:36:56 -04:00
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
rv = syscall(SYS_ioprio_set,which,who_,ioprio_);
|
2025-09-26 12:36:56 -04:00
|
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
|
#else
|
2025-09-29 00:17:51 -04:00
|
|
|
return -EOPNOTSUPP;
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ioprio::enable(const bool enable_)
|
|
|
|
|
{
|
|
|
|
|
_enabled = enable_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
ioprio::enabled()
|
|
|
|
|
{
|
|
|
|
|
return _enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ioprio::SetFrom::SetFrom(const pid_t pid_)
|
|
|
|
|
{
|
|
|
|
|
int client_prio;
|
|
|
|
|
|
|
|
|
|
if(!_enabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
client_prio = ioprio::get(pid_);
|
2025-09-26 12:36:56 -04:00
|
|
|
if(client_prio < 0)
|
|
|
|
|
return;
|
|
|
|
|
if(client_prio == thread_prio)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
thread_prio = client_prio;
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::set(0,client_prio);
|
2025-09-26 12:36:56 -04:00
|
|
|
}
|