-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaffinity_darwin.c
36 lines (31 loc) · 1.1 KB
/
affinity_darwin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <mach/thread_policy.h>
#include <mach/task_info.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
#include <pthread.h>
#include "affinity.h"
int get_thread_id() {
return pthread_mach_thread_np(pthread_self());
}
int numphyscpus() {
int numcpus;
size_t sizeofint = sizeof(numcpus);
if (-1 == sysctlbyname("hw.physicalcpu", &numcpus, &sizeofint, NULL, 0)) perror("sysctl");
return numcpus;
}
int pthread_create_with_cpu_affinity(pthread_t *restrict thread, int cpu,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void *), void *restrict arg) {
thread_affinity_policy_data_t policy_data = { cpu };
int rv = pthread_create_suspended_np(thread, attr, start_routine, arg);
mach_port_t mach_thread = pthread_mach_thread_np(*thread);
if (rv != 0) {
return rv;
}
thread_policy_set((thread_t)thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy_data, THREAD_AFFINITY_POLICY_COUNT);
thread_resume(mach_thread);
return 0;
}