diff options
author | michi_cc <michi_cc@openttd.org> | 2011-12-10 16:54:41 +0000 |
---|---|---|
committer | michi_cc <michi_cc@openttd.org> | 2011-12-10 16:54:41 +0000 |
commit | a0f3649c1abe7b831f083366e2fd91206bf23091 (patch) | |
tree | 5a352c430013c682e93d1a1b85960182cd18fbac /src/os/unix | |
parent | 0ca25fb3af8fbf00607df120149b27aa7a763edb (diff) | |
download | openttd-a0f3649c1abe7b831f083366e2fd91206bf23091.tar.xz |
(svn r23481) -Add: Function to get the CPU core count.
Diffstat (limited to 'src/os/unix')
-rw-r--r-- | src/os/unix/unix.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 371beb226..700a9050c 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -28,10 +28,18 @@ #define HAS_STATVFS #endif +#if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__) + #define HAS_SYSCTL +#endif + #ifdef HAS_STATVFS #include <sys/statvfs.h> #endif +#ifdef HAS_SYSCTL +#include <sys/sysctl.h> +#endif + #ifdef __MORPHOS__ #include <exec/types.h> @@ -318,3 +326,26 @@ void CSleep(int milliseconds) usleep(milliseconds * 1000); #endif } + + +#ifndef __APPLE__ +uint GetCPUCoreCount() +{ + uint count = 1; +#ifdef HAS_SYSCTL + int ncpu = 0; + size_t len = sizeof(ncpu); + + if (sysctlbyname("hw.availcpu", &ncpu, &len, NULL, 0) < 0) { + sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0); + } + + if (ncpu > 0) count = ncpu; +#elif defined(_SC_NPROCESSORS_ONLN) + long res = sysconf(_SC_NPROCESSORS_ONLN); + if (res > 0) count = res; +#endif + + return count; +} +#endif |