修复macos设置线程亲和性编译错误

This commit is contained in:
Nana 2024-06-19 11:41:37 +08:00
parent a728b69294
commit c304f78e4a

View File

@ -7,6 +7,8 @@
#include <pthread.h>
#elif PLATFORM_MACOS
#include <pthread.h>
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
#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<HANDLE>(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
}