Skip to content

Commit

Permalink
SetThreadAffinity support macos
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Jan 3, 2025
1 parent 9255490 commit ede4505
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions myframe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Author: 李柯鹏 <[email protected]>
#include <Windows.h>
#elif defined(MYFRAME_OS_MACOSX)
#include <mach-o/dyld.h>
#include <mach/mach.h>
#else
#error "Platform not supported"
#endif
Expand Down Expand Up @@ -135,6 +136,20 @@ int Common::SetThreadAffinity(std::thread* t, int cpu_core) {
return -1;
}
return 0;
#elif defined(MYFRAME_OS_MACOSX)
thread_affinity_policy policy;
policy.affinity_tag = cpu_core;
auto handle = pthread_mach_thread_np(t->native_handle());
kern_return_t kr = thread_policy_set(
handle,
THREAD_AFFINITY_POLICY,
reinterpret_cast<thread_policy_t>(&policy),
THREAD_AFFINITY_POLICY_COUNT);
mach_port_deallocate(mach_task_self(), handle);
if (kr != KERN_SUCCESS) {
return -1;
}
return 0;
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
Expand All @@ -153,6 +168,20 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
return -1;
}
return 0;
#elif defined(MYFRAME_OS_MACOSX)
thread_affinity_policy policy;
policy.affinity_tag = cpu_core;
auto handle = mach_thread_self();
kern_return_t kr = thread_policy_set(
handle,
THREAD_AFFINITY_POLICY,
reinterpret_cast<thread_policy_t>(&policy),
THREAD_AFFINITY_POLICY_COUNT);
mach_port_deallocate(mach_task_self(), handle);
if (kr != KERN_SUCCESS) {
return -1;
}
return 0;
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
Expand Down

0 comments on commit ede4505

Please sign in to comment.