summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@mmarkovcorp.com>2017-09-20 12:00:37 -0700
committerOri Bernstein <ori@mmarkovcorp.com>2017-09-20 12:00:37 -0700
commit3a6ce7b6c6f83632d6083ffa37a59c88e49fe737 (patch)
tree1afbcd148e9dab23cc449d6e9cedcc31f8815a13
parented2494d9fffdfb78d0658e8d3183874e04c8aced (diff)
downloadmc-3a6ce7b6c6f83632d6083ffa37a59c88e49fe737.tar.gz
Add Linux CPU count function.
Linux users deserve fast builds too.
-rw-r--r--lib/thread/bld.sub13
-rw-r--r--lib/thread/ncpu+linux.myr30
2 files changed, 37 insertions, 6 deletions
diff --git a/lib/thread/bld.sub b/lib/thread/bld.sub
index 02e5e50..c5df621 100644
--- a/lib/thread/bld.sub
+++ b/lib/thread/bld.sub
@@ -8,16 +8,17 @@ lib thread =
# linux impl of basic thread primitives
#condvar+linux.myr
+ exit+linux-x64.s
mutex+linux.myr
+ ncpu+linux.myr
spawn+linux.myr
- exit+linux-x64.s
# freebsd impl of thread primitives
#condvar+freebsd.myr
+ exit+freebsd-x64.s
mutex+freebsd.myr
- spawn+freebsd.myr
ncpu+freebsd.myr
- exit+freebsd-x64.s
+ spawn+freebsd.myr
# netbsd impl of thread primitives
#condvar+netbsd.myr
@@ -33,14 +34,14 @@ lib thread =
# 9front impl of thread primitives
#condvar+plan9.myr
+ atomic-impl+plan9-x64.s
mutex+plan9.myr
- spawn+plan9.myr
ncpu+plan9.myr
- atomic-impl+plan9-x64.s
+ spawn+plan9.myr
# openbsd impl of thread primitives
- spawn+openbsd.myr
exit+openbsd-x64.s
+ spawn+openbsd.myr
atomic-impl+x64.s
atomic.myr
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
+}