runtime: add windows support for nr_cpus()
parent
432e074b4e
commit
1292163637
|
@ -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
|
module runtime
|
||||||
|
|
||||||
/*
|
import os
|
||||||
$if linux {
|
|
||||||
#include <sys/sysinfo.h>
|
|
||||||
fn C.get_nprocs() int
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
//$if linux {
|
||||||
|
fn C.sysconf(name int) i64
|
||||||
|
//}
|
||||||
|
|
||||||
|
//$if windows {
|
||||||
|
fn C.GetCurrentProcessorNumber() u32
|
||||||
|
//}
|
||||||
|
|
||||||
pub fn nr_cpus() int {
|
pub fn nr_cpus() int {
|
||||||
//$if linux {
|
$if linux {
|
||||||
//return C.get_nprocs()
|
return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
|
||||||
//}
|
}
|
||||||
return 0
|
$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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
module runtime
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
module runtime
|
module runtime
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
import runtime
|
import runtime
|
||||||
|
|
||||||
fn test_nr_cpus() {
|
fn test_nr_cpus() {
|
||||||
$if linux {
|
nr_cpus := runtime.nr_cpus()
|
||||||
nr_cpus := runtime.nr_cpus()
|
assert nr_cpus > 0
|
||||||
println(nr_cpus)
|
}
|
||||||
assert nr_cpus >= 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
module runtime
|
module runtime
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue