summaryrefslogtreecommitdiff
path: root/lib/thread/ncpu+linux.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-10-29 19:08:13 -0700
committerOri Bernstein <ori@eigenstate.org>2017-10-29 19:08:13 -0700
commit269e4f69372bc1250dcb70a9dfde30486809f1b0 (patch)
treeeb86ec1c691bf8f479be36ede67e029f55f3c09c /lib/thread/ncpu+linux.myr
parent4eea6de57c9fce0a69f85270303baae069c3e237 (diff)
parentd359a98ab9cfc42de66ad35333bd6d6c0571b7ca (diff)
downloadmc-qbe.tar.gz
Merge branch 'master' into qbeqbe
Diffstat (limited to 'lib/thread/ncpu+linux.myr')
-rw-r--r--lib/thread/ncpu+linux.myr30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/thread/ncpu+linux.myr b/lib/thread/ncpu+linux.myr
new file mode 100644
index 0000000..5c2ea15
--- /dev/null
+++ b/lib/thread/ncpu+linux.myr
@@ -0,0 +1,30 @@
+use std
+use sys
+
+pkg thread =
+ const ncpu : (-> int)
+;;
+
+const ncpu = {
+ var cpubuf : uint64[4]
+ var n
+
+ sys.sched_getaffinity(sys.getpid(), sizeof(uint64[4]), (&cpubuf : uint64#))
+ n = 0
+ for b : cpubuf[:]
+ if b != 0
+ n += count(b)
+ ;;
+ ;;
+ -> n
+}
+
+const count = {b
+ var n = 0
+ for var i = 0; i < 8*sizeof(uint64); i++
+ if b & (1<<i) != 0
+ n++
+ ;;
+ ;;
+ -> n
+}