Implement nproc for mingw.
[pspp] / lib / nproc.c
1 /* Detect the number of processors.
2
3    Copyright (C) 2009 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Glen Lenker.  */
20
21 #include <config.h>
22 #include "nproc.h"
23
24 #include <unistd.h>
25
26 #include <sys/types.h>
27
28 #if HAVE_SYS_PSTAT_H
29 # include <sys/pstat.h>
30 #endif
31
32 #if HAVE_SYS_SYSMP_H
33 # include <sys/sysmp.h>
34 #endif
35
36 #if HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39
40 #if HAVE_SYS_SYSCTL_H
41 # include <sys/sysctl.h>
42 #endif
43
44 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45 # define WIN32_LEAN_AND_MEAN
46 # include <windows.h>
47 #endif
48
49 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
50
51 /* Return the total number of processors.  The result is guaranteed to
52    be at least 1.  */
53 unsigned long int
54 num_processors (void)
55 {
56 #if defined _SC_NPROCESSORS_ONLN
57   { /* This works on glibc, MacOS X 10.5, FreeBSD, AIX, OSF/1, Solaris, Cygwin,
58        Haiku.  */
59     long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
60     if (0 < nprocs)
61       return nprocs;
62   }
63 #endif
64
65 #if HAVE_PSTAT_GETDYNAMIC
66   { /* This works on HP-UX.  */
67     struct pst_dynamic psd;
68     if (0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)
69         && 0 < psd.psd_proc_cnt)
70       return psd.psd_proc_cnt;
71   }
72 #endif
73
74 #if HAVE_SYSMP && defined MP_NAPROCS
75   { /* This works on IRIX.  */
76     /* MP_NPROCS yields the number of installed processors.
77        MP_NAPROCS yields the number of processors available to unprivileged
78        processes.  We need the latter.  */
79     int nprocs = sysmp (MP_NAPROCS);
80     if (0 < nprocs)
81       return nprocs;
82   }
83 #endif
84
85 #if HAVE_SYSCTL && defined HW_NCPU
86   { /* This works on MacOS X, FreeBSD, NetBSD, OpenBSD.  */
87     int nprocs;
88     size_t len = sizeof (nprocs);
89     static int mib[2] = { CTL_HW, HW_NCPU };
90
91     if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0
92         && len == sizeof (nprocs)
93         && 0 < nprocs)
94       return nprocs;
95   }
96 #endif
97
98 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
99   { /* This works on native Windows platforms.  */
100     SYSTEM_INFO system_info;
101     GetSystemInfo (&system_info);
102     if (0 < system_info.dwNumberOfProcessors)
103       return system_info.dwNumberOfProcessors;
104   }
105 #endif
106
107   return 1;
108 }