Files
c3c/lib/std/os/cpu.c3
Book-reader bb9e9b54cf Improve android support & add CI (#2664)
* Change context destruction order.

* enable emulated tls on termux

* Fix stdlib on android

* Add a CI workflow for android termux

* update release notes

* use the new unified CI tests on android

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
2026-02-05 20:04:16 +01:00

72 lines
1.3 KiB
Plaintext

// https://www.cprogramming.com/snippets/source-code/find-the-number-of-cpu-cores-for-windows-mac-or-linux
module std::os @if(env::DARWIN);
import std::os::macos;
fn uint num_cpu()
{
int[2] nm;
usz len = 4;
uint count;
nm = { darwin::CTL_HW, darwin::HW_NCPU };
darwin::sysctl(&nm, 2, &count, &len, null, 0);
if (count < 1) count = 1;
return count;
}
module std::os @if(env::LINUX || env::ANDROID);
import std::os::posix;
fn uint num_cpu()
{
return posix::get_nprocs_conf();
}
module std::os @if(env::WIN32);
import std::os::win32;
fn uint num_cpu()
{
Win32_SYSTEM_INFO info;
win32::getSystemInfo(&info);
return info.dwNumberOfProcessors;
}
module std::os @if(env::NETBSD);
import std::os::netbsd;
import libc;
fn uint num_cpu()
{
int[2] nm;
usz len = 4;
uint count;
nm = { netbsd::CTL_HW, netbsd::HW_NCPU };
libc::sysctl(&nm, 2, &count, &len, null, 0);
if (count < 1) count = 1;
return count;
}
module std::os @if(env::OPENBSD);
import std::os::openbsd;
import libc;
fn uint num_cpu()
{
int[2] nm;
usz len = 4;
uint count;
nm = { openbsd::CTL_HW, openbsd::HW_NCPUONLINE };
if (libc::sysctl(&nm, 2, &count, &len, null, 0) == 0 && count >= 1) {
return count;
}
nm = { openbsd::CTL_HW, openbsd::HW_NCPU };
libc::sysctl(&nm, 2, &count, &len, null, 0);
if (count < 1) count = 1;
return count;
}