diff --git a/CHANGELOG.md b/CHANGELOG.md index e50db78d..0e91fe39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All notable changes to this project will be documented in this file. +## Unreleased - ??? +- Added `os/cpu-count` to get the number of available processors on a machine + ## 1.22.0 - 2022-05-09 - Prohibit negative size argument to `table/new`. - Add `module/value`. diff --git a/src/core/features.h b/src/core/features.h index 6f37f34c..40067414 100644 --- a/src/core/features.h +++ b/src/core/features.h @@ -36,6 +36,11 @@ # endif #endif +/* Needed for sched.h for cpu count */ +#ifdef __linux__ +#define _GNU_SOURCE +#endif + #if defined(WIN32) || defined(_WIN32) #define WIN32_LEAN_AND_MEAN #endif diff --git a/src/core/os.c b/src/core/os.c index 54497fcc..db747525 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -43,6 +43,10 @@ #include #endif +#ifdef JANET_LINUX +#include +#endif + #ifdef JANET_WINDOWS #include #include @@ -218,7 +222,11 @@ JANET_CORE_FN(os_cpu_count, return janet_wrap_integer(info.dwNumberOfProcessors); #elif defined(JANET_LINUX) (void) dflt; - return janet_wrap_integer(sysconf(_SC_NPROCESSORS_ONLN)); + cpu_set_t cs; + CPU_ZERO(&cs); + sched_getaffinity(0, sizeof(cs), &cs); + int count = CPU_COUNT(&cs); + return janet_wrap_integer(count); #elif defined(JANET_BSD) && defined(HW_NCPUONLINE) (void) dflt; const int name[2] = {CTL_HW, HW_NCPUONLINE};