Mercurial > hg > octave-kai > gnulib-hg
changeset 12176:4c3269eff85a
Implement nproc for NetBSD, OpenBSD.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 18 Oct 2009 10:13:58 +0200 |
parents | 45b975b8ea71 |
children | 9ceaaf0610a5 |
files | ChangeLog lib/nproc.c m4/nproc.m4 modules/nproc |
diffstat | 4 files changed, 71 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2009-10-18 Giuseppe Scrivano <gscrivano@gnu.org> + Bruno Haible <bruno@clisp.org> + + Implement nproc for NetBSD, OpenBSD. + * lib/nproc.c: Include <sys/types.h>, <sys/param.h>, <sys/sysctl.h>. + (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 <bruno@clisp.org> Fix recognition of sys/sysctl.h on OpenBSD 4.0.
--- a/lib/nproc.c +++ b/lib/nproc.c @@ -23,15 +23,43 @@ #include <unistd.h> +#include <sys/types.h> + +#if HAVE_SYS_PARAM_H +# include <sys/param.h> +#endif + +#if HAVE_SYS_SYSCTL_H +# include <sys/sysctl.h> +#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;
new file mode 100644 --- /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 <sys/sysctl.h> requires <sys/param.h> on OpenBSD 4.0. + AC_CHECK_HEADERS([sys/sysctl.h],,, + [AC_INCLUDES_DEFAULT + #if HAVE_SYS_PARAM_H + # include <sys/param.h> + #endif + ]) + AC_CHECK_FUNCS([sysctl]) +])