Automatically determine the number of threads on linux

Signed-off-by: Lauri Kasanen <curaga@operamail.com>
This commit is contained in:
Lauri Kasanen 2012-03-03 12:44:50 +02:00 committed by Rudolf Polzer
parent f02a56a694
commit 053ca4041b

View File

@ -426,15 +426,24 @@ void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
#if defined(__linux__) || (defined(__APPLE__) && !MAC_STATIC_HACK) #if defined(__linux__) || (defined(__APPLE__) && !MAC_STATIC_HACK)
#define USED #define USED
int numthreads = 4; #include <unistd.h>
int numthreads = -1;
void ThreadSetDefault (void) void ThreadSetDefault (void)
{ {
if (numthreads == -1) // not set manually if (numthreads == -1) // not set manually
{ {
/* default to one thread, only multi-thread when specifically told to */ #ifdef _SC_NPROCESSORS_CONF
numthreads = 1; long cpus = sysconf(_SC_NPROCESSORS_CONF);
if (cpus > 0)
numthreads = cpus;
else
#endif
/* can't detect, so default to four threads */
numthreads = 4;
} }
if(numthreads > 1) if(numthreads > 1)
Sys_Printf("threads: %d\n", numthreads); Sys_Printf("threads: %d\n", numthreads);
} }