runtime: simplify nr_cpus(), add a test for nr_jobs()

pull/5895/head
Carlos Esquerdo Bernat 2020-07-20 16:36:44 +02:00 committed by GitHub
parent fb4c3ff31a
commit c60948e52e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 32 deletions

View File

@ -14,13 +14,6 @@ fn C.sysconf(name int) i64
fn C.GetCurrentProcessorNumber() u32 fn C.GetCurrentProcessorNumber() u32
//} //}
pub fn nr_cpus() int {
$if windows {
return nr_cpus_win()
}
return nr_cpus_nix()
}
pub fn nr_jobs() int { pub fn nr_jobs() int {
mut cpus := nr_cpus() mut cpus := nr_cpus()
// allow for overrides, for example using `VJOBS=32 ./v test .` // allow for overrides, for example using `VJOBS=32 ./v test .`
@ -32,25 +25,21 @@ pub fn nr_jobs() int {
} }
pub fn is_32bit() bool { pub fn is_32bit() bool {
mut x := false $if x32 { return true }
$if x32 { x = true } return false
return x
} }
pub fn is_64bit() bool { pub fn is_64bit() bool {
mut x := false $if x64 { return true }
$if x64 { x = true } return false
return x
} }
pub fn is_little_endian() bool { pub fn is_little_endian() bool {
mut x := false $if little_endian { return true }
$if little_endian { x = true } return false
return x
} }
pub fn is_big_endian() bool { pub fn is_big_endian() bool {
mut x := false $if big_endian { return true }
$if big_endian { x = true } return false
return x
} }

View File

@ -1,6 +1,6 @@
module runtime module runtime
fn nr_cpus_nix() int { pub fn nr_cpus() int {
$if linux { $if linux {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN)) return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
} }
@ -11,9 +11,4 @@ fn nr_cpus_nix() int {
return int(C.sysconf(C._SC_NPROCESSORS_ONLN)) return int(C.sysconf(C._SC_NPROCESSORS_ONLN))
} }
return 1 return 1
} }
fn nr_cpus_win() int {
eprintln('nr_cpus_win should be callable only for windows')
return 1
}

View File

@ -5,6 +5,11 @@ fn test_nr_cpus() {
assert nr_cpus > 0 assert nr_cpus > 0
} }
fn test_nr_jobs() {
nr_jobs := runtime.nr_jobs()
assert nr_jobs > 0
}
fn test_is_32bit(){ fn test_is_32bit(){
x := runtime.is_32bit().str() x := runtime.is_32bit().str()
assert x == 'true' || x == 'false' assert x == 'true' || x == 'false'

View File

@ -2,15 +2,10 @@ module runtime
import os import os
fn nr_cpus_win() int { pub fn nr_cpus() int {
mut nr := int(C.GetCurrentProcessorNumber()) mut nr := int(C.GetCurrentProcessorNumber())
if nr == 0 { if nr == 0 {
nr = os.getenv('NUMBER_OF_PROCESSORS').int() nr = os.getenv('NUMBER_OF_PROCESSORS').int()
} }
return nr return nr
} }
fn nr_cpus_nix() int {
eprintln('nr_cpus_nix should be callable only for nix platforms')
return 1
}