ref: 3a6ce7b6c6f83632d6083ffa37a59c88e49fe737
parent: ed2494d9fffdfb78d0658e8d3183874e04c8aced
author: Ori Bernstein <[email protected]>
date: Wed Sep 20 08:00:37 EDT 2017
Add Linux CPU count function. Linux users deserve fast builds too.
--- a/lib/thread/bld.sub
+++ b/lib/thread/bld.sub
@@ -8,16 +8,17 @@
# 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 @@
# 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
--- /dev/null
+++ b/lib/thread/ncpu+linux.myr
@@ -1,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
+}