-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-\r
-#ifndef _WIN32\r
-// The below define is necessary to use\r
-// pthreads extensions like pthread_mutexattr_settype\r
-#define _GNU_SOURCE\r
-#include <pthread.h>\r
-#endif\r
-\r
-#include "cmdlib.h"\r
-#include "mathlib.h"\r
-#include "inout.h"\r
-#include "qthreads.h"\r
-\r
-#define MAX_THREADS 64\r
-\r
-int dispatch;\r
-int workcount;\r
-int oldf;\r
-qboolean pacifier;\r
-\r
-qboolean threaded;\r
-\r
-/*\r
-=============\r
-GetThreadWork\r
-\r
-=============\r
-*/\r
-int GetThreadWork (void)\r
-{\r
- int r;\r
- int f;\r
-\r
- ThreadLock ();\r
-\r
- if (dispatch == workcount)\r
- {\r
- ThreadUnlock ();\r
- return -1;\r
- }\r
-\r
- f = 10*dispatch / workcount;\r
- if (f != oldf)\r
- {\r
- oldf = f;\r
- if (pacifier)\r
- {\r
- Sys_Printf ("%i...", f);\r
- fflush( stdout ); /* ydnar */\r
- }\r
- }\r
-\r
- r = dispatch;\r
- dispatch++;\r
- ThreadUnlock ();\r
-\r
- return r;\r
-}\r
-\r
-\r
-void (*workfunction) (int);\r
-\r
-void ThreadWorkerFunction (int threadnum)\r
-{\r
- int work;\r
-\r
- while (1)\r
- {\r
- work = GetThreadWork ();\r
- if (work == -1)\r
- break;\r
-//Sys_Printf ("thread %i, work %i\n", threadnum, work);\r
- workfunction(work);\r
- }\r
-}\r
-\r
-void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- if (numthreads == -1)\r
- ThreadSetDefault ();\r
- workfunction = func;\r
- RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);\r
-}\r
-\r
-\r
-/*\r
-===================================================================\r
-\r
-WIN32\r
-\r
-===================================================================\r
-*/\r
-#ifdef _WIN32\r
-\r
-#define USED\r
-\r
-#include <windows.h>\r
-\r
-int numthreads = -1;\r
-CRITICAL_SECTION crit;\r
-static int enter;\r
-\r
-void ThreadSetDefault (void)\r
-{\r
- SYSTEM_INFO info;\r
-\r
- if (numthreads == -1) // not set manually\r
- {\r
- GetSystemInfo (&info);\r
- numthreads = info.dwNumberOfProcessors;\r
- if (numthreads < 1 || numthreads > 32)\r
- numthreads = 1;\r
- }\r
-\r
- Sys_Printf ("%i threads\n", numthreads);\r
-}\r
-\r
-\r
-void ThreadLock (void)\r
-{\r
- if (!threaded)\r
- return;\r
- EnterCriticalSection (&crit);\r
- if (enter)\r
- Error ("Recursive ThreadLock\n");\r
- enter = 1;\r
-}\r
-\r
-void ThreadUnlock (void)\r
-{\r
- if (!threaded)\r
- return;\r
- if (!enter)\r
- Error ("ThreadUnlock without lock\n");\r
- enter = 0;\r
- LeaveCriticalSection (&crit);\r
-}\r
-\r
-/*\r
-=============\r
-RunThreadsOn\r
-=============\r
-*/\r
-void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- int threadid[MAX_THREADS];\r
- HANDLE threadhandle[MAX_THREADS];\r
- int i;\r
- int start, end;\r
-\r
- start = I_FloatTime ();\r
- dispatch = 0;\r
- workcount = workcnt;\r
- oldf = -1;\r
- pacifier = showpacifier;\r
- threaded = qtrue;\r
-\r
- //\r
- // run threads in parallel\r
- //\r
- InitializeCriticalSection (&crit);\r
-\r
- if (numthreads == 1)\r
- { // use same thread\r
- func (0);\r
- }\r
- else\r
- {\r
- for (i=0 ; i<numthreads ; i++)\r
- {\r
- threadhandle[i] = CreateThread(\r
- NULL, // LPSECURITY_ATTRIBUTES lpsa,\r
- //0, // DWORD cbStack,\r
-\r
- /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */\r
- (4096 * 1024),\r
-\r
- (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,\r
- (LPVOID)i, // LPVOID lpvThreadParm,\r
- 0, // DWORD fdwCreate,\r
- &threadid[i]);\r
- }\r
-\r
- for (i=0 ; i<numthreads ; i++)\r
- WaitForSingleObject (threadhandle[i], INFINITE);\r
- }\r
- DeleteCriticalSection (&crit);\r
-\r
- threaded = qfalse;\r
- end = I_FloatTime ();\r
- if (pacifier)\r
- Sys_Printf (" (%i)\n", end-start);\r
-}\r
-\r
-\r
-#endif\r
-\r
-/*\r
-===================================================================\r
-\r
-OSF1\r
-\r
-===================================================================\r
-*/\r
-\r
-#ifdef __osf__\r
-#define USED\r
-\r
-int numthreads = 4;\r
-\r
-void ThreadSetDefault (void)\r
-{\r
- if (numthreads == -1) // not set manually\r
- {\r
- numthreads = 4;\r
- }\r
-}\r
-\r
-\r
-#include <pthread.h>\r
-\r
-pthread_mutex_t *my_mutex;\r
-\r
-void ThreadLock (void)\r
-{\r
- if (my_mutex)\r
- pthread_mutex_lock (my_mutex);\r
-}\r
-\r
-void ThreadUnlock (void)\r
-{\r
- if (my_mutex)\r
- pthread_mutex_unlock (my_mutex);\r
-}\r
-\r
-\r
-/*\r
-=============\r
-RunThreadsOn\r
-=============\r
-*/\r
-void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- int i;\r
- pthread_t work_threads[MAX_THREADS];\r
- pthread_addr_t status;\r
- pthread_attr_t attrib;\r
- pthread_mutexattr_t mattrib;\r
- int start, end;\r
-\r
- start = I_FloatTime ();\r
- dispatch = 0;\r
- workcount = workcnt;\r
- oldf = -1;\r
- pacifier = showpacifier;\r
- threaded = qtrue;\r
-\r
- if (pacifier)\r
- setbuf (stdout, NULL);\r
-\r
- if (!my_mutex)\r
- {\r
- my_mutex = safe_malloc (sizeof(*my_mutex));\r
- if (pthread_mutexattr_create (&mattrib) == -1)\r
- Error ("pthread_mutex_attr_create failed");\r
- if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)\r
- Error ("pthread_mutexattr_setkind_np failed");\r
- if (pthread_mutex_init (my_mutex, mattrib) == -1)\r
- Error ("pthread_mutex_init failed");\r
- }\r
-\r
- if (pthread_attr_create (&attrib) == -1)\r
- Error ("pthread_attr_create failed");\r
- if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)\r
- Error ("pthread_attr_setstacksize failed");\r
- \r
- for (i=0 ; i<numthreads ; i++)\r
- {\r
- if (pthread_create(&work_threads[i], attrib\r
- , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)\r
- Error ("pthread_create failed");\r
- }\r
- \r
- for (i=0 ; i<numthreads ; i++)\r
- {\r
- if (pthread_join (work_threads[i], &status) == -1)\r
- Error ("pthread_join failed");\r
- }\r
-\r
- threaded = qfalse;\r
-\r
- end = I_FloatTime ();\r
- if (pacifier)\r
- Sys_Printf (" (%i)\n", end-start);\r
-}\r
-\r
-\r
-#endif\r
-\r
-/*\r
-===================================================================\r
-\r
-IRIX\r
-\r
-===================================================================\r
-*/\r
-\r
-#ifdef _MIPS_ISA \r
-#define USED\r
-\r
-#include <task.h>\r
-#include <abi_mutex.h>\r
-#include <sys/types.h>\r
-#include <sys/prctl.h>\r
-\r
-\r
-int numthreads = -1;\r
-abilock_t lck;\r
-\r
-void ThreadSetDefault (void)\r
-{\r
- if (numthreads == -1)\r
- numthreads = prctl(PR_MAXPPROCS);\r
- Sys_Printf ("%i threads\n", numthreads);\r
- usconfig (CONF_INITUSERS, numthreads);\r
-}\r
-\r
-\r
-void ThreadLock (void)\r
-{\r
- spin_lock (&lck);\r
-}\r
-\r
-void ThreadUnlock (void)\r
-{\r
- release_lock (&lck);\r
-}\r
-\r
-\r
-/*\r
-=============\r
-RunThreadsOn\r
-=============\r
-*/\r
-void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- int i;\r
- int pid[MAX_THREADS];\r
- int start, end;\r
-\r
- start = I_FloatTime ();\r
- dispatch = 0;\r
- workcount = workcnt;\r
- oldf = -1;\r
- pacifier = showpacifier;\r
- threaded = qtrue;\r
-\r
- if (pacifier)\r
- setbuf (stdout, NULL);\r
-\r
- init_lock (&lck);\r
-\r
- for (i=0 ; i<numthreads-1 ; i++)\r
- {\r
- pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i\r
- , NULL, 0x200000); // 2 meg stacks\r
- if (pid[i] == -1)\r
- {\r
- perror ("sproc");\r
- Error ("sproc failed");\r
- }\r
- }\r
- \r
- func(i);\r
- \r
- for (i=0 ; i<numthreads-1 ; i++)\r
- wait (NULL);\r
-\r
- threaded = qfalse;\r
-\r
- end = I_FloatTime ();\r
- if (pacifier)\r
- Sys_Printf (" (%i)\n", end-start);\r
-}\r
-\r
-\r
-#endif\r
-\r
-\r
-/*\r
-=======================================================================\r
-\r
- Linux pthreads\r
-\r
-=======================================================================\r
-*/\r
-\r
-#ifdef __linux__\r
-#define USED\r
-\r
-int numthreads = 4;\r
-\r
-void ThreadSetDefault (void)\r
-{\r
- if (numthreads == -1) // not set manually\r
- {\r
- /* default to one thread, only multi-thread when specifically told to */\r
- numthreads = 1;\r
- }\r
- if(numthreads > 1)\r
- Sys_Printf("threads: %d\n", numthreads);\r
-}\r
-\r
-#include <pthread.h>\r
-\r
-typedef struct pt_mutex_s\r
-{\r
- pthread_t *owner;\r
- pthread_mutex_t a_mutex;\r
- pthread_cond_t cond;\r
- unsigned int lock;\r
-} pt_mutex_t;\r
-\r
-pt_mutex_t global_lock;\r
-\r
-void ThreadLock(void)\r
-{\r
- pt_mutex_t *pt_mutex = &global_lock;\r
-\r
- if(!threaded)\r
- return;\r
-\r
- pthread_mutex_lock(&pt_mutex->a_mutex);\r
- if(pthread_equal(pthread_self(), (pthread_t)&pt_mutex->owner)) \r
- pt_mutex->lock++;\r
- else\r
- {\r
- if((!pt_mutex->owner) && (pt_mutex->lock == 0))\r
- {\r
- pt_mutex->owner = (pthread_t *)pthread_self();\r
- pt_mutex->lock = 1;\r
- }\r
- else\r
- {\r
- while(1)\r
- {\r
- pthread_cond_wait(&pt_mutex->cond, &pt_mutex->a_mutex);\r
- if((!pt_mutex->owner) && (pt_mutex->lock == 0))\r
- {\r
- pt_mutex->owner = (pthread_t *)pthread_self();\r
- pt_mutex->lock = 1;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- pthread_mutex_unlock(&pt_mutex->a_mutex);\r
-}\r
-\r
-void ThreadUnlock(void)\r
-{\r
- pt_mutex_t *pt_mutex = &global_lock;\r
- \r
- if(!threaded)\r
- return;\r
-\r
- pthread_mutex_lock(&pt_mutex->a_mutex);\r
- pt_mutex->lock--;\r
- \r
- if(pt_mutex->lock == 0)\r
- {\r
- pt_mutex->owner = NULL;\r
- pthread_cond_signal(&pt_mutex->cond);\r
- }\r
- \r
- pthread_mutex_unlock(&pt_mutex->a_mutex);\r
-}\r
-\r
-void recursive_mutex_init(pthread_mutexattr_t attribs)\r
-{\r
- pt_mutex_t *pt_mutex = &global_lock;\r
- \r
- pt_mutex->owner = NULL;\r
- if(pthread_mutex_init(&pt_mutex->a_mutex, &attribs) != 0)\r
- Error("pthread_mutex_init failed\n");\r
- if(pthread_cond_init(&pt_mutex->cond, NULL) != 0)\r
- Error("pthread_cond_init failed\n");\r
- \r
- pt_mutex->lock = 0;\r
-}\r
-\r
-/*\r
-=============\r
-RunThreadsOn\r
-=============\r
-*/\r
-void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- pthread_mutexattr_t mattrib;\r
- pthread_t work_threads[MAX_THREADS];\r
- \r
- int start, end;\r
- int i=0, status=0;\r
- \r
- start = I_FloatTime ();\r
- pacifier = showpacifier;\r
- \r
- dispatch = 0;\r
- oldf = -1;\r
- workcount = workcnt;\r
- \r
- if(numthreads == 1)\r
- func(0);\r
- else\r
- { \r
- threaded = qtrue;\r
- \r
- if(pacifier)\r
- setbuf(stdout, NULL);\r
-\r
- if(pthread_mutexattr_init(&mattrib) != 0)\r
- Error("pthread_mutexattr_init failed");\r
-#if __GLIBC_MINOR__ == 1\r
- if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_FAST_NP) != 0)\r
-#else\r
- if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)\r
-#endif\r
- Error ("pthread_mutexattr_settype failed");\r
- recursive_mutex_init(mattrib);\r
-\r
- for (i=0 ; i<numthreads ; i++)\r
- {\r
- /* Default pthread attributes: joinable & non-realtime scheduling */\r
- if(pthread_create(&work_threads[i], NULL, (void*)func, (void*)i) != 0)\r
- Error("pthread_create failed");\r
- }\r
- for (i=0 ; i<numthreads ; i++)\r
- {\r
- if(pthread_join(work_threads[i], (void **)&status) != 0)\r
- Error("pthread_join failed");\r
- }\r
- pthread_mutexattr_destroy(&mattrib);\r
- threaded = qfalse;\r
- }\r
- \r
- end = I_FloatTime ();\r
- if (pacifier)\r
- Sys_Printf (" (%i)\n", end-start);\r
-}\r
-#endif // ifdef __linux__\r
-\r
-\r
-/*\r
-=======================================================================\r
-\r
- SINGLE THREAD\r
-\r
-=======================================================================\r
-*/\r
-\r
-#ifndef USED\r
-\r
-int numthreads = 1;\r
-\r
-void ThreadSetDefault (void)\r
-{\r
- numthreads = 1;\r
-}\r
-\r
-void ThreadLock (void)\r
-{\r
-}\r
-\r
-void ThreadUnlock (void)\r
-{\r
-}\r
-\r
-/*\r
-=============\r
-RunThreadsOn\r
-=============\r
-*/\r
-void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))\r
-{\r
- int i;\r
- int start, end;\r
-\r
- dispatch = 0;\r
- workcount = workcnt;\r
- oldf = -1;\r
- pacifier = showpacifier;\r
- start = I_FloatTime (); \r
- func(0);\r
-\r
- end = I_FloatTime ();\r
- if (pacifier)\r
- Sys_Printf (" (%i)\n", end-start);\r
-}\r
-\r
-#endif\r
+/*
+ Copyright (C) 1999-2007 id Software, Inc. and contributors.
+ For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+ This file is part of GtkRadiant.
+
+ GtkRadiant is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ GtkRadiant is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GtkRadiant; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _WIN32
+// The below define is necessary to use
+// pthreads extensions like pthread_mutexattr_settype
+#define _GNU_SOURCE
+#include <pthread.h>
+#endif
+
+#include "cmdlib.h"
+#include "mathlib.h"
+#include "inout.h"
+#include "qthreads.h"
+
+#define MAX_THREADS 64
+
+int dispatch;
+int workcount;
+int oldf;
+qboolean pacifier;
+
+qboolean threaded;
+
+/*
+ =============
+ GetThreadWork
+
+ =============
+ */
+int GetThreadWork( void ){
+ int r;
+ int f;
+
+ ThreadLock();
+
+ if ( dispatch == workcount ) {
+ ThreadUnlock();
+ return -1;
+ }
+
+ f = 10 * dispatch / workcount;
+ if ( f != oldf ) {
+ oldf = f;
+ if ( pacifier ) {
+ Sys_Printf( "%i...", f );
+ fflush( stdout ); /* ydnar */
+ }
+ }
+
+ r = dispatch;
+ dispatch++;
+ ThreadUnlock();
+
+ return r;
+}
+
+
+void ( *workfunction )( int );
+
+void ThreadWorkerFunction( int threadnum ){
+ int work;
+
+ while ( 1 )
+ {
+ work = GetThreadWork();
+ if ( work == -1 ) {
+ break;
+ }
+//Sys_Printf ("thread %i, work %i\n", threadnum, work);
+ workfunction( work );
+ }
+}
+
+void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ if ( numthreads == -1 ) {
+ ThreadSetDefault();
+ }
+ workfunction = func;
+ RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
+}
+
+
+/*
+ ===================================================================
+
+ WIN32
+
+ ===================================================================
+ */
+#ifdef WIN32
+
+#define USED
+
+#include <windows.h>
+
+int numthreads = -1;
+CRITICAL_SECTION crit;
+static int enter;
+
+void ThreadSetDefault( void ){
+ SYSTEM_INFO info;
+
+ if ( numthreads == -1 ) { // not set manually
+ GetSystemInfo( &info );
+ numthreads = info.dwNumberOfProcessors;
+ if ( numthreads < 1 || numthreads > 32 ) {
+ numthreads = 1;
+ }
+ }
+
+ Sys_Printf( "%i threads\n", numthreads );
+}
+
+
+void ThreadLock( void ){
+ if ( !threaded ) {
+ return;
+ }
+ EnterCriticalSection( &crit );
+ if ( enter ) {
+ Error( "Recursive ThreadLock\n" );
+ }
+ enter = 1;
+}
+
+void ThreadUnlock( void ){
+ if ( !threaded ) {
+ return;
+ }
+ if ( !enter ) {
+ Error( "ThreadUnlock without lock\n" );
+ }
+ enter = 0;
+ LeaveCriticalSection( &crit );
+}
+
+/*
+ =============
+ RunThreadsOn
+ =============
+ */
+void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ int threadid[MAX_THREADS];
+ HANDLE threadhandle[MAX_THREADS];
+ int i;
+ int start, end;
+
+ start = I_FloatTime();
+ dispatch = 0;
+ workcount = workcnt;
+ oldf = -1;
+ pacifier = showpacifier;
+ threaded = qtrue;
+
+ //
+ // run threads in parallel
+ //
+ InitializeCriticalSection( &crit );
+
+ if ( numthreads == 1 ) { // use same thread
+ func( 0 );
+ }
+ else
+ {
+ for ( i = 0 ; i < numthreads ; i++ )
+ {
+ threadhandle[i] = CreateThread(
+ NULL, // LPSECURITY_ATTRIBUTES lpsa,
+ //0, // DWORD cbStack,
+
+ /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
+ ( 4096 * 1024 ),
+
+ (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
+ (LPVOID)i, // LPVOID lpvThreadParm,
+ 0, // DWORD fdwCreate,
+ &threadid[i] );
+ }
+
+ for ( i = 0 ; i < numthreads ; i++ )
+ WaitForSingleObject( threadhandle[i], INFINITE );
+ }
+ DeleteCriticalSection( &crit );
+
+ threaded = qfalse;
+ end = I_FloatTime();
+ if ( pacifier ) {
+ Sys_Printf( " (%i)\n", end - start );
+ }
+}
+
+
+#endif
+
+/*
+ ===================================================================
+
+ OSF1
+
+ ===================================================================
+ */
+
+#ifdef __osf__
+#define USED
+
+int numthreads = 4;
+
+void ThreadSetDefault( void ){
+ if ( numthreads == -1 ) { // not set manually
+ numthreads = 4;
+ }
+}
+
+
+#include <pthread.h>
+
+pthread_mutex_t *my_mutex;
+
+void ThreadLock( void ){
+ if ( my_mutex ) {
+ pthread_mutex_lock( my_mutex );
+ }
+}
+
+void ThreadUnlock( void ){
+ if ( my_mutex ) {
+ pthread_mutex_unlock( my_mutex );
+ }
+}
+
+
+/*
+ =============
+ RunThreadsOn
+ =============
+ */
+void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ int i;
+ pthread_t work_threads[MAX_THREADS];
+ pthread_addr_t status;
+ pthread_attr_t attrib;
+ pthread_mutexattr_t mattrib;
+ int start, end;
+
+ start = I_FloatTime();
+ dispatch = 0;
+ workcount = workcnt;
+ oldf = -1;
+ pacifier = showpacifier;
+ threaded = qtrue;
+
+ if ( pacifier ) {
+ setbuf( stdout, NULL );
+ }
+
+ if ( !my_mutex ) {
+ my_mutex = safe_malloc( sizeof( *my_mutex ) );
+ if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
+ Error( "pthread_mutex_attr_create failed" );
+ }
+ if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
+ Error( "pthread_mutexattr_setkind_np failed" );
+ }
+ if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
+ Error( "pthread_mutex_init failed" );
+ }
+ }
+
+ if ( pthread_attr_create( &attrib ) == -1 ) {
+ Error( "pthread_attr_create failed" );
+ }
+ if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
+ Error( "pthread_attr_setstacksize failed" );
+ }
+
+ for ( i = 0 ; i < numthreads ; i++ )
+ {
+ if ( pthread_create( &work_threads[i], attrib
+ , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
+ Error( "pthread_create failed" );
+ }
+ }
+
+ for ( i = 0 ; i < numthreads ; i++ )
+ {
+ if ( pthread_join( work_threads[i], &status ) == -1 ) {
+ Error( "pthread_join failed" );
+ }
+ }
+
+ threaded = qfalse;
+
+ end = I_FloatTime();
+ if ( pacifier ) {
+ Sys_Printf( " (%i)\n", end - start );
+ }
+}
+
+
+#endif
+
+/*
+ ===================================================================
+
+ IRIX
+
+ ===================================================================
+ */
+
+#ifdef _MIPS_ISA
+#define USED
+
+#include <task.h>
+#include <abi_mutex.h>
+#include <sys/types.h>
+#include <sys/prctl.h>
+
+
+int numthreads = -1;
+abilock_t lck;
+
+void ThreadSetDefault( void ){
+ if ( numthreads == -1 ) {
+ numthreads = prctl( PR_MAXPPROCS );
+ }
+ Sys_Printf( "%i threads\n", numthreads );
+ usconfig( CONF_INITUSERS, numthreads );
+}
+
+
+void ThreadLock( void ){
+ spin_lock( &lck );
+}
+
+void ThreadUnlock( void ){
+ release_lock( &lck );
+}
+
+
+/*
+ =============
+ RunThreadsOn
+ =============
+ */
+void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ int i;
+ int pid[MAX_THREADS];
+ int start, end;
+
+ start = I_FloatTime();
+ dispatch = 0;
+ workcount = workcnt;
+ oldf = -1;
+ pacifier = showpacifier;
+ threaded = qtrue;
+
+ if ( pacifier ) {
+ setbuf( stdout, NULL );
+ }
+
+ init_lock( &lck );
+
+ for ( i = 0 ; i < numthreads - 1 ; i++ )
+ {
+ pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
+ , NULL, 0x200000 ); // 2 meg stacks
+ if ( pid[i] == -1 ) {
+ perror( "sproc" );
+ Error( "sproc failed" );
+ }
+ }
+
+ func( i );
+
+ for ( i = 0 ; i < numthreads - 1 ; i++ )
+ wait( NULL );
+
+ threaded = qfalse;
+
+ end = I_FloatTime();
+ if ( pacifier ) {
+ Sys_Printf( " (%i)\n", end - start );
+ }
+}
+
+
+#endif
+
+
+/*
+ =======================================================================
+
+ Linux pthreads
+
+ =======================================================================
+ */
+
+#ifdef __linux__
+#define USED
+
+int numthreads = 4;
+
+void ThreadSetDefault( void ){
+ if ( numthreads == -1 ) { // not set manually
+ /* default to one thread, only multi-thread when specifically told to */
+ numthreads = 1;
+ }
+ if ( numthreads > 1 ) {
+ Sys_Printf( "threads: %d\n", numthreads );
+ }
+}
+
+#include <pthread.h>
+
+typedef struct pt_mutex_s
+{
+ pthread_t *owner;
+ pthread_mutex_t a_mutex;
+ pthread_cond_t cond;
+ unsigned int lock;
+} pt_mutex_t;
+
+pt_mutex_t global_lock;
+
+void ThreadLock( void ){
+ pt_mutex_t *pt_mutex = &global_lock;
+
+ if ( !threaded ) {
+ return;
+ }
+
+ pthread_mutex_lock( &pt_mutex->a_mutex );
+ if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
+ pt_mutex->lock++;
+ }
+ else
+ {
+ if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
+ pt_mutex->owner = (pthread_t *)pthread_self();
+ pt_mutex->lock = 1;
+ }
+ else
+ {
+ while ( 1 )
+ {
+ pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
+ if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
+ pt_mutex->owner = (pthread_t *)pthread_self();
+ pt_mutex->lock = 1;
+ break;
+ }
+ }
+ }
+ }
+ pthread_mutex_unlock( &pt_mutex->a_mutex );
+}
+
+void ThreadUnlock( void ){
+ pt_mutex_t *pt_mutex = &global_lock;
+
+ if ( !threaded ) {
+ return;
+ }
+
+ pthread_mutex_lock( &pt_mutex->a_mutex );
+ pt_mutex->lock--;
+
+ if ( pt_mutex->lock == 0 ) {
+ pt_mutex->owner = NULL;
+ pthread_cond_signal( &pt_mutex->cond );
+ }
+
+ pthread_mutex_unlock( &pt_mutex->a_mutex );
+}
+
+void recursive_mutex_init( pthread_mutexattr_t attribs ){
+ pt_mutex_t *pt_mutex = &global_lock;
+
+ pt_mutex->owner = NULL;
+ if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
+ Error( "pthread_mutex_init failed\n" );
+ }
+ if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
+ Error( "pthread_cond_init failed\n" );
+ }
+
+ pt_mutex->lock = 0;
+}
+
+/*
+ =============
+ RunThreadsOn
+ =============
+ */
+void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ pthread_mutexattr_t mattrib;
+ pthread_t work_threads[MAX_THREADS];
+
+ int start, end;
+ int i = 0;
+ void *exit_value;
+
+ start = I_FloatTime();
+ pacifier = showpacifier;
+
+ dispatch = 0;
+ oldf = -1;
+ workcount = workcnt;
+
+ if ( numthreads == 1 ) {
+ func( 0 );
+ }
+ else
+ {
+ threaded = qtrue;
+
+ if ( pacifier ) {
+ setbuf( stdout, NULL );
+ }
+
+ 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" ); }
+ 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 ) {
+ Error( "pthread_create failed" );
+ }
+ }
+ for ( i = 0 ; i < numthreads ; i++ )
+ {
+ if ( pthread_join( work_threads[i], &exit_value ) != 0 ) {
+ Error( "pthread_join failed" );
+ }
+ }
+ pthread_mutexattr_destroy( &mattrib );
+ threaded = qfalse;
+ }
+
+ end = I_FloatTime();
+ if ( pacifier ) {
+ Sys_Printf( " (%i)\n", end - start );
+ }
+}
+#endif // ifdef __linux__
+
+
+/*
+ =======================================================================
+
+ SINGLE THREAD
+
+ =======================================================================
+ */
+
+#ifndef USED
+
+int numthreads = 1;
+
+void ThreadSetDefault( void ){
+ numthreads = 1;
+}
+
+void ThreadLock( void ){
+}
+
+void ThreadUnlock( void ){
+}
+
+/*
+ =============
+ RunThreadsOn
+ =============
+ */
+void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
+ int start, end;
+
+ dispatch = 0;
+ workcount = workcnt;
+ oldf = -1;
+ pacifier = showpacifier;
+ start = I_FloatTime();
+ func( 0 );
+
+ end = I_FloatTime();
+ if ( pacifier ) {
+ Sys_Printf( " (%i)\n", end - start );
+ }
+}
+
+#endif