blob: 532984569132de11f6f05b561f817817523a3468 [file] [log] [blame]
Andreas Ericsson833e3df2008-02-22 20:11:56 -06001#include "cache.h"
Stephen Boydc2e86ad2011-03-22 00:51:05 -07002#include "thread-utils.h"
Andreas Ericsson833e3df2008-02-22 20:11:56 -06003
Marius Storm-Olsen435bdf82009-09-16 10:20:26 +02004#if defined(hpux) || defined(__hpux) || defined(_hpux)
Andreas Ericsson833e3df2008-02-22 20:11:56 -06005# 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{
Nguyễn Thái Ngọc Duy0ec79352018-10-27 19:29:59 +020023#ifdef NO_PTHREADS
24 return 1;
25#else
Andreas Ericsson833e3df2008-02-22 20:11:56 -060026#ifdef _SC_NPROCESSORS_ONLN
27 long ncpus;
28#endif
29
Jonathan Nieder380395d2013-05-02 20:26:08 +010030#ifdef GIT_WINDOWS_NATIVE
Andreas Ericsson833e3df2008-02-22 20:11:56 -060031 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;
Kyle J. McKaya25b5a32015-03-07 23:14:37 -080041#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) */
Andreas Ericsson833e3df2008-02-22 20:11:56 -060058
59#ifdef _SC_NPROCESSORS_ONLN
60 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
61 return (int)ncpus;
62#endif
63
64 return 1;
Nguyễn Thái Ngọc Duy0ec79352018-10-27 19:29:59 +020065#endif
Andreas Ericsson833e3df2008-02-22 20:11:56 -060066}
Johannes Sixt93749192010-04-08 09:15:39 +020067
68int init_recursive_mutex(pthread_mutex_t *m)
69{
Nguyễn Thái Ngọc Duy0ec79352018-10-27 19:29:59 +020070#ifndef NO_PTHREADS
Johannes Sixt93749192010-04-08 09:15:39 +020071 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;
Nguyễn Thái Ngọc Duy0ec79352018-10-27 19:29:59 +020082#else
83 return 0;
84#endif
Johannes Sixt93749192010-04-08 09:15:39 +020085}
Nguyễn Thái Ngọc Duy0ec79352018-10-27 19:29:59 +020086
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