Andreas Ericsson | 833e3df | 2008-02-22 20:11:56 -0600 | [diff] [blame] | 1 | #include "cache.h" |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 2 | #include <pthread.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 | |
| 27 | #ifdef _WIN32 |
| 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; |
| 38 | #endif |
| 39 | |
| 40 | #ifdef _SC_NPROCESSORS_ONLN |
| 41 | if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0) |
| 42 | return (int)ncpus; |
| 43 | #endif |
| 44 | |
| 45 | return 1; |
| 46 | } |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 47 | |
| 48 | int init_recursive_mutex(pthread_mutex_t *m) |
| 49 | { |
| 50 | pthread_mutexattr_t a; |
| 51 | int ret; |
| 52 | |
| 53 | ret = pthread_mutexattr_init(&a); |
| 54 | if (!ret) { |
| 55 | ret = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE); |
| 56 | if (!ret) |
| 57 | ret = pthread_mutex_init(m, &a); |
| 58 | pthread_mutexattr_destroy(&a); |
| 59 | } |
| 60 | return ret; |
| 61 | } |