1#include "cache.h"
2#include "thread-utils.h"
3
4#if defined(hpux) || defined(__hpux) || defined(_hpux)
5# include <sys/pstat.h>
6#endif
7
8/*
9 * By doing this in two steps we can at least get
10 * the function to be somewhat coherent, even
11 * with this disgusting nest of #ifdefs.
12 */
13#ifndef _SC_NPROCESSORS_ONLN
14# ifdef _SC_NPROC_ONLN
15# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
16# elif defined _SC_CRAY_NCPU
17# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
18# endif
19#endif
20
21int online_cpus(void)
22{
23#ifdef NO_PTHREADS
24 return 1;
25#else
26#ifdef _SC_NPROCESSORS_ONLN
27 long ncpus;
28#endif
29
30#ifdef GIT_WINDOWS_NATIVE
31 SYSTEM_INFO info;
32 GetSystemInfo(&info);
33
34 if ((int)info.dwNumberOfProcessors > 0)
35 return (int)info.dwNumberOfProcessors;
36#elif defined(hpux) || defined(__hpux) || defined(_hpux)
37 struct pst_dynamic psd;
38
39 if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
40 return (int)psd.psd_proc_cnt;
41#elif defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU)
42 int mib[2];
43 size_t len;
44 int cpucount;
45
46 mib[0] = CTL_HW;
47# ifdef HW_AVAILCPU
48 mib[1] = HW_AVAILCPU;
49 len = sizeof(cpucount);
50 if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
51 return cpucount;
52# endif /* HW_AVAILCPU */
53 mib[1] = HW_NCPU;
54 len = sizeof(cpucount);
55 if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
56 return cpucount;
57#endif /* defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) */
58
59#ifdef _SC_NPROCESSORS_ONLN
60 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
61 return (int)ncpus;
62#endif
63
64 return 1;
65#endif
66}
67
68int init_recursive_mutex(pthread_mutex_t *m)
69{
70#ifndef NO_PTHREADS
71 pthread_mutexattr_t a;
72 int ret;
73
74 ret = pthread_mutexattr_init(&a);
75 if (!ret) {
76 ret = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
77 if (!ret)
78 ret = pthread_mutex_init(m, &a);
79 pthread_mutexattr_destroy(&a);
80 }
81 return ret;
82#else
83 return 0;
84#endif
85}
86
87#ifdef NO_PTHREADS
88int dummy_pthread_create(pthread_t *pthread, const void *attr,
89 void *(*fn)(void *), void *data)
90{
91 /*
92 * Do nothing.
93 *
94 * The main purpose of this function is to break compiler's
95 * flow analysis and avoid -Wunused-variable false warnings.
96 */
97 return ENOSYS;
98}
99
100int dummy_pthread_init(void *data)
101{
102 /*
103 * Do nothing.
104 *
105 * The main purpose of this function is to break compiler's
106 * flow analysis or it may realize that functions like
107 * pthread_mutex_init() is no-op, which means the (static)
108 * variable is not used/initialized at all and trigger
109 * -Wunused-variable
110 */
111 return ENOSYS;
112}
113
114int dummy_pthread_join(pthread_t pthread, void **retval)
115{
116 /*
117 * Do nothing.
118 *
119 * The main purpose of this function is to break compiler's
120 * flow analysis and avoid -Wunused-variable false warnings.
121 */
122 return ENOSYS;
123}
124
125#endif