runtime: add windows support for nr_cpus()

pull/2317/head
Nicolas Sauzede 2019-10-13 00:01:15 +02:00 committed by Alexander Medvednikov
parent 432e074b4e
commit 1292163637
5 changed files with 29 additions and 23 deletions

View File

@ -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 <sys/sysinfo.h>
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
}

View File

@ -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

View File

@ -1,2 +1 @@
module runtime

View File

@ -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
}

View File

@ -1,2 +1 @@
module runtime