]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/threads.c
query the online, not total, CPUs in case some were shut off
[xonotic/netradiant.git] / tools / quake3 / common / threads.c
index a486abafaea5006e169ece64f156579523260734..b50f6d6071790e95e67a2e06a62b76c5b2d2153b 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_ONLN
+               long cpus = sysconf(_SC_NPROCESSORS_ONLN);
+               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>
@@ -565,7 +574,7 @@ void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
     for (i=0 ; i<numthreads ; i++)
     {
       /* Default pthread attributes: joinable & non-realtime scheduling */
-      if(pthread_create(&work_threads[i], &attr, (void*)func, (void*)i) != 0)
+      if(pthread_create(&work_threads[i], &attr, (void*)func, (void*)(size_t)i) != 0)
         Error("pthread_create failed");
     }
     for (i=0 ; i<numthreads ; i++)