Add platform files for porting to MSVC
[git/git.git] / thread-utils.c
CommitLineData
833e3df1
AE
1#include "cache.h"
2
3#ifdef _WIN32
4# define WIN32_LEAN_AND_MEAN
5# include <windows.h>
6#elif defined(hpux) || defined(__hpux) || defined(_hpux)
7# include <sys/pstat.h>
8#endif
9
10/*
11 * By doing this in two steps we can at least get
12 * the function to be somewhat coherent, even
13 * with this disgusting nest of #ifdefs.
14 */
15#ifndef _SC_NPROCESSORS_ONLN
16# ifdef _SC_NPROC_ONLN
17# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
18# elif defined _SC_CRAY_NCPU
19# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
20# endif
21#endif
22
23int online_cpus(void)
24{
25#ifdef _SC_NPROCESSORS_ONLN
26 long ncpus;
27#endif
28
29#ifdef _WIN32
30 SYSTEM_INFO info;
31 GetSystemInfo(&info);
32
33 if ((int)info.dwNumberOfProcessors > 0)
34 return (int)info.dwNumberOfProcessors;
35#elif defined(hpux) || defined(__hpux) || defined(_hpux)
36 struct pst_dynamic psd;
37
38 if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
39 return (int)psd.psd_proc_cnt;
40#endif
41
42#ifdef _SC_NPROCESSORS_ONLN
43 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
44 return (int)ncpus;
45#endif
46
47 return 1;
48}