diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index e56b396cc8..1ae49032b2 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2399,6 +2399,30 @@ void thread_ctrl::set_native_priority(int priority) #endif } +u64 thread_ctrl::get_process_affinity_mask() +{ + static const u64 mask = []() -> u64 + { +#ifdef _WIN32 + DWORD_PTR res, _sys; + if (!GetProcessAffinityMask(GetCurrentProcess(), &res, &_sys)) + { + sig_log.error("Failed to get process affinity mask."); + return 0; + } + + return res; +#else + // Assume it's called from the main thread (this is a bit shaky) + return thread_ctrl::get_thread_affinity_mask(); +#endif + }(); + + return mask; +} + +DECLARE(thread_ctrl::process_affinity_mask) = get_process_affinity_mask(); + void thread_ctrl::set_thread_affinity_mask(u64 mask) { #ifdef _WIN32 @@ -2441,12 +2465,7 @@ void thread_ctrl::set_thread_affinity_mask(u64 mask) u64 thread_ctrl::get_thread_affinity_mask() { #ifdef _WIN32 - DWORD_PTR res, _sys; - if (!GetProcessAffinityMask(GetCurrentProcess(), &res, &_sys)) - { - sig_log.error("Failed to get process affinity mask."); - return 0; - } + const u64 res = get_process_affinity_mask(); if (DWORD_PTR result = SetThreadAffinityMask(GetCurrentThread(), res)) { diff --git a/Utilities/Thread.h b/Utilities/Thread.h index 8e69ca0ad1..4525097c11 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -249,8 +249,15 @@ public: // Sets the preferred affinity mask for this thread static void set_thread_affinity_mask(u64 mask); + // Get process affinity mask + static u64 get_process_affinity_mask(); + // Miscellaneous static u64 get_thread_affinity_mask(); + +private: + // Miscellaneous + static const u64 process_affinity_mask; }; // Derived from the callable object Context, possibly a lambda