From 2d20b9d88c4fc91637babaafa4a60dd7943e03a7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 17 Feb 2021 13:55:12 +0000 Subject: nproc: port to WIN32 and enable by default Use the WIN32 API GetProcessAffinityMask() to obtain the number of processors configured and the number available to the current process. This only works for up to 64 processors. --- configs/mingw32_defconfig | 4 ++-- configs/mingw64_defconfig | 4 ++-- coreutils/nproc.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/configs/mingw32_defconfig b/configs/mingw32_defconfig index 772f7fdb9..7108c2dda 100644 --- a/configs/mingw32_defconfig +++ b/configs/mingw32_defconfig @@ -1,7 +1,7 @@ # # Automatically generated make config: don't edit # Busybox version: 1.34.0.git -# Sat Feb 13 10:29:16 2021 +# Wed Feb 17 13:34:38 2021 # CONFIG_HAVE_DOT_CONFIG=y # CONFIG_PLATFORM_POSIX is not set @@ -295,7 +295,7 @@ CONFIG_MV=y # CONFIG_NICE is not set CONFIG_NL=y # CONFIG_NOHUP is not set -# CONFIG_NPROC is not set +CONFIG_NPROC=y CONFIG_OD=y CONFIG_PASTE=y CONFIG_PRINTENV=y diff --git a/configs/mingw64_defconfig b/configs/mingw64_defconfig index f73f23fa3..0cb4f4a3e 100644 --- a/configs/mingw64_defconfig +++ b/configs/mingw64_defconfig @@ -1,7 +1,7 @@ # # Automatically generated make config: don't edit # Busybox version: 1.34.0.git -# Sat Feb 13 10:37:01 2021 +# Wed Feb 17 13:34:38 2021 # CONFIG_HAVE_DOT_CONFIG=y # CONFIG_PLATFORM_POSIX is not set @@ -295,7 +295,7 @@ CONFIG_MV=y # CONFIG_NICE is not set CONFIG_NL=y # CONFIG_NOHUP is not set -# CONFIG_NPROC is not set +CONFIG_NPROC=y CONFIG_OD=y CONFIG_PASTE=y CONFIG_PRINTENV=y diff --git a/coreutils/nproc.c b/coreutils/nproc.c index 31e452bc4..7025765c5 100644 --- a/coreutils/nproc.c +++ b/coreutils/nproc.c @@ -29,6 +29,7 @@ int nproc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) { +#if !ENABLE_PLATFORM_MINGW32 unsigned long mask[1024]; int count = 0; #if ENABLE_LONG_OPTS @@ -63,6 +64,35 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) } } } +#else /* ENABLE_PLATFORM_MINGW32 */ + int count = 0; + DWORD_PTR process_affinity, system_affinity; +#if ENABLE_LONG_OPTS + int ignore = 0; + int opts = getopt32long(argv, "\xfe:+", + "ignore\0" Required_argument "\xfe" + "all\0" No_argument "\xff" + , &ignore + ); +#endif + if (!GetProcessAffinityMask(GetCurrentProcess(), &process_affinity, + &system_affinity)) { + process_affinity = system_affinity = 0; + } + +#if ENABLE_LONG_OPTS + if (opts & (1 << 1)) { + for (count = 0; system_affinity; system_affinity >>= 1) { + count += system_affinity & 1; + } + } else +#endif + { + for (count = 0; process_affinity; process_affinity >>= 1) { + count += process_affinity & 1; + } + } +#endif /* ENABLE_PLATFORM_MINGW32 */ IF_LONG_OPTS(count -= ignore;) if (count <= 0) -- cgit v1.2.3-55-g6feb