| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | |
| 5 | module runtime |
| 6 | |
| 7 | import os |
| 8 | |
| 9 | // nr_jobs returns the same as `nr_cpus`, but influenced by the env variable `VJOBS`. |
| 10 | // If the environment variable `VJOBS` is set, and has a value > 0, |
| 11 | // then `nr_jobs` will return that number instead. |
| 12 | // This is useful for runtime tweaking of e.g. threaded or concurrent code. |
| 13 | pub fn nr_jobs() int { |
| 14 | $if cross ? { |
| 15 | // A single thread is *more likely* to work consistently everywhere during bootstrapping. |
| 16 | // NB: the compiler itself uses runtime.nr_jobs() and sync.pool to process things in parallel |
| 17 | // in its cgen stage. Returning 1 here, increases the chances of it working on non linux systems. |
| 18 | return 1 |
| 19 | } |
| 20 | mut cpus := nr_cpus() |
| 21 | // allow for overrides, for example using `VJOBS=32 ./v test .` |
| 22 | vjobs := os.getenv('VJOBS').int() |
| 23 | if vjobs > 0 { |
| 24 | cpus = vjobs |
| 25 | } |
| 26 | if cpus == 0 { |
| 27 | return 1 |
| 28 | } |
| 29 | return cpus |
| 30 | } |
| 31 | |
| 32 | // is_32bit returns true if the current executable is running on a 32 bit system. |
| 33 | pub fn is_32bit() bool { |
| 34 | $if x32 { |
| 35 | return true |
| 36 | } |
| 37 | return false |
| 38 | } |
| 39 | |
| 40 | // is_64bit returns true if the current executable is running on a 64 bit system. |
| 41 | pub fn is_64bit() bool { |
| 42 | $if x64 { |
| 43 | return true |
| 44 | } |
| 45 | return false |
| 46 | } |
| 47 | |
| 48 | // is_little_endian returns true if the current executable is running on a little-endian system. |
| 49 | pub fn is_little_endian() bool { |
| 50 | $if little_endian { |
| 51 | return true |
| 52 | } |
| 53 | return false |
| 54 | } |
| 55 | |
| 56 | // is_big_endian returns true if the current executable is running on a big-endian system. |
| 57 | pub fn is_big_endian() bool { |
| 58 | $if big_endian { |
| 59 | return true |
| 60 | } |
| 61 | return false |
| 62 | } |
| 63 | |