diff options
author | valid-ptr <konstantin.matveyev@eligovision.ru> | 2018-11-26 18:27:26 +0300 |
---|---|---|
committer | Konstantin S. Matveyev <root@zorro.ev> | 2018-11-26 18:28:52 +0300 |
commit | 1e8656f3d9f920b23a5106de2dd6167471ccbdb2 (patch) | |
tree | 1ee9b66b46479e3d515b8fdc5e756e0a26935cee /src | |
parent | 7809a2c08bd3dbea49af2f19a24346372f219b5b (diff) | |
download | lanes-1e8656f3d9f920b23a5106de2dd6167471ccbdb2.tar.gz lanes-1e8656f3d9f920b23a5106de2dd6167471ccbdb2.tar.bz2 lanes-1e8656f3d9f920b23a5106de2dd6167471ccbdb2.zip |
Compilation of 'threading.c' fixed for macos
Diffstat (limited to 'src')
-rw-r--r-- | src/threading.c | 4 | ||||
-rw-r--r-- | src/threading_osx.h | 59 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/threading.c b/src/threading.c index c0e6a55..7b75986 100644 --- a/src/threading.c +++ b/src/threading.c | |||
@@ -58,6 +58,10 @@ THE SOFTWARE. | |||
58 | volatile bool_t sudo; | 58 | volatile bool_t sudo; |
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | #ifdef PLATFORM_OSX | ||
62 | # include "threading_osx.h" | ||
63 | #endif | ||
64 | |||
61 | /* Linux with older glibc (such as Debian) don't have pthread_setname_np, but have prctl | 65 | /* Linux with older glibc (such as Debian) don't have pthread_setname_np, but have prctl |
62 | */ | 66 | */ |
63 | #if defined PLATFORM_LINUX | 67 | #if defined PLATFORM_LINUX |
diff --git a/src/threading_osx.h b/src/threading_osx.h new file mode 100644 index 0000000..93da8c3 --- /dev/null +++ b/src/threading_osx.h | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * THREADING_OSX.H | ||
3 | * http://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html | ||
4 | */ | ||
5 | #ifndef __threading_osx_h__ | ||
6 | #define __threading_osx_h__ 1 | ||
7 | |||
8 | #include <mach/mach_types.h> | ||
9 | #include <mach/thread_act.h> | ||
10 | #include <sys/sysctl.h> | ||
11 | |||
12 | #define SYSCTL_CORE_COUNT "machdep.cpu.core_count" | ||
13 | |||
14 | typedef struct cpu_set { | ||
15 | uint32_t count; | ||
16 | } cpu_set_t; | ||
17 | |||
18 | static inline void CPU_ZERO(cpu_set_t *cs) { cs->count = 0; } | ||
19 | static inline void CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); } | ||
20 | static inline int CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); } | ||
21 | |||
22 | int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set) | ||
23 | { | ||
24 | int32_t core_count = 0; | ||
25 | size_t len = sizeof(core_count); | ||
26 | int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0); | ||
27 | if (ret) | ||
28 | { | ||
29 | // printf("error while get core count %d\n", ret); | ||
30 | return -1; | ||
31 | } | ||
32 | cpu_set->count = 0; | ||
33 | for (int i = 0; i < core_count; i++) | ||
34 | { | ||
35 | cpu_set->count |= (1 << i); | ||
36 | } | ||
37 | |||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | int pthread_setaffinity_np(pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set) | ||
42 | { | ||
43 | thread_port_t mach_thread; | ||
44 | int core = 0; | ||
45 | |||
46 | for (core = 0; core < 8 * cpu_size; core++) | ||
47 | { | ||
48 | if (CPU_ISSET(core, cpu_set)) | ||
49 | break; | ||
50 | } | ||
51 | // printf("binding to core %d\n", core); | ||
52 | thread_affinity_policy_data_t policy = { core }; | ||
53 | mach_thread = pthread_mach_thread_np(thread); | ||
54 | thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1); | ||
55 | |||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | #endif | ||