diff --git a/include/system.hpp b/include/system.hpp index f02b99f8..3728aa94 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -16,6 +16,7 @@ void createDirectory(const std::string &path); /** Currently this lies and returns the number of logical cores instead. */ int getPhysicalCoreCount(); void setThreadName(const std::string &name); +void setThreadRealTime(); /** Opens a URL, also happens to work with PDFs and folders. Shell injection is possible, so make sure the URL is trusted or hard coded. diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index ff0d975e..16116136 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -101,6 +101,7 @@ struct EngineWorker { void start() { thread = std::thread([&] { system::setThreadName("Engine worker"); + system::setThreadRealTime(); run(); }); } @@ -298,10 +299,10 @@ static void Engine_step(Engine *engine) { } static void Engine_run(Engine *engine) { -#if defined ARCH_LIN - // Name thread + // Set up thread system::setThreadName("Engine"); -#endif + system::setThreadRealTime(); + // Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode // https://software.intel.com/en-us/node/682949 _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); diff --git a/src/system.cpp b/src/system.cpp index 01961c21..2e50bc25 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -3,9 +3,15 @@ #include #include +#if defined ARCH_LIN + #include + #include +#endif + #if defined ARCH_WIN #include #include + #include #endif @@ -84,8 +90,23 @@ int getPhysicalCoreCount() { } void setThreadName(const std::string &name) { -#if defined ARCH_LIN +#if defined ARCH_LIN || defined ARCH_MAC pthread_setname_np(pthread_self(), name.c_str()); +#elif defined ARCH_WIN + SetThreadDescription(GetCurrentThread(), name.c_str()); +#endif +} + +void setThreadRealTime() { +#if defined ARCH_LIN || defined ARCH_MAC + int policy = SCHED_RR; + struct sched_param param; + param.sched_priority = sched_get_priority_max(policy); + pthread_setschedparam(pthread_self(), policy, ¶m); +#elif defined ARCH_WIN + // Set entire process as realtime + SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); #endif }