comparison src/cpuinfo/SDL_cpuinfo.c @ 3586:b6758aee0dd4

Added support for querying the number of CPUs available on Linux. This also happens to work on Mac OS X.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 17 Dec 2009 03:04:04 +0000
parents 41d01d70659c
children 2080e8d75ac6
comparison
equal deleted inserted replaced
3585:f8816ffa210b 3586:b6758aee0dd4
23 23
24 /* CPU feature detection for SDL */ 24 /* CPU feature detection for SDL */
25 25
26 #include "SDL_cpuinfo.h" 26 #include "SDL_cpuinfo.h"
27 27
28 #ifdef HAVE_SYSCONF
29 #include <unistd.h>
30 #endif
28 #ifdef HAVE_SYSCTLBYNAME 31 #ifdef HAVE_SYSCTLBYNAME
29 #include <sys/types.h> 32 #include <sys/types.h>
30 #include <sys/sysctl.h> 33 #include <sys/sysctl.h>
31 #endif 34 #endif
32 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__)) 35 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
295 298
296 int 299 int
297 SDL_GetCPUCount() 300 SDL_GetCPUCount()
298 { 301 {
299 if (!SDL_CPUCount) { 302 if (!SDL_CPUCount) {
303 #ifdef HAVE_SYSCONF
304 if (SDL_CPUCount <= 0) {
305 SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
306 }
307 #endif
300 #ifdef HAVE_SYSCTLBYNAME 308 #ifdef HAVE_SYSCTLBYNAME
301 { 309 if (SDL_CPUCount <= 0) {
302 size_t size = sizeof(SDL_CPUCount); 310 size_t size = sizeof(SDL_CPUCount);
303 sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0); 311 sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
304 } 312 }
305 #endif 313 #endif
306 #ifdef __WIN32__ 314 #ifdef __WIN32__
307 { 315 if (SDL_CPUCount <= 0) {
308 SYSTEM_INFO info; 316 SYSTEM_INFO info;
309 GetSystemInfo(&info); 317 GetSystemInfo(&info);
310 SDL_CPUCount = info.dwNumberOfProcessors; 318 SDL_CPUCount = info.dwNumberOfProcessors;
311 } 319 }
312 #endif 320 #endif
313 /* There has to be at least 1, right? :) */ 321 /* There has to be at least 1, right? :) */
314 if (!SDL_CPUCount) { 322 if (SDL_CPUCount <= 0) {
315 SDL_CPUCount = 1; 323 SDL_CPUCount = 1;
316 } 324 }
317 } 325 }
318 return SDL_CPUCount; 326 return SDL_CPUCount;
319 } 327 }