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 | { |
Nguyễn Thái Ngọc Duy | 0ec7935 | 2018-10-27 19:29:59 +0200 | [diff] [blame] | 23 | #ifdef NO_PTHREADS |
| 24 | return 1; |
| 25 | #else |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 26 | #ifdef _SC_NPROCESSORS_ONLN |
| 27 | long ncpus; |
| 28 | #endif |
| 29 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 30 | #ifdef GIT_WINDOWS_NATIVE |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 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; |
Kyle J. McKay | a25b5a3 | 2015-03-07 23:14:37 -0800 | [diff] [blame] | 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) */ |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 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; |
Nguyễn Thái Ngọc Duy | 0ec7935 | 2018-10-27 19:29:59 +0200 | [diff] [blame] | 65 | #endif |
Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 66 | } |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 67 | |
| 68 | int init_recursive_mutex(pthread_mutex_t *m) |
| 69 | { |
Nguyễn Thái Ngọc Duy | 0ec7935 | 2018-10-27 19:29:59 +0200 | [diff] [blame] | 70 | #ifndef NO_PTHREADS |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 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; |
Nguyễn Thái Ngọc Duy | 0ec7935 | 2018-10-27 19:29:59 +0200 | [diff] [blame] | 82 | #else |
| 83 | return 0; |
| 84 | #endif |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 85 | } |
Nguyễn Thái Ngọc Duy | 0ec7935 | 2018-10-27 19:29:59 +0200 | [diff] [blame] | 86 | |
| 87 | #ifdef NO_PTHREADS |
| 88 | int 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 | |
| 100 | int 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 | |
| 114 | int 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 |