2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "globaldefs.h"
25 // The below define is necessary to use
26 // pthreads extensions like pthread_mutexattr_settype
36 #define MAX_THREADS 64
51 int GetThreadWork( void ){
57 if ( dispatch == workcount ) {
62 f = 40 * dispatch / workcount;
64 Sys_Printf( "warning: progress went backwards (should never happen)\n" );
71 if ( oldf % 4 == 0 ) {
72 Sys_Printf( "%i", f / 4 );
77 fflush( stdout ); /* ydnar */
89 void ( *workfunction )( int );
91 void ThreadWorkerFunction( int threadnum ){
96 work = GetThreadWork();
100 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
101 workfunction( work );
105 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
106 if ( numthreads == -1 ) {
110 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
115 ===================================================================
119 ===================================================================
128 CRITICAL_SECTION crit;
131 void ThreadSetDefault( void ){
134 if ( numthreads == -1 ) { // not set manually
135 GetSystemInfo( &info );
136 numthreads = info.dwNumberOfProcessors;
137 if ( numthreads < 1 || numthreads > 32 ) {
142 Sys_Printf( "%i threads\n", numthreads );
146 void ThreadLock( void ){
150 EnterCriticalSection( &crit );
152 Error( "Recursive ThreadLock\n" );
157 void ThreadUnlock( void ){
162 Error( "ThreadUnlock without lock\n" );
165 LeaveCriticalSection( &crit );
173 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
174 int threadid[MAX_THREADS];
175 HANDLE threadhandle[MAX_THREADS];
179 start = I_FloatTime();
183 pacifier = showpacifier;
187 // run threads in parallel
189 InitializeCriticalSection( &crit );
191 if ( numthreads == 1 ) { // use same thread
196 for ( i = 0 ; i < numthreads ; i++ )
198 threadhandle[i] = CreateThread(
199 NULL, // LPSECURITY_ATTRIBUTES lpsa,
200 //0, // DWORD cbStack,
202 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
205 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
206 (LPVOID)i, // LPVOID lpvThreadParm,
207 0, // DWORD fdwCreate,
211 for ( i = 0 ; i < numthreads ; i++ )
212 WaitForSingleObject( threadhandle[i], INFINITE );
214 DeleteCriticalSection( &crit );
219 Sys_Printf( " (%i)\n", end - start );
227 ===================================================================
231 ===================================================================
239 void ThreadSetDefault( void ){
240 if ( numthreads == -1 ) { // not set manually
248 pthread_mutex_t *my_mutex;
250 void ThreadLock( void ){
252 pthread_mutex_lock( my_mutex );
256 void ThreadUnlock( void ){
258 pthread_mutex_unlock( my_mutex );
268 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
270 pthread_t work_threads[MAX_THREADS];
271 pthread_addr_t status;
272 pthread_attr_t attrib;
273 pthread_mutexattr_t mattrib;
276 start = I_FloatTime();
280 pacifier = showpacifier;
284 setbuf( stdout, NULL );
288 my_mutex = safe_malloc( sizeof( *my_mutex ) );
289 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
290 Error( "pthread_mutex_attr_create failed" );
292 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
293 Error( "pthread_mutexattr_setkind_np failed" );
295 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
296 Error( "pthread_mutex_init failed" );
300 if ( pthread_attr_create( &attrib ) == -1 ) {
301 Error( "pthread_attr_create failed" );
303 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
304 Error( "pthread_attr_setstacksize failed" );
307 for ( i = 0 ; i < numthreads ; i++ )
309 if ( pthread_create( &work_threads[i], attrib
310 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
311 Error( "pthread_create failed" );
315 for ( i = 0 ; i < numthreads ; i++ )
317 if ( pthread_join( work_threads[i], &status ) == -1 ) {
318 Error( "pthread_join failed" );
326 Sys_Printf( " (%i)\n", end - start );
334 ===================================================================
338 ===================================================================
345 #include <abi_mutex.h>
346 #include <sys/types.h>
347 #include <sys/prctl.h>
353 void ThreadSetDefault( void ){
354 if ( numthreads == -1 ) {
355 numthreads = prctl( PR_MAXPPROCS );
357 Sys_Printf( "%i threads\n", numthreads );
358 usconfig( CONF_INITUSERS, numthreads );
362 void ThreadLock( void ){
366 void ThreadUnlock( void ){
367 release_lock( &lck );
376 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
378 int pid[MAX_THREADS];
381 start = I_FloatTime();
385 pacifier = showpacifier;
389 setbuf( stdout, NULL );
394 for ( i = 0 ; i < numthreads - 1 ; i++ )
396 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
397 , NULL, 0x200000 ); // 2 meg stacks
398 if ( pid[i] == -1 ) {
400 Error( "sproc failed" );
406 for ( i = 0 ; i < numthreads - 1 ; i++ )
413 Sys_Printf( " (%i)\n", end - start );
422 =======================================================================
426 =======================================================================
429 #if GDEF_OS_LINUX || ( GDEF_OS_MACOS && !MAC_STATIC_HACK )
436 void ThreadSetDefault( void ){
437 if ( numthreads == -1 ) { // not set manually
438 #ifdef _SC_NPROCESSORS_ONLN
439 long cpus = sysconf( _SC_NPROCESSORS_ONLN );
445 /* can't detect, so default to four threads */
449 if ( numthreads > 1 ) {
450 Sys_Printf( "threads: %d\n", numthreads );
456 typedef struct pt_mutex_s
459 pthread_mutex_t a_mutex;
464 pt_mutex_t global_lock;
466 void ThreadLock( void ){
467 pt_mutex_t *pt_mutex = &global_lock;
473 pthread_mutex_lock( &pt_mutex->a_mutex );
474 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
479 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
480 pt_mutex->owner = (pthread_t *)pthread_self();
487 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
488 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
489 pt_mutex->owner = (pthread_t *)pthread_self();
496 pthread_mutex_unlock( &pt_mutex->a_mutex );
499 void ThreadUnlock( void ){
500 pt_mutex_t *pt_mutex = &global_lock;
506 pthread_mutex_lock( &pt_mutex->a_mutex );
509 if ( pt_mutex->lock == 0 ) {
510 pt_mutex->owner = NULL;
511 pthread_cond_signal( &pt_mutex->cond );
514 pthread_mutex_unlock( &pt_mutex->a_mutex );
517 void recursive_mutex_init( pthread_mutexattr_t attribs ){
518 pt_mutex_t *pt_mutex = &global_lock;
520 pt_mutex->owner = NULL;
521 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
522 Error( "pthread_mutex_init failed\n" );
524 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
525 Error( "pthread_cond_init failed\n" );
536 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
537 pthread_mutexattr_t mattrib;
539 pthread_t work_threads[MAX_THREADS];
545 start = I_FloatTime();
546 pacifier = showpacifier;
552 pthread_attr_init( &attr );
553 if ( pthread_attr_setstacksize( &attr, 8388608 ) != 0 ) {
555 pthread_attr_getstacksize( &attr, &stacksize );
556 Sys_Printf( "Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0 );
559 if ( numthreads == 1 ) {
567 setbuf( stdout, NULL );
570 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
571 Error( "pthread_mutexattr_init failed" );
573 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
574 Error( "pthread_mutexattr_settype failed" );
576 recursive_mutex_init( mattrib );
578 for ( i = 0 ; i < numthreads ; i++ )
580 /* Default pthread attributes: joinable & non-realtime scheduling */
581 if ( pthread_create(&work_threads[i], &attr, (void *(*)(void *)) func, (void*)(uintptr_t)i ) != 0 ) {
582 Error( "pthread_create failed" );
585 for ( i = 0 ; i < numthreads ; i++ )
587 if ( pthread_join( work_threads[i], NULL ) != 0 ) {
588 Error( "pthread_join failed" );
591 pthread_mutexattr_destroy( &mattrib );
597 Sys_Printf( " (%i)\n", end - start );
600 #endif // ifdef __linux__
604 =======================================================================
608 =======================================================================
615 void ThreadSetDefault( void ){
619 void ThreadLock( void ){
622 void ThreadUnlock( void ){
630 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
636 pacifier = showpacifier;
637 start = I_FloatTime();
642 Sys_Printf( " (%i)\n", end - start );