aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/old/vlib/runtime')
-rw-r--r--v_windows/v/old/vlib/runtime/runtime.v56
-rw-r--r--v_windows/v/old/vlib/runtime/runtime_nix.c.v11
-rw-r--r--v_windows/v/old/vlib/runtime/runtime_test.v39
-rw-r--r--v_windows/v/old/vlib/runtime/runtime_windows.c.v21
4 files changed, 127 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/runtime/runtime.v b/v_windows/v/old/vlib/runtime/runtime.v
new file mode 100644
index 0000000..4f92fe1
--- /dev/null
+++ b/v_windows/v/old/vlib/runtime/runtime.v
@@ -0,0 +1,56 @@
+// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
+// Use of this source code is governed by an MIT license
+// that can be found in the LICENSE file.
+
+module runtime
+
+import os
+
+// nr_jobs returns the same as `nr_cpus` with the difference that if an
+// environment variable `VJOBS` is set, and has a value > 0,
+// then `nr_jobs` will return that number instead.
+// This is useful for runtime tweaking of e.g. threaded or concurrent code.
+pub fn nr_jobs() int {
+ mut cpus := nr_cpus() - 1
+ // allow for overrides, for example using `VJOBS=32 ./v test .`
+ vjobs := os.getenv('VJOBS').int()
+ if vjobs > 0 {
+ cpus = vjobs
+ }
+ if cpus == 0 {
+ return 1
+ }
+ return cpus
+}
+
+// is_32bit returns true if the current executable is running on a 32 bit system.
+pub fn is_32bit() bool {
+ $if x32 {
+ return true
+ }
+ return false
+}
+
+// is_64bit returns true if the current executable is running on a 64 bit system.
+pub fn is_64bit() bool {
+ $if x64 {
+ return true
+ }
+ return false
+}
+
+// is_little_endian returns true if the current executable is running on a little-endian system.
+pub fn is_little_endian() bool {
+ $if little_endian {
+ return true
+ }
+ return false
+}
+
+// is_big_endian returns true if the current executable is running on a big-endian system.
+pub fn is_big_endian() bool {
+ $if big_endian {
+ return true
+ }
+ return false
+}
diff --git a/v_windows/v/old/vlib/runtime/runtime_nix.c.v b/v_windows/v/old/vlib/runtime/runtime_nix.c.v
new file mode 100644
index 0000000..a5c69b0
--- /dev/null
+++ b/v_windows/v/old/vlib/runtime/runtime_nix.c.v
@@ -0,0 +1,11 @@
+module runtime
+
+fn C.sysconf(name int) i64
+
+// nr_cpus returns the number of virtual CPU cores found on the system.
+pub fn nr_cpus() int {
+ $if linux || macos || solaris {
+ return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
+ }
+ return 1
+}
diff --git a/v_windows/v/old/vlib/runtime/runtime_test.v b/v_windows/v/old/vlib/runtime/runtime_test.v
new file mode 100644
index 0000000..d180890
--- /dev/null
+++ b/v_windows/v/old/vlib/runtime/runtime_test.v
@@ -0,0 +1,39 @@
+import runtime
+
+fn test_nr_cpus() {
+ nr_cpus := runtime.nr_cpus()
+ assert nr_cpus > 0
+}
+
+fn test_nr_jobs() {
+ nr_jobs := runtime.nr_jobs()
+ assert nr_jobs > 0
+}
+
+fn test_is_32bit() {
+ x := runtime.is_32bit().str()
+ assert x == 'true' || x == 'false'
+}
+
+fn test_is_64bit() {
+ x := runtime.is_64bit().str()
+ assert x == 'true' || x == 'false'
+}
+
+fn test_is_little_endian() {
+ x := runtime.is_little_endian().str()
+ assert x == 'true' || x == 'false'
+}
+
+fn test_is_big_endian() {
+ x := runtime.is_big_endian().str()
+ assert x == 'true' || x == 'false'
+}
+
+fn test_is_big_endian_different_than_is_little_endian() {
+ assert runtime.is_big_endian() != runtime.is_little_endian()
+}
+
+fn test_is_32bit_different_than_is_64bit() {
+ assert runtime.is_32bit() != runtime.is_64bit()
+}
diff --git a/v_windows/v/old/vlib/runtime/runtime_windows.c.v b/v_windows/v/old/vlib/runtime/runtime_windows.c.v
new file mode 100644
index 0000000..de5b2ce
--- /dev/null
+++ b/v_windows/v/old/vlib/runtime/runtime_windows.c.v
@@ -0,0 +1,21 @@
+module runtime
+
+import os
+
+[typedef]
+struct C.SYSTEM_INFO {
+ dwNumberOfProcessors u32
+}
+
+fn C.GetSystemInfo(&C.SYSTEM_INFO)
+
+// nr_cpus returns the number of virtual CPU cores found on the system.
+pub fn nr_cpus() int {
+ sinfo := C.SYSTEM_INFO{}
+ C.GetSystemInfo(&sinfo)
+ mut nr := int(sinfo.dwNumberOfProcessors)
+ if nr == 0 {
+ nr = os.getenv('NUMBER_OF_PROCESSORS').int()
+ }
+ return nr
+}