]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/threads.c
some fixes to issues pointed by @tkoeppe
[xonotic/netradiant.git] / tools / quake3 / common / threads.c
index 7b66e8b81bee91a0ef840f957bb46709c5fbd304..054dc746625ef214d0b73ecc65793160db865cc6 100644 (file)
@@ -19,7 +19,8 @@
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#ifndef _WIN32
+#include "globaldefs.h"
+#if !GDEF_OS_WINDOWS
 // The below define is necessary to use
 // pthreads extensions like pthread_mutexattr_settype
 #define _GNU_SOURCE
@@ -57,11 +58,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 */
                }
        }
@@ -106,7 +117,7 @@ void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )(
 
    ===================================================================
  */
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
 
 #define USED
 
@@ -414,16 +425,26 @@ void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
    =======================================================================
  */
 
-#ifdef __linux__
+#if GDEF_OS_LINUX || ( GDEF_OS_MACOS && !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 );
        }
@@ -513,11 +534,12 @@ void recursive_mutex_init( pthread_mutexattr_t attribs ){
  */
 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;
-       void *exit_value;
 
        start     = I_FloatTime();
        pacifier  = showpacifier;
@@ -526,6 +548,13 @@ void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
        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 );
        }
@@ -540,24 +569,21 @@ void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
                if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
                        Error( "pthread_mutexattr_init failed" );
                }
-#if __GLIBC_MINOR__ == 1
-               if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_FAST_NP ) != 0 )
-#else
-               if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ADAPTIVE_NP ) != 0 )
-#endif
-               { Error( "pthread_mutexattr_settype failed" ); }
+               if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
+                       Error( "pthread_mutexattr_settype failed" );
+               }
                recursive_mutex_init( mattrib );
 
                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 *(*)(void *)) func, (void*)(uintptr_t)i ) != 0 ) {
                                Error( "pthread_create failed" );
                        }
                }
                for ( i = 0 ; i < numthreads ; i++ )
                {
-                       if ( pthread_join( work_threads[i], &exit_value ) != 0 ) {
+                       if ( pthread_join( work_threads[i], NULL ) != 0 ) {
                                Error( "pthread_join failed" );
                        }
                }