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/unix/unix.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/os/unix/unix.cpp') 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 -- cgit v1.2.3-70-g09d2