in runtime/native/src/thread_pool/thread_pool.h [74:123]
inline void SetAffinity() {
#ifdef _WIN32
/* Windows */
SetThreadAffinityMask(GetCurrentThread(), 0x1);
for (int i = 0; i < num_worker_; ++i) {
const int core_id = i + 1;
SetThreadAffinityMask(thread_[i].native_handle(), (1 << core_id));
}
#elif defined(__APPLE__) && defined(__MACH__)
#include <TargetConditionals.h>
#if TARGET_OS_MAC == 1
/* Mac OSX */
thread_port_t mach_thread = pthread_mach_thread_np(pthread_self());
thread_affinity_policy_data_t policy = {0};
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
for (int i = 0; i < num_worker_; ++i) {
const int core_id = i + 1;
mach_thread = pthread_mach_thread_np(thread_[i].native_handle());
policy = {core_id};
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
}
#else
#error "iPhone not supported yet"
#endif
#else
/* Linux and others */
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
#if defined(__ANDROID__)
sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &cpuset);
#else
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
#endif
for (int i = 0; i < num_worker_; ++i) {
const int core_id = i + 1;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
#if defined(__ANDROID__)
sched_setaffinity(thread_[i].native_handle(),
sizeof(cpu_set_t), &cpuset);
#else
pthread_setaffinity_np(thread_[i].native_handle(),
sizeof(cpu_set_t), &cpuset);
#endif
}
#endif
}