From a0f3649c1abe7b831f083366e2fd91206bf23091 Mon Sep 17 00:00:00 2001 From: michi_cc Date: Sat, 10 Dec 2011 16:54:41 +0000 Subject: (svn r23481) -Add: Function to get the CPU core count. --- src/os/macosx/macos.mm | 17 +++++++++++++++++ src/os/os2/os2.cpp | 5 +++++ src/os/unix/unix.cpp | 31 +++++++++++++++++++++++++++++++ src/os/windows/win32.cpp | 8 ++++++++ 4 files changed, 61 insertions(+) (limited to 'src/os') diff --git a/src/os/macosx/macos.mm b/src/os/macosx/macos.mm index f6b2d4e89..7c5092403 100644 --- a/src/os/macosx/macos.mm +++ b/src/os/macosx/macos.mm @@ -172,3 +172,20 @@ bool GetClipboardContents(char *buffer, size_t buff_len) 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; +} diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index bef44d4f5..21fd0c78b 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -210,3 +210,8 @@ void CSleep(int milliseconds) const char *FS2OTTD(const char *name) {return name;} const char *OTTD2FS(const char *name) {return name;} + +uint GetCPUCoreCount() +{ + return 1; +} 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 #endif +#ifdef HAS_SYSCTL +#include +#endif + #ifdef __MORPHOS__ #include @@ -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 diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 39203cbca..6c7a3ec68 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -753,3 +753,11 @@ const char *GetCurrentLocale(const char *) static char retbuf[6] = {lang[0], lang[1], '_', country[0], country[1], 0}; return retbuf; } + +uint GetCPUCoreCount() +{ + SYSTEM_INFO info; + + GetSystemInfo(&info); + return info.dwNumberOfProcessors; +} -- cgit v1.2.3-54-g00ecf