From e841c33e13f765327b1d2cd98b5934e337ab44d9 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 18 Oct 2009 10:13:58 +0200 Subject: [PATCH] Implement nproc for NetBSD, OpenBSD. --- ChangeLog | 12 ++++++++++++ lib/nproc.c | 36 ++++++++++++++++++++++++++++++++---- m4/nproc.m4 | 24 ++++++++++++++++++++++++ modules/nproc | 4 +++- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 m4/nproc.m4 diff --git a/ChangeLog b/ChangeLog index 6f7e5d8a3..240ef97ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2009-10-18 Giuseppe Scrivano + Bruno Haible + + Implement nproc for NetBSD, OpenBSD. + * lib/nproc.c: Include , , . + (ARRAY_SIZE): New macro. + (num_processors): On BSD systems, try sysctl of HW_NCPU. + * m4/nproc.m4: New file. + * modules/nproc (Files): Add m4/nproc.m4. + (configure.ac): Invoke gl_NPROC. Remove AC_LIBOBJ invocation. + (Makefile.am): Instead, augment lib_SOURCES. + 2009-10-18 Bruno Haible Fix recognition of sys/sysctl.h on OpenBSD 4.0. diff --git a/lib/nproc.c b/lib/nproc.c index 9a6e23a44..a1a7c41df 100644 --- a/lib/nproc.c +++ b/lib/nproc.c @@ -23,15 +23,43 @@ #include +#include + +#if HAVE_SYS_PARAM_H +# include +#endif + +#if HAVE_SYS_SYSCTL_H +# include +#endif + +#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) + /* Return the total number of processors. The result is guaranteed to be at least 1. */ unsigned long int num_processors (void) { -#ifdef _SC_NPROCESSORS_ONLN - long int nprocs = sysconf (_SC_NPROCESSORS_ONLN); - if (0 < nprocs) - return nprocs; +#if defined _SC_NPROCESSORS_ONLN + { /* This works on glibc, MacOS X 10.5, FreeBSD, AIX, OSF/1, Solaris, Cygwin, + Haiku. */ + long int nprocs = sysconf (_SC_NPROCESSORS_ONLN); + if (0 < nprocs) + return nprocs; + } +#endif + +#if HAVE_SYSCTL && defined HW_NCPU + { /* This works on MacOS X, FreeBSD, NetBSD, OpenBSD. */ + int nprocs; + size_t len = sizeof (nprocs); + static int mib[2] = { CTL_HW, HW_NCPU }; + + if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0 + && len == sizeof (nprocs) + && 0 < nprocs) + return nprocs; + } #endif return 1; diff --git a/m4/nproc.m4 b/m4/nproc.m4 new file mode 100644 index 000000000..73b812dd2 --- /dev/null +++ b/m4/nproc.m4 @@ -0,0 +1,24 @@ +# nproc.m4 serial 1 +dnl Copyright (C) 2009 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_NPROC], +[ + gl_PREREQ_NPROC +]) + +# Prerequisites of lib/nproc.c. +AC_DEFUN([gl_PREREQ_NPROC], +[ + AC_CHECK_HEADERS([sys/param.h],,, [AC_INCLUDES_DEFAULT]) + dnl requires on OpenBSD 4.0. + AC_CHECK_HEADERS([sys/sysctl.h],,, + [AC_INCLUDES_DEFAULT + #if HAVE_SYS_PARAM_H + # include + #endif + ]) + AC_CHECK_FUNCS([sysctl]) +]) diff --git a/modules/nproc b/modules/nproc index 7bdc4ed5d..7b24121c5 100644 --- a/modules/nproc +++ b/modules/nproc @@ -4,14 +4,16 @@ Detect the number of processors Files: lib/nproc.h lib/nproc.c +m4/nproc.m4 Depends-on: unistd configure.ac: -AC_LIBOBJ([nproc]) +gl_NPROC Makefile.am: +lib_SOURCES += nproc.c Include: "nproc.h" -- 2.11.0