2011-09-27 18:12:32 +00:00
|
|
|
|
// This code is in the public domain -- Ignacio Casta<74>o <castano@gmail.com>
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
|
|
#include "nvthread.h"
|
|
|
|
|
|
|
|
|
|
#include "Thread.h"
|
|
|
|
|
|
2011-09-27 18:12:32 +00:00
|
|
|
|
#if NV_OS_WIN32
|
2013-06-07 17:53:55 +00:00
|
|
|
|
#include "Win32.h"
|
2011-09-27 18:12:32 +00:00
|
|
|
|
#elif NV_OS_UNIX
|
2013-06-07 17:53:55 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
#include <unistd.h>
|
2013-02-05 07:17:25 +00:00
|
|
|
|
#elif NV_OS_DARWIN
|
2013-06-07 17:53:55 +00:00
|
|
|
|
#import <stdio.h>
|
|
|
|
|
#import <string.h>
|
|
|
|
|
#import <mach/mach_host.h>
|
|
|
|
|
#import <sys/sysctl.h>
|
|
|
|
|
|
|
|
|
|
//#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <syslog.h>
|
2012-02-14 16:35:42 +00:00
|
|
|
|
#endif
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find the number of cores in the system.
|
|
|
|
|
// Based on: http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
|
|
|
|
|
// @@ Distinguish between logical and physical cores?
|
|
|
|
|
uint nv::hardwareThreadCount() {
|
|
|
|
|
#if NV_OS_WIN32
|
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
|
GetSystemInfo( &sysinfo );
|
|
|
|
|
return sysinfo.dwNumberOfProcessors;
|
|
|
|
|
#elif NV_OS_XBOX
|
|
|
|
|
return 3; // or 6?
|
|
|
|
|
#elif NV_OS_LINUX // Linux, Solaris, & AIX
|
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
2012-09-11 16:41:02 +00:00
|
|
|
|
#elif NV_OS_DARWIN || NV_OS_FREEBSD || NV_OS_OPENBSD
|
2011-09-27 17:48:46 +00:00
|
|
|
|
int numCPU;
|
|
|
|
|
int mib[4];
|
2013-02-05 07:17:25 +00:00
|
|
|
|
size_t len = sizeof(numCPU);
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
|
|
// set the mib for hw.ncpu
|
|
|
|
|
mib[0] = CTL_HW;
|
2012-09-11 16:41:02 +00:00
|
|
|
|
|
|
|
|
|
#if NV_OS_OPENBSD
|
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
|
#else
|
|
|
|
|
mib[1] = HW_AVAILCPU;
|
|
|
|
|
#endif
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
|
|
// get the number of CPUs from the system
|
|
|
|
|
sysctl(mib, 2, &numCPU, &len, NULL, 0);
|
|
|
|
|
|
|
|
|
|
if (numCPU < 1) {
|
2011-09-28 01:45:08 +00:00
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
|
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
2011-09-28 01:45:08 +00:00
|
|
|
|
if (numCPU < 1) {
|
|
|
|
|
return 1; // Assume single core.
|
|
|
|
|
}
|
2011-09-27 17:48:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numCPU;
|
|
|
|
|
#else
|
|
|
|
|
return 1; // Assume single core.
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|