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
34 #include "q2_threads.h"
36 #define MAX_THREADS 64
51 int GetThreadWork( void ){
57 if ( dispatch == workcount ) {
62 f = 10 * dispatch / workcount;
66 Sys_Printf( "%i...", f );
67 fflush( stdout ); /* ydnar */
79 void ( *workfunction )( int );
81 void ThreadWorkerFunction( int threadnum ){
86 work = GetThreadWork();
90 //Sys_FPrintf( SYS_VRB,"thread %i, work %i\n", threadnum, work);
95 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
96 if ( numthreads == -1 ) {
100 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
105 ===================================================================
109 ===================================================================
117 // Setting default Threads to 1
119 CRITICAL_SECTION crit;
122 void ThreadSetDefault( void ){
125 if ( numthreads == -1 ) { // not set manually
126 GetSystemInfo( &info );
127 numthreads = info.dwNumberOfProcessors;
128 if ( numthreads < 1 || numthreads > 32 ) {
133 Sys_Printf( "%i threads\n", numthreads );
137 void ThreadLock( void ){
141 EnterCriticalSection( &crit );
143 Error( "Recursive ThreadLock\n" );
148 void ThreadUnlock( void ){
153 Error( "ThreadUnlock without lock\n" );
156 LeaveCriticalSection( &crit );
164 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
165 int threadid[MAX_THREADS];
166 HANDLE threadhandle[MAX_THREADS];
170 start = I_FloatTime();
174 pacifier = showpacifier;
178 // run threads in parallel
180 InitializeCriticalSection( &crit );
182 if ( numthreads == 1 ) { // use same thread
187 for ( i = 0 ; i < numthreads ; i++ )
189 threadhandle[i] = CreateThread(
190 NULL, // LPSECURITY_ATTRIBUTES lpsa,
191 //0, // DWORD cbStack,
193 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
196 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
197 (LPVOID)i, // LPVOID lpvThreadParm,
198 0, // DWORD fdwCreate,
202 for ( i = 0 ; i < numthreads ; i++ )
203 WaitForSingleObject( threadhandle[i], INFINITE );
205 DeleteCriticalSection( &crit );
210 Sys_Printf( " (%i)\n", end - start );
218 ===================================================================
222 ===================================================================
230 void ThreadSetDefault( void ){
231 if ( numthreads == -1 ) { // not set manually
239 pthread_mutex_t *my_mutex;
241 void ThreadLock( void ){
243 pthread_mutex_lock( my_mutex );
247 void ThreadUnlock( void ){
249 pthread_mutex_unlock( my_mutex );
259 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
261 pthread_t work_threads[MAX_THREADS];
262 pthread_addr_t status;
263 pthread_attr_t attrib;
264 pthread_mutexattr_t mattrib;
267 start = I_FloatTime();
271 pacifier = showpacifier;
275 setbuf( stdout, NULL );
279 my_mutex = safe_malloc( sizeof( *my_mutex ) );
280 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
281 Error( "pthread_mutex_attr_create failed" );
283 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
284 Error( "pthread_mutexattr_setkind_np failed" );
286 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
287 Error( "pthread_mutex_init failed" );
291 if ( pthread_attr_create( &attrib ) == -1 ) {
292 Error( "pthread_attr_create failed" );
294 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
295 Error( "pthread_attr_setstacksize failed" );
298 for ( i = 0 ; i < numthreads ; i++ )
300 if ( pthread_create( &work_threads[i], attrib
301 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
302 Error( "pthread_create failed" );
306 for ( i = 0 ; i < numthreads ; i++ )
308 if ( pthread_join( work_threads[i], &status ) == -1 ) {
309 Error( "pthread_join failed" );
317 Sys_Printf( " (%i)\n", end - start );
325 ===================================================================
329 ===================================================================
336 #include <abi_mutex.h>
337 #include <sys/types.h>
338 #include <sys/prctl.h>
344 void ThreadSetDefault( void ){
345 if ( numthreads == -1 ) {
346 numthreads = prctl( PR_MAXPPROCS );
348 Sys_Printf( "%i threads\n", numthreads );
349 usconfig( CONF_INITUSERS, numthreads );
353 void ThreadLock( void ){
357 void ThreadUnlock( void ){
358 release_lock( &lck );
367 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
369 int pid[MAX_THREADS];
372 start = I_FloatTime();
376 pacifier = showpacifier;
380 setbuf( stdout, NULL );
385 for ( i = 0 ; i < numthreads - 1 ; i++ )
387 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
388 , NULL, 0x200000 ); // 2 meg stacks
389 if ( pid[i] == -1 ) {
391 Error( "sproc failed" );
397 for ( i = 0 ; i < numthreads - 1 ; i++ )
404 Sys_Printf( " (%i)\n", end - start );
413 =======================================================================
417 =======================================================================
420 #if GDEF_OS_LINUX || GDEF_OS_MACOS
423 // Setting default Threads to 1
426 void ThreadSetDefault( void ){
427 if ( numthreads == -1 ) { // not set manually
428 /* default to one thread, only multi-thread when specifically told to */
431 if ( numthreads > 1 ) {
432 Sys_Printf( "threads: %d\n", numthreads );
438 typedef struct pt_mutex_s
441 pthread_mutex_t a_mutex;
446 pt_mutex_t global_lock;
448 void ThreadLock( void ){
449 pt_mutex_t *pt_mutex = &global_lock;
455 pthread_mutex_lock( &pt_mutex->a_mutex );
456 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
461 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
462 pt_mutex->owner = (pthread_t *)pthread_self();
469 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
470 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
471 pt_mutex->owner = (pthread_t *)pthread_self();
478 pthread_mutex_unlock( &pt_mutex->a_mutex );
481 void ThreadUnlock( void ){
482 pt_mutex_t *pt_mutex = &global_lock;
488 pthread_mutex_lock( &pt_mutex->a_mutex );
491 if ( pt_mutex->lock == 0 ) {
492 pt_mutex->owner = NULL;
493 pthread_cond_signal( &pt_mutex->cond );
496 pthread_mutex_unlock( &pt_mutex->a_mutex );
499 void recursive_mutex_init( pthread_mutexattr_t attribs ){
500 pt_mutex_t *pt_mutex = &global_lock;
502 pt_mutex->owner = NULL;
503 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
504 Error( "pthread_mutex_init failed\n" );
506 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
507 Error( "pthread_cond_init failed\n" );
518 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
519 pthread_mutexattr_t mattrib;
520 pthread_t work_threads[MAX_THREADS];
523 int i = 0, status = 0;
525 start = I_FloatTime();
526 pacifier = showpacifier;
532 if ( numthreads == 1 ) {
540 setbuf( stdout, NULL );
543 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
544 Error( "pthread_mutexattr_init failed" );
546 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
547 Error( "pthread_mutexattr_settype failed" );
549 recursive_mutex_init( mattrib );
551 for ( i = 0 ; i < numthreads ; i++ )
553 /* Default pthread attributes: joinable & non-realtime scheduling */
554 if ( pthread_create( &work_threads[i], NULL, (void*)func, (void*)i ) != 0 ) {
555 Error( "pthread_create failed" );
558 for ( i = 0 ; i < numthreads ; i++ )
560 if ( pthread_join( work_threads[i], (void **)&status ) != 0 ) {
561 Error( "pthread_join failed" );
564 pthread_mutexattr_destroy( &mattrib );
570 Sys_Printf( " (%i)\n", end - start );
573 #endif // ifdef __linux__
577 =======================================================================
581 =======================================================================
588 void ThreadSetDefault( void ){
592 void ThreadLock( void ){
595 void ThreadUnlock( void ){
603 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
610 pacifier = showpacifier;
611 start = I_FloatTime();
616 Sys_Printf( " (%i)\n", end - start );