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"
24 // The below define is necessary to use
25 // pthreads extensions like pthread_mutexattr_settype
35 #define MAX_THREADS 64
50 int GetThreadWork( void ){
56 if ( dispatch == workcount ) {
61 f = 40 * dispatch / workcount;
63 Sys_Printf( "warning: progress went backwards (should never happen)\n" );
70 if ( oldf % 4 == 0 ) {
71 Sys_Printf( "%i", f / 4 );
76 fflush( stdout ); /* ydnar */
88 void ( *workfunction )( int );
90 void ThreadWorkerFunction( int threadnum ){
95 work = GetThreadWork();
99 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
100 workfunction( work );
104 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
105 if ( numthreads == -1 ) {
109 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
114 ===================================================================
118 ===================================================================
127 CRITICAL_SECTION crit;
130 void ThreadSetDefault( void ){
133 if ( numthreads == -1 ) { // not set manually
134 GetSystemInfo( &info );
135 numthreads = info.dwNumberOfProcessors;
136 if ( numthreads < 1 || numthreads > 32 ) {
141 Sys_Printf( "%i threads\n", numthreads );
145 void ThreadLock( void ){
149 EnterCriticalSection( &crit );
151 Error( "Recursive ThreadLock\n" );
156 void ThreadUnlock( void ){
161 Error( "ThreadUnlock without lock\n" );
164 LeaveCriticalSection( &crit );
172 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
173 int threadid[MAX_THREADS];
174 HANDLE threadhandle[MAX_THREADS];
178 start = I_FloatTime();
182 pacifier = showpacifier;
186 // run threads in parallel
188 InitializeCriticalSection( &crit );
190 if ( numthreads == 1 ) { // use same thread
195 for ( i = 0 ; i < numthreads ; i++ )
197 threadhandle[i] = CreateThread(
198 NULL, // LPSECURITY_ATTRIBUTES lpsa,
199 //0, // DWORD cbStack,
201 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
204 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
205 (LPVOID)i, // LPVOID lpvThreadParm,
206 0, // DWORD fdwCreate,
210 for ( i = 0 ; i < numthreads ; i++ )
211 WaitForSingleObject( threadhandle[i], INFINITE );
213 DeleteCriticalSection( &crit );
218 Sys_Printf( " (%i)\n", end - start );
226 ===================================================================
230 ===================================================================
238 void ThreadSetDefault( void ){
239 if ( numthreads == -1 ) { // not set manually
247 pthread_mutex_t *my_mutex;
249 void ThreadLock( void ){
251 pthread_mutex_lock( my_mutex );
255 void ThreadUnlock( void ){
257 pthread_mutex_unlock( my_mutex );
267 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
269 pthread_t work_threads[MAX_THREADS];
270 pthread_addr_t status;
271 pthread_attr_t attrib;
272 pthread_mutexattr_t mattrib;
275 start = I_FloatTime();
279 pacifier = showpacifier;
283 setbuf( stdout, NULL );
287 my_mutex = safe_malloc( sizeof( *my_mutex ) );
288 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
289 Error( "pthread_mutex_attr_create failed" );
291 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
292 Error( "pthread_mutexattr_setkind_np failed" );
294 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
295 Error( "pthread_mutex_init failed" );
299 if ( pthread_attr_create( &attrib ) == -1 ) {
300 Error( "pthread_attr_create failed" );
302 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
303 Error( "pthread_attr_setstacksize failed" );
306 for ( i = 0 ; i < numthreads ; i++ )
308 if ( pthread_create( &work_threads[i], attrib
309 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
310 Error( "pthread_create failed" );
314 for ( i = 0 ; i < numthreads ; i++ )
316 if ( pthread_join( work_threads[i], &status ) == -1 ) {
317 Error( "pthread_join failed" );
325 Sys_Printf( " (%i)\n", end - start );
333 ===================================================================
337 ===================================================================
344 #include <abi_mutex.h>
345 #include <sys/types.h>
346 #include <sys/prctl.h>
352 void ThreadSetDefault( void ){
353 if ( numthreads == -1 ) {
354 numthreads = prctl( PR_MAXPPROCS );
356 Sys_Printf( "%i threads\n", numthreads );
357 usconfig( CONF_INITUSERS, numthreads );
361 void ThreadLock( void ){
365 void ThreadUnlock( void ){
366 release_lock( &lck );
375 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
377 int pid[MAX_THREADS];
380 start = I_FloatTime();
384 pacifier = showpacifier;
388 setbuf( stdout, NULL );
393 for ( i = 0 ; i < numthreads - 1 ; i++ )
395 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
396 , NULL, 0x200000 ); // 2 meg stacks
397 if ( pid[i] == -1 ) {
399 Error( "sproc failed" );
405 for ( i = 0 ; i < numthreads - 1 ; i++ )
412 Sys_Printf( " (%i)\n", end - start );
421 =======================================================================
425 =======================================================================
428 #if GDEF_OS_LINUX || ( GDEF_OS_MACOS && !MAC_STATIC_HACK )
435 void ThreadSetDefault( void ){
436 if ( numthreads == -1 ) { // not set manually
437 #ifdef _SC_NPROCESSORS_ONLN
438 long cpus = sysconf( _SC_NPROCESSORS_ONLN );
444 /* can't detect, so default to four threads */
448 if ( numthreads > 1 ) {
449 Sys_Printf( "threads: %d\n", numthreads );
455 typedef struct pt_mutex_s
458 pthread_mutex_t a_mutex;
463 pt_mutex_t global_lock;
465 void ThreadLock( void ){
466 pt_mutex_t *pt_mutex = &global_lock;
472 pthread_mutex_lock( &pt_mutex->a_mutex );
473 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
478 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
479 pt_mutex->owner = (pthread_t *)pthread_self();
486 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
487 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
488 pt_mutex->owner = (pthread_t *)pthread_self();
495 pthread_mutex_unlock( &pt_mutex->a_mutex );
498 void ThreadUnlock( void ){
499 pt_mutex_t *pt_mutex = &global_lock;
505 pthread_mutex_lock( &pt_mutex->a_mutex );
508 if ( pt_mutex->lock == 0 ) {
509 pt_mutex->owner = NULL;
510 pthread_cond_signal( &pt_mutex->cond );
513 pthread_mutex_unlock( &pt_mutex->a_mutex );
516 void recursive_mutex_init( pthread_mutexattr_t attribs ){
517 pt_mutex_t *pt_mutex = &global_lock;
519 pt_mutex->owner = NULL;
520 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
521 Error( "pthread_mutex_init failed\n" );
523 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
524 Error( "pthread_cond_init failed\n" );
535 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
536 pthread_mutexattr_t mattrib;
538 pthread_t work_threads[MAX_THREADS];
544 start = I_FloatTime();
545 pacifier = showpacifier;
551 pthread_attr_init( &attr );
552 if ( pthread_attr_setstacksize( &attr, 8388608 ) != 0 ) {
554 pthread_attr_getstacksize( &attr, &stacksize );
555 Sys_Printf( "Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0 );
558 if ( numthreads == 1 ) {
566 setbuf( stdout, NULL );
569 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
570 Error( "pthread_mutexattr_init failed" );
572 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
573 Error( "pthread_mutexattr_settype failed" );
575 recursive_mutex_init( mattrib );
577 for ( i = 0 ; i < numthreads ; i++ )
579 /* Default pthread attributes: joinable & non-realtime scheduling */
580 if ( pthread_create(&work_threads[i], &attr, (void *(*)(void *)) func, (void*)(uintptr_t)i ) != 0 ) {
581 Error( "pthread_create failed" );
584 for ( i = 0 ; i < numthreads ; i++ )
586 if ( pthread_join( work_threads[i], NULL ) != 0 ) {
587 Error( "pthread_join failed" );
590 pthread_mutexattr_destroy( &mattrib );
596 Sys_Printf( " (%i)\n", end - start );
599 #endif // ifdef __linux__
603 =======================================================================
607 =======================================================================
614 void ThreadSetDefault( void ){
618 void ThreadLock( void ){
621 void ThreadUnlock( void ){
629 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
635 pacifier = showpacifier;
636 start = I_FloatTime();
641 Sys_Printf( " (%i)\n", end - start );