Automatically determine the number of threads on linux
authorLauri Kasanen <curaga@operamail.com>
Sat, 3 Mar 2012 10:44:50 +0000 (12:44 +0200)
committerRudolf Polzer <divverent@alientrap.org>
Sat, 3 Mar 2012 16:28:34 +0000 (17:28 +0100)
Signed-off-by: Lauri Kasanen <curaga@operamail.com>
tools/quake3/common/threads.c

index 7f404a4f03da537b6b90f33c09647ca65139f2a3..1c8e7e758bbe02ad9ecb4112f9561cf19ffcc3cd 100644 (file)
@@ -426,17 +426,26 @@ void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
 #if defined(__linux__) || (defined(__APPLE__) && !MAC_STATIC_HACK)
 #define USED
 
-int numthreads = 4;
+#include <unistd.h>
+
+int numthreads = -1;
 
 void ThreadSetDefault (void)
 {
        if (numthreads == -1)   // not set manually
        {
-    /* default to one thread, only multi-thread when specifically told to */
-               numthreads = 1;
+#ifdef _SC_NPROCESSORS_CONF
+               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)
-    Sys_Printf("threads: %d\n", numthreads);
+
+       if(numthreads > 1)
+               Sys_Printf("threads: %d\n", numthreads);
 }
 
 #include <pthread.h>