1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-09-11 11:50:52 +03:00

Implement nproc for NetBSD, OpenBSD.

This commit is contained in:
Bruno Haible
2009-10-18 10:13:58 +02:00
parent bb0465fcf8
commit e841c33e13
4 changed files with 71 additions and 5 deletions

View File

@@ -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.

View File

@@ -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;

24
m4/nproc.m4 Normal file
View File

@@ -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])
])

View File

@@ -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"