From 269d12d1beee357b05311308fc3b473107648089 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 28 Jan 2020 19:39:02 +0100 Subject: [PATCH] Fixed setThreadAffinity() Added 1 ms sleep to guarantee thread rescheduling to the correct CPU core before returning. --- src/base/kernel/Platform_hwloc.cpp | 6 +++++- src/base/kernel/Platform_mac.cpp | 5 ++++- src/base/kernel/Platform_unix.cpp | 8 ++++++-- src/base/kernel/Platform_win.cpp | 4 +++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/base/kernel/Platform_hwloc.cpp b/src/base/kernel/Platform_hwloc.cpp index f4b46ba1e..4e56663b1 100644 --- a/src/base/kernel/Platform_hwloc.cpp +++ b/src/base/kernel/Platform_hwloc.cpp @@ -30,6 +30,7 @@ #include +#include bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) @@ -42,8 +43,11 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) } if (hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD | HWLOC_CPUBIND_STRICT) >= 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); return true; } - return hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD) >= 0; + const bool result = (hwloc_set_cpubind(cpu->topology(), pu->cpuset, HWLOC_CPUBIND_THREAD) >= 0); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + return result; } diff --git a/src/base/kernel/Platform_mac.cpp b/src/base/kernel/Platform_mac.cpp index 2cd3a8314..995dc9516 100644 --- a/src/base/kernel/Platform_mac.cpp +++ b/src/base/kernel/Platform_mac.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "base/kernel/Platform.h" @@ -67,7 +68,9 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) thread_affinity_policy_data_t policy = { static_cast(cpu_id) }; mach_thread = pthread_mach_thread_np(pthread_self()); - return thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS; + const bool result = (thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + return result; } #endif diff --git a/src/base/kernel/Platform_unix.cpp b/src/base/kernel/Platform_unix.cpp index f449995c6..a391f663e 100644 --- a/src/base/kernel/Platform_unix.cpp +++ b/src/base/kernel/Platform_unix.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include "base/kernel/Platform.h" @@ -92,10 +93,13 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) CPU_SET(cpu_id, &mn); # ifndef __ANDROID__ - return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mn) == 0; + const bool result = (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mn) == 0); # else - return sched_setaffinity(gettid(), sizeof(cpu_set_t), &mn) == 0; + const bool result = (sched_setaffinity(gettid(), sizeof(cpu_set_t), &mn) == 0); # endif + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + return result; } #endif diff --git a/src/base/kernel/Platform_win.cpp b/src/base/kernel/Platform_win.cpp index 064c8352f..fc4f1b9fd 100644 --- a/src/base/kernel/Platform_win.cpp +++ b/src/base/kernel/Platform_win.cpp @@ -98,7 +98,9 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) LOG_ERR("Unable to set affinity. Windows supports only affinity up to 63."); } - return SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0; + const bool result = (SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0); + Sleep(1); + return result; } #endif