Implement process and thread priority adjustments (#691)

This commit is contained in:
Cameron Gutman
2023-01-03 21:05:00 -06:00
committed by GitHub
parent 997e8c6e5a
commit cc688c7845
8 changed files with 128 additions and 2 deletions

View File

@@ -306,6 +306,18 @@ std::vector<std::string> display_names(mem_type_e hwdevice_type);
boost::process::child run_unprivileged(const std::string &cmd, boost::filesystem::path &working_dir, boost::process::environment &env, FILE *file, std::error_code &ec);
enum class thread_priority_e : int {
low,
normal,
high,
critical
};
void adjust_thread_priority(thread_priority_e priority);
// Allow OS-specific actions to be taken to prepare for streaming
void streaming_will_start();
void streaming_will_stop();
input_t input();
void move_mouse(input_t &input, int deltaX, int deltaY);
void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y);

View File

@@ -153,6 +153,18 @@ bp::child run_unprivileged(const std::string &cmd, boost::filesystem::path &work
}
}
void adjust_thread_priority(thread_priority_e priority) {
// Unimplemented
}
void streaming_will_start() {
// Nothing to do
}
void streaming_will_stop() {
// Nothing to do
}
namespace source {
enum source_e : std::size_t {
#ifdef SUNSHINE_BUILD_CUDA

View File

@@ -131,6 +131,18 @@ bp::child run_unprivileged(const std::string &cmd, boost::filesystem::path &work
}
}
void adjust_thread_priority(thread_priority_e priority) {
// Unimplemented
}
void streaming_will_start() {
// Nothing to do
}
void streaming_will_stop() {
// Nothing to do
}
} // namespace platf
namespace dyn {

View File

@@ -13,9 +13,12 @@
#include <winuser.h>
#include <ws2tcpip.h>
#include <userenv.h>
#include <dwmapi.h>
#include <timeapi.h>
// clang-format on
#include "src/main.h"
#include "src/platform/common.h"
#include "src/utility.h"
namespace bp = boost::process;
@@ -470,4 +473,53 @@ bp::child run_unprivileged(const std::string &cmd, boost::filesystem::path &work
}
}
void adjust_thread_priority(thread_priority_e priority) {
int win32_priority;
switch(priority) {
case thread_priority_e::low:
win32_priority = THREAD_PRIORITY_BELOW_NORMAL;
break;
case thread_priority_e::normal:
win32_priority = THREAD_PRIORITY_NORMAL;
break;
case thread_priority_e::high:
win32_priority = THREAD_PRIORITY_ABOVE_NORMAL;
break;
case thread_priority_e::critical:
win32_priority = THREAD_PRIORITY_HIGHEST;
break;
default:
BOOST_LOG(error) << "Unknown thread priority: "sv << (int)priority;
return;
}
if(!SetThreadPriority(GetCurrentThread(), win32_priority)) {
auto winerr = GetLastError();
BOOST_LOG(warning) << "Unable to set thread priority to "sv << win32_priority << ": "sv << winerr;
}
}
void streaming_will_start() {
// Enable MMCSS scheduling for DWM
DwmEnableMMCSS(true);
// Reduce timer period to 1ms
timeBeginPeriod(1);
// Promote ourselves to high priority class
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
}
void streaming_will_stop() {
// Demote ourselves back to normal priority class
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
// End our 1ms timer request
timeEndPeriod(1);
// Disable MMCSS scheduling for DWM
DwmEnableMMCSS(false);
}
} // namespace platf