From 4e4c89613661ee6c000b50092a4a51468dc5e32b Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 20 Jul 2019 15:57:23 +0300 Subject: [PATCH] Expand thread affinity mask to u64 Also fix and note __APPLE__ path. --- Utilities/Thread.cpp | 26 +++++++++++++++++--------- Utilities/Thread.h | 4 ++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index a52d1a7b96..5789086919 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2002,13 +2002,13 @@ void thread_ctrl::detect_cpu_layout() } } -u16 thread_ctrl::get_affinity_mask(thread_class group) +u64 thread_ctrl::get_affinity_mask(thread_class group) { detect_cpu_layout(); if (const auto thread_count = std::thread::hardware_concurrency()) { - const u16 all_cores_mask = thread_count < 16 ? (u16)(~(UINT16_MAX << thread_count)): UINT16_MAX; + const u64 all_cores_mask = thread_count < 64 ? UINT64_MAX >> (64 - thread_count): UINT64_MAX; switch (g_native_core_layout) { @@ -2019,7 +2019,7 @@ u16 thread_ctrl::get_affinity_mask(thread_class group) } case native_core_arrangement::amd_ccx: { - u16 spu_mask, ppu_mask, rsx_mask; + u64 spu_mask, ppu_mask, rsx_mask; if (thread_count >= 16) { // Threadripper, R7 @@ -2077,7 +2077,7 @@ u16 thread_ctrl::get_affinity_mask(thread_class group) } } - return UINT16_MAX; + return UINT64_MAX; } void thread_ctrl::set_native_priority(int priority) @@ -2113,25 +2113,33 @@ void thread_ctrl::set_native_priority(int priority) #endif } -void thread_ctrl::set_thread_affinity_mask(u16 mask) +void thread_ctrl::set_thread_affinity_mask(u64 mask) { #ifdef _WIN32 HANDLE _this_thread = GetCurrentThread(); - SetThreadAffinityMask(_this_thread, (DWORD_PTR)mask); + SetThreadAffinityMask(_this_thread, mask); #elif __APPLE__ - thread_affinity_policy_data_t policy = { static_cast(mask) }; + // Supports only one core + thread_affinity_policy_data_t policy = { static_cast(utils::cnttz64(mask)) }; thread_port_t mach_thread = pthread_mach_thread_np(pthread_self()); thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1); #elif defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) cpu_set_t cs; CPU_ZERO(&cs); - for (u32 core = 0; core < 16u; ++core) + for (u32 core = 0; core < 64u; ++core) { - if ((u32)mask & (1u << core)) + const u64 shifted = mask >> core; + + if (shifted & 1) { CPU_SET(core, &cs); } + + if (shifted <= 1) + { + break; + } } pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cs); diff --git a/Utilities/Thread.h b/Utilities/Thread.h index a7c400e8ef..4dafb8fcfb 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -258,13 +258,13 @@ public: static void detect_cpu_layout(); // Returns a core affinity mask. Set whether to generate the high priority set or not - static u16 get_affinity_mask(thread_class group); + static u64 get_affinity_mask(thread_class group); // Sets the native thread priority static void set_native_priority(int priority); // Sets the preferred affinity mask for this thread - static void set_thread_affinity_mask(u16 mask); + static void set_thread_affinity_mask(u64 mask); // Spawn a detached named thread template