diff options
-rw-r--r-- | lib/thread/bld.sub | 13 | ||||
-rw-r--r-- | lib/thread/ncpu+linux.myr | 30 |
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 +} |