From 129216363798a0b3a8879fe4dd5327d98894edf3 Mon Sep 17 00:00:00 2001 From: Nicolas Sauzede Date: Sun, 13 Oct 2019 00:01:15 +0200 Subject: [PATCH] runtime: add windows support for nr_cpus() --- vlib/runtime/runtime.v | 36 ++++++++++++++++++++++++++---------- vlib/runtime/runtime_lin.v | 5 ----- vlib/runtime/runtime_mac.v | 1 - vlib/runtime/runtime_test.v | 9 +++------ vlib/runtime/runtime_win.v | 1 - 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/vlib/runtime/runtime.v b/vlib/runtime/runtime.v index d008eb0275..cfede391c1 100644 --- a/vlib/runtime/runtime.v +++ b/vlib/runtime/runtime.v @@ -1,16 +1,32 @@ +// Copyright (c) 2019 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 -/* -$if linux { - #include - fn C.get_nprocs() int -} -*/ +import os +//$if linux { +fn C.sysconf(name int) i64 +//} + +//$if windows { +fn C.GetCurrentProcessorNumber() u32 +//} pub fn nr_cpus() int { - //$if linux { - //return C.get_nprocs() - //} - return 0 + $if linux { + return int(C.sysconf(C._SC_NPROCESSORS_ONLN)) + } + $if mac { + return int(C.sysconf(C._SC_NPROCESSORS_ONLN)) + } + $if windows { + mut nr := int(C.GetCurrentProcessorNumber()) + if nr == 0 { + nr = os.getenv('NUMBER_OF_PROCESSORS').int() + } + return nr + } + return 1 } diff --git a/vlib/runtime/runtime_lin.v b/vlib/runtime/runtime_lin.v index 18c61de478..7777c00615 100644 --- a/vlib/runtime/runtime_lin.v +++ b/vlib/runtime/runtime_lin.v @@ -1,6 +1 @@ -// Copyright (c) 2019 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 - diff --git a/vlib/runtime/runtime_mac.v b/vlib/runtime/runtime_mac.v index 9df2fff173..7777c00615 100644 --- a/vlib/runtime/runtime_mac.v +++ b/vlib/runtime/runtime_mac.v @@ -1,2 +1 @@ module runtime - diff --git a/vlib/runtime/runtime_test.v b/vlib/runtime/runtime_test.v index ee85285d27..00d1bb72fa 100644 --- a/vlib/runtime/runtime_test.v +++ b/vlib/runtime/runtime_test.v @@ -1,9 +1,6 @@ import runtime fn test_nr_cpus() { - $if linux { - nr_cpus := runtime.nr_cpus() - println(nr_cpus) - assert nr_cpus >= 0 - } -} + nr_cpus := runtime.nr_cpus() + assert nr_cpus > 0 +} diff --git a/vlib/runtime/runtime_win.v b/vlib/runtime/runtime_win.v index 9df2fff173..7777c00615 100644 --- a/vlib/runtime/runtime_win.v +++ b/vlib/runtime/runtime_win.v @@ -1,2 +1 @@ module runtime -