git
/
thread-utils.c
125 строк · 2.6 Кб
1#include "git-compat-util.h"2#include "thread-utils.h"3
4#if defined(hpux) || defined(__hpux) || defined(_hpux)5# include <sys/pstat.h>6#endif7
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_ONLN14# ifdef _SC_NPROC_ONLN15# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN16# elif defined _SC_CRAY_NCPU17# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU18# endif19#endif20
21int online_cpus(void)22{
23#ifdef NO_PTHREADS24return 1;25#else26#ifdef _SC_NPROCESSORS_ONLN27long ncpus;28#endif29
30#ifdef GIT_WINDOWS_NATIVE31SYSTEM_INFO info;32GetSystemInfo(&info);33
34if ((int)info.dwNumberOfProcessors > 0)35return (int)info.dwNumberOfProcessors;36#elif defined(hpux) || defined(__hpux) || defined(_hpux)37struct pst_dynamic psd;38
39if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))40return (int)psd.psd_proc_cnt;41#elif defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU)42int mib[2];43size_t len;44int cpucount;45
46mib[0] = CTL_HW;47# ifdef HW_AVAILCPU48mib[1] = HW_AVAILCPU;49len = sizeof(cpucount);50if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))51return cpucount;52# endif /* HW_AVAILCPU */53mib[1] = HW_NCPU;54len = sizeof(cpucount);55if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))56return cpucount;57#endif /* defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) */58
59#ifdef _SC_NPROCESSORS_ONLN60if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)61return (int)ncpus;62#endif63
64return 1;65#endif66}
67
68int init_recursive_mutex(pthread_mutex_t *m)69{
70#ifndef NO_PTHREADS71pthread_mutexattr_t a;72int ret;73
74ret = pthread_mutexattr_init(&a);75if (!ret) {76ret = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);77if (!ret)78ret = pthread_mutex_init(m, &a);79pthread_mutexattr_destroy(&a);80}81return ret;82#else83return 0;84#endif85}
86
87#ifdef NO_PTHREADS88int dummy_pthread_create(pthread_t *pthread, const void *attr,89void *(*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*/
97return 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*/
111return 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*/
122return ENOSYS;123}
124
125#endif126