nvidia-texture-tools/src/nvthread/nvthread.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

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
2011-09-28 01:45:08 +00:00
#include "Win32.h"
2011-09-27 18:12:32 +00:00
#elif NV_OS_UNIX
2011-09-28 01:45:08 +00:00
#include <sys/types.h>
#include <sys/sysctl.h>
2012-03-01 03:48:34 +00:00
#include <unistd.h>
2012-02-14 16:35:42 +00:00
#elif NV_OS_DARWIN
#import <stdio.h>
#import <string.h>
#import <mach/mach_host.h>
#import <sys/sysctl.h>
2011-09-27 18:12:32 +00:00
2012-02-14 16:35:42 +00:00
#include <CoreFoundation/CoreFoundation.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#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);
#elif NV_OS_DARWIN || NV_OS_FREEBSD
int numCPU;
int mib[4];
size_t len = sizeof(numCPU);
// set the mib for hw.ncpu
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
// 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
}