]> de.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 8a5fa35a7915f1dc3f6da7158ef994e3e6ae9a00..b50f6d6071790e95e67a2e06a62b76c5b2d2153b 100644 (file)
@@ -59,13 +59,21 @@ int GetThreadWork (void)
                return -1;
        }
 
-       f = 10*dispatch / workcount;
-       if (f != oldf)
+       f = 40*dispatch / workcount;
+       if(f < oldf)
        {
+               Sys_Printf("warning: progress went backwards (should never happen)\n");
                oldf = f;
+       }
+       while(f > oldf)
+       {
+               ++oldf;
                if (pacifier)
                {
-                       Sys_Printf ("%i...", f);
+                       if(oldf % 4 == 0)
+                               Sys_Printf("%i", f / 4);
+                       else
+                               Sys_Printf (".");
                        fflush( stdout );       /* ydnar */
                }
        }
@@ -415,20 +423,29 @@ void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
 =======================================================================
 */
 
-#if defined(__linux__) || defined(__APPLE__)
+#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>
@@ -517,7 +534,9 @@ RunThreadsOn
 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
 {
   pthread_mutexattr_t         mattrib;
+  pthread_attr_t              attr;
   pthread_t work_threads[MAX_THREADS];
+  size_t stacksize;
   
   int    start, end;
   int   i=0, status=0;
@@ -528,6 +547,14 @@ void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
   dispatch  = 0;
   oldf      = -1;
   workcount = workcnt;
+
+  pthread_attr_init(&attr);
+  if(pthread_attr_setstacksize(&attr, 8388608) != 0)
+  {
+         stacksize = 0;
+         pthread_attr_getstacksize(&attr, &stacksize);
+         Sys_Printf("Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0);
+  }
   
   if(numthreads == 1)
     func(0);
@@ -547,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], NULL, (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++)