hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 376cf96c81cdb916ef3ab2f7e366fb3e812c0679
parent 20b2772a9a35717e34fb9df399c7f5a31905ff90
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 28 Jan 2023 23:43:57 -0500

os: add cpucount function

Diffstat:
Mos/+freebsd/environ.ha | 13+++++++++++++
Mos/+linux/environ.ha | 18++++++++++++++++++
Mscripts/gen-stdlib | 3++-
Mstdlib.mk | 4++--
4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/os/+freebsd/environ.ha b/os/+freebsd/environ.ha @@ -2,6 +2,7 @@ // (c) 2021 Drew DeVault <sir@cmpwn.com> // (c) 2021 Ember Sawady <ecs@d2evs.net> use bytes; +use errors; use rt; use strings; use types; @@ -123,3 +124,15 @@ export fn machine() const str = { return mach; }; }; + +// Returns the number of usable CPUs. +export fn cpucount() (int | errors::error) = { + let count = 0; + let length = size(int); + match (rt::sysctlbyname("hw.ncpu", &count, &length, null, 0)) { + case void => void; + case let err: rt::errno => + return errors::errno(err); + }; + return count; +}; diff --git a/os/+linux/environ.ha b/os/+linux/environ.ha @@ -2,6 +2,8 @@ // (c) 2021 Drew DeVault <sir@cmpwn.com> // (c) 2021 Ember Sawady <ecs@d2evs.net> use bytes; +use errors; +use math; use rt; use strings; use types; @@ -124,3 +126,19 @@ export fn machine() const str = { }; return strings::fromc(&uts.machine: *const char)!; }; + +// Returns the number of usable CPUs. +export fn cpucount() (int | errors::error) = { + let set = rt::cpu_set { ... }; + match (rt::sched_getaffinity(rt::getpid(), size(rt::cpu_set), &set)) { + case void => void; + case let err: rt::errno => + return errors::errno(err); + }; + + let ret = 0; + for (let i = 0z; i < len(set.__bits); i += 1) { + ret += math::pop_count_u64(set.__bits[i]): int; + }; + return ret; +}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1171,7 +1171,8 @@ os() { +linux/memory.ha \ +linux/stdfd.ha \ os.ha - gen_ssa -plinux os io strings types fs encoding::utf8 bytes bufio errors + gen_ssa -plinux os io strings types fs encoding::utf8 bytes bufio \ + errors math gen_srcs -pfreebsd os \ +freebsd/environ.ha \ diff --git a/stdlib.mk b/stdlib.mk @@ -1786,7 +1786,7 @@ stdlib_os_linux_srcs = \ $(STDLIB)/os/+linux/stdfd.ha \ $(STDLIB)/os/os.ha -$(HARECACHE)/os/os-linux.ssa: $(stdlib_os_linux_srcs) $(stdlib_rt) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_types_$(PLATFORM)) $(stdlib_fs_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) $(stdlib_bytes_$(PLATFORM)) $(stdlib_bufio_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) +$(HARECACHE)/os/os-linux.ssa: $(stdlib_os_linux_srcs) $(stdlib_rt) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_types_$(PLATFORM)) $(stdlib_fs_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) $(stdlib_bytes_$(PLATFORM)) $(stdlib_bufio_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) $(stdlib_math_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/os @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nos \ @@ -4063,7 +4063,7 @@ testlib_os_linux_srcs = \ $(STDLIB)/os/+linux/stdfd.ha \ $(STDLIB)/os/os.ha -$(TESTCACHE)/os/os-linux.ssa: $(testlib_os_linux_srcs) $(testlib_rt) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_types_$(PLATFORM)) $(testlib_fs_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) $(testlib_bytes_$(PLATFORM)) $(testlib_bufio_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) +$(TESTCACHE)/os/os-linux.ssa: $(testlib_os_linux_srcs) $(testlib_rt) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_types_$(PLATFORM)) $(testlib_fs_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) $(testlib_bytes_$(PLATFORM)) $(testlib_bufio_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) $(testlib_math_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/os @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nos \