Implement nproc for NetBSD, OpenBSD.
authorBruno Haible <bruno@clisp.org>
Sun, 18 Oct 2009 08:13:58 +0000 (10:13 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 18 Oct 2009 08:13:58 +0000 (10:13 +0200)
ChangeLog
lib/nproc.c
m4/nproc.m4 [new file with mode: 0644]
modules/nproc

index 6f7e5d8..240ef97 100644 (file)
--- 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.
 2009-10-18  Bruno Haible  <bruno@clisp.org>
 
        Fix recognition of sys/sysctl.h on OpenBSD 4.0.
index 9a6e23a..a1a7c41 100644 (file)
 
 #include <unistd.h>
 
 
 #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)
 {
 /* 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;
 #endif
 
   return 1;
diff --git a/m4/nproc.m4 b/m4/nproc.m4
new file mode 100644 (file)
index 0000000..73b812d
--- /dev/null
@@ -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])
+])
index 7bdc4ed..7b24121 100644 (file)
@@ -4,14 +4,16 @@ Detect the number of processors
 Files:
 lib/nproc.h
 lib/nproc.c
 Files:
 lib/nproc.h
 lib/nproc.c
+m4/nproc.m4
 
 Depends-on:
 unistd
 
 configure.ac:
 
 Depends-on:
 unistd
 
 configure.ac:
-AC_LIBOBJ([nproc])
+gl_NPROC
 
 Makefile.am:
 
 Makefile.am:
+lib_SOURCES += nproc.c
 
 Include:
 "nproc.h"
 
 Include:
 "nproc.h"