move num_cpu() to std::os (#928)

* lib/std: move num_cpu() to std::os

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* add ThreadPool (#926)

* lib/std/collections: fix tab indentation

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/threads: add ThreadPool

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* ats/lib/threads: add num_cpu()

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/os: move macos constants to std::os::macos

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2023-08-14 17:04:06 +02:00
committed by GitHub
parent 65bea1cb2d
commit f4ad9fcee0
5 changed files with 98 additions and 3 deletions

34
lib/std/os/cpu.c3 Normal file
View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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");

View File

@@ -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();