From f4ad9fcee03b8db93987a9375d38f7a127d6ba05 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Mon, 14 Aug 2023 17:04:06 +0200 Subject: [PATCH] move num_cpu() to std::os (#928) * lib/std: move num_cpu() to std::os Signed-off-by: Pierre Curto * add ThreadPool (#926) * lib/std/collections: fix tab indentation Signed-off-by: Pierre Curto * lib/std/threads: add ThreadPool Signed-off-by: Pierre Curto * ats/lib/threads: add num_cpu() Signed-off-by: Pierre Curto --------- Signed-off-by: Pierre Curto * lib/std/os: move macos constants to std::os::macos Signed-off-by: Pierre Curto --------- Signed-off-by: Pierre Curto --- lib/std/os/cpu.c3 | 34 +++++++++++++++++++++++++++++++++ lib/std/os/macos/darwin.c3 | 38 +++++++++++++++++++++++++++++++++++++ lib/std/os/posix/process.c3 | 4 ++++ lib/std/os/win32/process.c3 | 22 +++++++++++++++++++++ lib/std/threads/thread.c3 | 3 --- 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 lib/std/os/cpu.c3 create mode 100644 lib/std/os/macos/darwin.c3 diff --git a/lib/std/os/cpu.c3 b/lib/std/os/cpu.c3 new file mode 100644 index 000000000..e0d3e6bbc --- /dev/null +++ b/lib/std/os/cpu.c3 @@ -0,0 +1,34 @@ +// 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 = { macos::CTL_HW, macos::HW_NCPU }; + macos::sysctl(&nm, 2, &count, &len, null, 0); + if (count < 1) count = 1; + return count; +} + +module std::os @if(env::LINUX); +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() +{ + SystemInfo info; + win32::get_system_info(&info); + return info.dwNumberOfProcessors; +} \ No newline at end of file diff --git a/lib/std/os/macos/darwin.c3 b/lib/std/os/macos/darwin.c3 new file mode 100644 index 000000000..40f7374c1 --- /dev/null +++ b/lib/std/os/macos/darwin.c3 @@ -0,0 +1,38 @@ +module std::os::macos @if(env::DARWIN); + +const CTL_UNSPEC = 0; /* unused */ +const CTL_KERN = 1; /* "high kernel": proc, limits */ +const CTL_VM = 2; /* virtual memory */ +const CTL_VFS = 3; /* file system, mount type is next */ +const CTL_NET = 4; /* network, see socket.h */ +const CTL_DEBUG = 5; /* debugging parameters */ +const CTL_HW = 6; /* generic cpu/io */ +const CTL_MACHDEP = 7; /* machine dependent */ +const CTL_USER = 8; /* user-level */ +const CTL_MAXID = 9; /* number of valid top-level ids */ + +const HW_MACHINE = 1; /* string: machine class */ +const HW_MODEL = 2; /* string: specific machine model */ +const HW_NCPU = 3; /* int: number of cpus */ +const HW_BYTEORDER = 4; /* int: machine byte order */ +const HW_PHYSMEM = 5; /* int: total memory */ +const HW_USERMEM = 6; /* int: non-kernel memory */ +const HW_PAGESIZE = 7; /* int: software page size */ +const HW_DISKNAMES = 8; /* strings: disk drive names */ +const HW_DISKSTATS = 9; /* struct: diskstats[] */ +const HW_EPOCH = 10; /* int: 0 for Legacy, else NewWorld */ +const HW_FLOATINGPT = 11; /* int: has HW floating point? */ +const HW_MACHINE_ARCH = 12; /* string: machine architecture */ +const HW_VECTORUNIT = 13; /* int: has HW vector unit? */ +const HW_BUS_FREQ = 14; /* int: Bus Frequency */ +const HW_CPU_FREQ = 15; /* int: CPU Frequency */ +const HW_CACHELINE = 16; /* int: Cache Line Size in Bytes */ +const HW_L1ICACHESIZE = 17; /* int: L1 I Cache Size in Bytes */ +const HW_L1DCACHESIZE = 18; /* int: L1 D Cache Size in Bytes */ +const HW_L2SETTINGS = 19; /* int: L2 Cache Settings */ +const HW_L2CACHESIZE = 20; /* int: L2 Cache Size in Bytes */ +const HW_L3SETTINGS = 21; /* int: L3 Cache Settings */ +const HW_L3CACHESIZE = 22; /* int: L3 Cache Size in Bytes */ +const HW_MAXID = 23; /* number of valid hw ids */ + +extern fn CInt sysctl(CInt *name, CUInt namelen, void *oldp, usz *oldlenp, void *newp, usz newlen); diff --git a/lib/std/os/posix/process.c3 b/lib/std/os/posix/process.c3 index 47a737ca7..4ef659b98 100644 --- a/lib/std/os/posix/process.c3 +++ b/lib/std/os/posix/process.c3 @@ -1,5 +1,6 @@ module std::os::posix @if(env::POSIX); import libc; + struct Posix_spawn_file_actions_t { int __allocated; @@ -18,6 +19,9 @@ extern fn CInt posix_spawn_file_actions_destroy(Posix_spawn_file_actions_t *file extern fn CInt posix_spawn_file_actions_addclose(Posix_spawn_file_actions_t *file_actions, CInt fd); extern fn CInt posix_spawn_file_actions_adddup2(Posix_spawn_file_actions_t *file_actions, CInt fd, CInt newfd); +extern fn CInt get_nprocs(); +extern fn CInt get_nprocs_conf(); + def spawn_file_actions_init = posix_spawn_file_actions_init; def spawn_file_actions_destroy = posix_spawn_file_actions_destroy; def spawn_file_actions_addclose = posix_spawn_file_actions_addclose; diff --git a/lib/std/os/win32/process.c3 b/lib/std/os/win32/process.c3 index 6d703b744..0840c29aa 100644 --- a/lib/std/os/win32/process.c3 +++ b/lib/std/os/win32/process.c3 @@ -84,3 +84,25 @@ extern fn Win32_BOOL getOverlappedResult( Win32_BOOL bWait ) @extern("GetOverlappedResult"); + +struct SystemInfo +{ + union { + uint dwOemId; + struct { + ushort wProcessorArchitecture; + ushort wReserved; + } + } + uint dwPageSize; + void* lpMinimumApplicationAddress; + void* lpMaximumApplicationAddress; + usz dwActiveProcessorMask; + uint dwNumberOfProcessors; + uint dwProcessorType; + uint dwAllocationGranularity; + ushort wProcessorLevel; + ushort wProcessorRevision; +} + +extern fn CInt get_system_info(SystemInfo*) @extern("GetSystemInfo"); diff --git a/lib/std/threads/thread.c3 b/lib/std/threads/thread.c3 index 402658b58..382d7fd58 100644 --- a/lib/std/threads/thread.c3 +++ b/lib/std/threads/thread.c3 @@ -1,5 +1,4 @@ module std::thread; -import std::thread::cpu; import std::thread::os; def MutexType = distinct int; @@ -65,5 +64,3 @@ macro void exit(int result) => os::native_thread_exit(result); macro void! sleep(double s) @maydiscard => os::native_sleep(s); macro void! sleep_ms(ulong ms) @maydiscard => os::native_sleep_ms(ms); macro void! sleep_ns(ulong ns) @maydiscard => os::native_sleep_nano(ns); - -macro uint num_cpu() => cpu::native_cpu(); \ No newline at end of file