Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 1 | #include "cache.h" |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 2 | #include "thread-utils.h" |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 3 | |
Marius Storm-Olsen | 435bdf8 | 2009-09-16 10:20:26 +0200 | [diff] [blame] | 4 | #if defined(hpux) || defined(__hpux) || defined(_hpux) |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 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 | |
| 21 | int online_cpus(void) |
| 22 | { |
| 23 | #ifdef _SC_NPROCESSORS_ONLN |
| 24 | long ncpus; |
| 25 | #endif |
| 26 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 27 | #ifdef GIT_WINDOWS_NATIVE |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 28 | SYSTEM_INFO info; |
| 29 | GetSystemInfo(&info); |
| 30 | |
| 31 | if ((int)info.dwNumberOfProcessors > 0) |
| 32 | return (int)info.dwNumberOfProcessors; |
| 33 | #elif defined(hpux) || defined(__hpux) || defined(_hpux) |
| 34 | struct pst_dynamic psd; |
| 35 | |
| 36 | if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0)) |
| 37 | return (int)psd.psd_proc_cnt; |
Kyle J. McKay | a25b5a3 | 2015-03-07 23:14:37 -0800 | [diff] [blame] | 38 | #elif defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) |
| 39 | int mib[2]; |
| 40 | size_t len; |
| 41 | int cpucount; |
| 42 | |
| 43 | mib[0] = CTL_HW; |
| 44 | # ifdef HW_AVAILCPU |
| 45 | mib[1] = HW_AVAILCPU; |
| 46 | len = sizeof(cpucount); |
| 47 | if (!sysctl(mib, 2, &cpucount, &len, NULL, 0)) |
| 48 | return cpucount; |
| 49 | # endif /* HW_AVAILCPU */ |
| 50 | mib[1] = HW_NCPU; |
| 51 | len = sizeof(cpucount); |
| 52 | if (!sysctl(mib, 2, &cpucount, &len, NULL, 0)) |
| 53 | return cpucount; |
| 54 | #endif /* defined(HAVE_BSD_SYSCTL) && defined(HW_NCPU) */ |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 55 | |
| 56 | #ifdef _SC_NPROCESSORS_ONLN |
| 57 | if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0) |
| 58 | return (int)ncpus; |
| 59 | #endif |
| 60 | |
| 61 | return 1; |
| 62 | } |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 63 | |
| 64 | int init_recursive_mutex(pthread_mutex_t *m) |
| 65 | { |
| 66 | pthread_mutexattr_t a; |
| 67 | int ret; |
| 68 | |
| 69 | ret = pthread_mutexattr_init(&a); |
| 70 | if (!ret) { |
| 71 | ret = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE); |
| 72 | if (!ret) |
| 73 | ret = pthread_mutex_init(m, &a); |
| 74 | pthread_mutexattr_destroy(&a); |
| 75 | } |
| 76 | return ret; |
| 77 | } |