Sync with 'maint'
[git/gitster.git] / thread-utils.c
blob1f89ffab4c32bc02b5d955851401628a5b9a540e
1 #include "git-compat-util.h"
2 #include "thread-utils.h"
4 #if defined(hpux) || defined(__hpux) || defined(_hpux)
5 # include <sys/pstat.h>
6 #endif
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.
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
21 int online_cpus(void)
23 #ifdef NO_PTHREADS
24 return 1;
25 #else
26 #ifdef _SC_NPROCESSORS_ONLN
27 long ncpus;
28 #endif
30 #ifdef GIT_WINDOWS_NATIVE
31 SYSTEM_INFO info;
32 GetSystemInfo(&info);
34 if ((int)info.dwNumberOfProcessors > 0)
35 return (int)info.dwNumberOfProcessors;
36 #elif defined(hpux) || defined(__hpux) || defined(_hpux)
37 struct pst_dynamic psd;
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;
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) */
59 #ifdef _SC_NPROCESSORS_ONLN
60 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
61 return (int)ncpus;
62 #endif
64 return 1;
65 #endif
68 int init_recursive_mutex(pthread_mutex_t *m)
70 #ifndef NO_PTHREADS
71 pthread_mutexattr_t a;
72 int ret;
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);
81 return ret;
82 #else
83 return 0;
84 #endif
87 #ifdef NO_PTHREADS
88 int dummy_pthread_create(pthread_t *pthread, const void *attr,
89 void *(*fn)(void *), void *data)
92 * Do nothing.
94 * The main purpose of this function is to break compiler's
95 * flow analysis and avoid -Wunused-variable false warnings.
97 return ENOSYS;
100 int dummy_pthread_init(void *data)
103 * Do nothing.
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
111 return ENOSYS;
114 int dummy_pthread_join(pthread_t pthread, void **retval)
117 * Do nothing.
119 * The main purpose of this function is to break compiler's
120 * flow analysis and avoid -Wunused-variable false warnings.
122 return ENOSYS;
125 #endif