From 4812757f1abd728946fefd25261271f5cc06bf6e Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 22 Jan 2021 00:44:43 +0100 Subject: [PATCH] Add a function for getting the current priority --- src/common/utils.cpp | 10 ++++++++++ src/common/utils.h | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 79c35178..b131664a 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -31,6 +31,16 @@ fs::path get_temporary_directory() { } } +std::optional get_scheduling_priority() { + sched_param current_params{}; + if (sched_getparam(0, ¤t_params) == 0 && + current_params.sched_priority > 0) { + return current_params.sched_priority; + } else { + return std::nullopt; + } +} + bool set_realtime_priority(bool sched_fifo) { sched_param params{.sched_priority = 5}; return sched_setscheduler(0, sched_fifo ? SCHED_FIFO : SCHED_OTHER, diff --git a/src/common/utils.h b/src/common/utils.h index 239e30b0..1f89e23f 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -36,6 +36,13 @@ overload(Ts...) -> overload; */ boost::filesystem::path get_temporary_directory(); +/** + * Get the current thread's scheduling priority if the thread is using + * `SCHED_FIFO`. Returns a nullopt of the calling thread is not under realtime + * scheduling. + */ +std::optional get_scheduling_priority(); + /** * Set the scheduling policy to `SCHED_FIFO` with priority 10 for this process. * We explicitly don't do this for wineserver itself since from my testing that