summary refs log tree commit diff stats
path: root/lib/system
diff options
context:
space:
mode:
authorClyybber <darkmine956@gmail.com>2021-03-30 08:05:37 +0200
committerGitHub <noreply@github.com>2021-03-30 08:05:37 +0200
commita672ec3c9e25159d3482aebfe1d0bb4271910869 (patch)
treece4ecc81c86506d13d21f35fcf4af88bf59ecd25 /lib/system
parent8db93fd0a29dbb950cc14327b31d683c3b0d8926 (diff)
downloadNim-a672ec3c9e25159d3482aebfe1d0bb4271910869.tar.gz
Fix #17299, fix setAffinity for android (#17574)
* Fix #17299

* Comment

* Fix typo
Diffstat (limited to 'lib/system')
-rw-r--r--lib/system/threadlocalstorage.nim19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/system/threadlocalstorage.nim b/lib/system/threadlocalstorage.nim
index 922150fff..cbb74c7df 100644
--- a/lib/system/threadlocalstorage.nim
+++ b/lib/system/threadlocalstorage.nim
@@ -194,8 +194,23 @@ else:
   proc cpusetIncl(cpu: cint; s: var CpuSet) {.
     importc: "CPU_SET", header: schedh.}
 
-  proc setAffinity(thread: SysThread; setsize: csize_t; s: var CpuSet) {.
-    importc: "pthread_setaffinity_np", header: pthreadh.}
+  when defined(android):
+    # libc of android doesn't implement pthread_setaffinity_np,
+    # it exposes pthread_gettid_np though, so we can use that in combination
+    # with sched_setaffinity to set the thread affinity.
+    type Pid {.importc: "pid_t", header: "<sys/types.h>".} = int32 # From posix_other.nim
+
+    proc setAffinityTID(tid: Pid; setsize: csize_t; s: var CpuSet) {.
+      importc: "sched_setaffinity", header: schedh.}
+
+    proc pthread_gettid_np(thread: SysThread): Pid {.
+      importc: "pthread_gettid_np", header: pthreadh.}
+
+    proc setAffinity(thread: SysThread; setsize: csize_t; s: var CpuSet) =
+      setAffinityTID(pthread_gettid_np(thread), setsize, s)
+  else:
+    proc setAffinity(thread: SysThread; setsize: csize_t; s: var CpuSet) {.
+      importc: "pthread_setaffinity_np", header: pthreadh.}
 
 
 const