From c304f78e4a8635785c18dc3ea27022cd79bc0a61 Mon Sep 17 00:00:00 2001 From: Nana <469449812@qq.com> Date: Wed, 19 Jun 2024 11:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmacos=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BA=B2=E5=92=8C=E6=80=A7=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/misc/platform_thread.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/core/misc/platform_thread.h b/core/misc/platform_thread.h index 556bac1..d495ab9 100644 --- a/core/misc/platform_thread.h +++ b/core/misc/platform_thread.h @@ -7,6 +7,8 @@ #include #elif PLATFORM_MACOS #include +#include +#include #endif inline void set_thread_name(const char* name) { @@ -23,10 +25,24 @@ inline void set_thread_affinity(std::thread& thread, int core) { #if PLATFORM_WINDOWS const auto handle = reinterpret_cast(thread.native_handle()); SetThreadAffinityMask(handle, 1 << core); -#elif PLATFORM_LINUX || PLATFORM_MACOS +#elif PLATFORM_LINUX cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(core, &cpuset); pthread_setaffinity_np(thread.native_handle(), sizeof(cpu_set_t), &cpuset); +#elif PLATFORM_MACOS + thread_affinity_policy_data_t policy = { core }; + kern_return_t result = thread_policy_set( + pthread_mach_thread_np(thread.native_handle()), + THREAD_AFFINITY_POLICY, + (thread_policy_t)&policy, + THREAD_AFFINITY_POLICY_COUNT + ); + + if (result != KERN_SUCCESS) { + spdlog::error("Failed to set thread affinity: {}", result); + } else { + spdlog::info("Thread bound to core {}", core); + } #endif }