From a4b30a4eed17aae8771fb21821e9658c93126d76 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Mon, 3 Nov 2014 09:10:18 +0000 Subject: [PATCH] add new numcpus field to the quakeparms structure and detect it during Sys_Init time git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1138 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/quakedef.h | 1 + Quake/sys_sdl_unix.c | 84 ++++++++++++++++++++++++++++++++++++++++++++ Quake/sys_sdl_win.c | 9 ++++- 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Quake/quakedef.h b/Quake/quakedef.h index 8f3da6fb..3051f011 100644 --- a/Quake/quakedef.h +++ b/Quake/quakedef.h @@ -193,6 +193,7 @@ typedef struct char **argv; void *membase; int memsize; + int numcpus; } quakeparms_t; #include "common.h" diff --git a/Quake/sys_sdl_unix.c b/Quake/sys_sdl_unix.c index aa0056c1..b86858f6 100644 --- a/Quake/sys_sdl_unix.c +++ b/Quake/sys_sdl_unix.c @@ -155,6 +155,88 @@ int Sys_FileTime (const char *path) return -1; } + +#if defined(__linux__) || defined(__sun) || defined(sun) || defined(_AIX) +static int Sys_NumCPUs (void) +{ + int numcpus = sysconf(_SC_NPROCESSORS_ONLN); + return (numcpus < 1) ? 1 : numcpus; +} + +#elif defined(PLATFORM_OSX) +#include +#if !defined(HW_AVAILCPU) /* using an ancient SDK? */ +#define HW_AVAILCPU 25 /* needs >= 10.2 */ +#endif +static int Sys_NumCPUs (void) +{ + int numcpus; + int mib[2]; + size_t len; + +#if defined(_SC_NPROCESSORS_ONLN) /* needs >= 10.5 */ + numcpus = sysconf(_SC_NPROCESSORS_ONLN); + if (numcpus != -1) + return (numcpus < 1) ? 1 : numcpus; +#endif + len = sizeof(numcpus); + mib[0] = CTL_HW; + mib[1] = HW_AVAILCPU; + sysctl(mib, 2, &numcpus, &len, NULL, 0); + if (sysctl(mib, 2, &numcpus, &len, NULL, 0) == -1) + { + mib[1] = HW_NCPU; + if (sysctl(mib, 2, &numcpus, &len, NULL, 0) == -1) + return 1; + } + return (numcpus < 1) ? 1 : numcpus; +} + +#elif defined(__sgi) || defined(sgi) || defined(__sgi__) /* IRIX */ +static int Sys_NumCPUs (void) +{ + int numcpus = sysconf(_SC_NPROC_ONLN); + if (numcpus < 1) + numcpus = 1; + return numcpus; +} + +#elif defined(PLATFORM_BSD) +#include +static int Sys_NumCPUs (void) +{ + int numcpus; + int mib[2]; + size_t len; + +#if defined(_SC_NPROCESSORS_ONLN) + numcpus = sysconf(_SC_NPROCESSORS_ONLN); + if (numcpus != -1) + return (numcpus < 1) ? 1 : numcpus; +#endif + len = sizeof(numcpus); + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + if (sysctl(mib, 2, &numcpus, &len, NULL, 0) == -1) + return 1; + return (numcpus < 1) ? 1 : numcpus; +} + +#elif defined(__hpux) || defined(__hpux__) || defined(_hpux) +#include +static int Sys_NumCPUs (void) +{ + int numcpus = mpctl(MPC_GETNUMSPUS, NULL, NULL); + return numcpus; +} + +#else /* unknown OS */ +static int Sys_NumCPUs (void) +{ + return -2; +} +#endif + static char cwd[MAX_OSPATH]; #ifdef DO_USERDIRS static char userdir[MAX_OSPATH]; @@ -265,6 +347,8 @@ void Sys_Init (void) Sys_mkdir (userdir); host_parms->userdir = userdir; #endif + host_parms->numcpus = Sys_NumCPUs (); + Sys_Printf("Detected %d CPUs.\n", host_parms->numcpus); } void Sys_mkdir (const char *path) diff --git a/Quake/sys_sdl_win.c b/Quake/sys_sdl_win.c index 2fbc3af5..ca91fb2c 100644 --- a/Quake/sys_sdl_win.c +++ b/Quake/sys_sdl_win.c @@ -177,7 +177,7 @@ typedef enum { dpi_unaware = 0, dpi_system_aware = 1, dpi_monitor_aware = 2 } dp typedef BOOL(*SetProcessDPIAwareFunc)(); typedef HRESULT(*SetProcessDPIAwarenessFunc)(dpi_awareness value); -void Sys_SetDPIAware (void) +static void Sys_SetDPIAware (void) { HMODULE hUser32, hShcore; SetProcessDPIAwarenessFunc setDPIAwareness; @@ -231,13 +231,19 @@ void Sys_Init (void) if (vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT) { + SYSTEM_INFO info; WinNT = true; if (vinfo.dwMajorVersion >= 6) WinVista = true; + GetSystemInfo(&info); + host_parms->numcpus = info.dwNumberOfProcessors; + if (host_parms->numcpus < 1) + host_parms->numcpus = 1; } else { WinNT = false; /* Win9x or WinME */ + host_parms->numcpus = 1; if ((vinfo.dwMajorVersion == 4) && (vinfo.dwMinorVersion == 0)) { Win95 = true; @@ -246,6 +252,7 @@ void Sys_Init (void) Win95old = true; } } + Sys_Printf("Detected %d CPUs.\n", host_parms->numcpus); if (isDedicated) {