Files
third_party_rust_num_cpus/extern/num_cpus.c
T
Sean McArthur 1aeb70231f v0.1.0
2015-03-16 11:42:22 -07:00

48 lines
1.0 KiB
C

#if !defined(__WIN32__)
#include <unistd.h>
#else
#include <windows.h>
#endif
static int
num_cpus() {
#if defined(__WIN32__)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return (int) sysinfo.dwNumberOfProcessors;
#elif defined(__BSD__)
/* swiped from http://stackoverflow.com/questions/150355/
* programmatically-find-the-number-of-cores-on-a-machine */
unsigned 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 ) {
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 ) {
numCPU = 1;
}
}
return numCPU;
#elif defined(__GNUC__)
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
int
crates_io_get_num_cpus() {
return num_cpus();
}