Mercurial > hg > openttd
changeset 18634:45e8cda75fba draft
(svn r23481) -Add: Function to get the CPU core count.
author | michi_cc <michi_cc@openttd.org> |
---|---|
date | Sat, 10 Dec 2011 16:54:41 +0000 |
parents | b99ace5e6811 |
children | 4e8238f64e46 |
files | src/os/macosx/macos.mm src/os/os2/os2.cpp src/os/unix/unix.cpp src/os/windows/win32.cpp src/thread/thread.h |
diffstat | 5 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/os/macosx/macos.mm +++ b/src/os/macosx/macos.mm @@ -172,3 +172,20 @@ return true; } #endif + +uint GetCPUCoreCount() +{ + uint count = 1; +#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) + if (MacOSVersionIsAtLeast(10, 5, 0)) { + count = [ [ NSProcessInfo processInfo ] activeProcessorCount ]; + } else +#endif + { +#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) + count = MPProcessorsScheduled(); +#endif + } + + return count; +}
--- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -210,3 +210,8 @@ const char *FS2OTTD(const char *name) {return name;} const char *OTTD2FS(const char *name) {return name;} + +uint GetCPUCoreCount() +{ + return 1; +}
--- 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 @@ 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