2 Copyright (C) 1999-2006 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
23 // The below define is necessary to use
24 // pthreads extensions like pthread_mutexattr_settype
34 #define MAX_THREADS 64
49 int GetThreadWork( void ){
55 if ( dispatch == workcount ) {
60 f = 40 * dispatch / workcount;
62 Sys_Printf( "warning: progress went backwards (should never happen)\n" );
69 if ( oldf % 4 == 0 ) {
70 Sys_Printf( "%i", f / 4 );
75 fflush( stdout ); /* ydnar */
87 void ( *workfunction )( int );
89 void ThreadWorkerFunction( int threadnum ){
94 work = GetThreadWork();
98 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
103 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
104 if ( numthreads == -1 ) {
108 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
113 ===================================================================
117 ===================================================================
126 CRITICAL_SECTION crit;
129 void ThreadSetDefault( void ){
132 if ( numthreads == -1 ) { // not set manually
133 GetSystemInfo( &info );
134 numthreads = info.dwNumberOfProcessors;
135 if ( numthreads < 1 || numthreads > 32 ) {
140 Sys_Printf( "%i threads\n", numthreads );
144 void ThreadLock( void ){
148 EnterCriticalSection( &crit );
150 Error( "Recursive ThreadLock\n" );
155 void ThreadUnlock( void ){
160 Error( "ThreadUnlock without lock\n" );
163 LeaveCriticalSection( &crit );
171 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
172 int threadid[MAX_THREADS];
173 HANDLE threadhandle[MAX_THREADS];
177 start = I_FloatTime();
181 pacifier = showpacifier;
185 // run threads in parallel
187 InitializeCriticalSection( &crit );
189 if ( numthreads == 1 ) { // use same thread
194 for ( i = 0 ; i < numthreads ; i++ )
196 threadhandle[i] = CreateThread(
197 NULL, // LPSECURITY_ATTRIBUTES lpsa,
198 //0, // DWORD cbStack,
200 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
203 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
204 (LPVOID)i, // LPVOID lpvThreadParm,
205 0, // DWORD fdwCreate,
209 for ( i = 0 ; i < numthreads ; i++ )
210 WaitForSingleObject( threadhandle[i], INFINITE );
212 DeleteCriticalSection( &crit );
217 Sys_Printf( " (%i)\n", end - start );
225 ===================================================================
229 ===================================================================
237 void ThreadSetDefault( void ){
238 if ( numthreads == -1 ) { // not set manually
246 pthread_mutex_t *my_mutex;
248 void ThreadLock( void ){
250 pthread_mutex_lock( my_mutex );
254 void ThreadUnlock( void ){
256 pthread_mutex_unlock( my_mutex );
266 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
268 pthread_t work_threads[MAX_THREADS];
269 pthread_addr_t status;
270 pthread_attr_t attrib;
271 pthread_mutexattr_t mattrib;
274 start = I_FloatTime();
278 pacifier = showpacifier;
282 setbuf( stdout, NULL );
286 my_mutex = safe_malloc( sizeof( *my_mutex ) );
287 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
288 Error( "pthread_mutex_attr_create failed" );
290 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
291 Error( "pthread_mutexattr_setkind_np failed" );
293 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
294 Error( "pthread_mutex_init failed" );
298 if ( pthread_attr_create( &attrib ) == -1 ) {
299 Error( "pthread_attr_create failed" );
301 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
302 Error( "pthread_attr_setstacksize failed" );
305 for ( i = 0 ; i < numthreads ; i++ )
307 if ( pthread_create( &work_threads[i], attrib
308 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
309 Error( "pthread_create failed" );
313 for ( i = 0 ; i < numthreads ; i++ )
315 if ( pthread_join( work_threads[i], &status ) == -1 ) {
316 Error( "pthread_join failed" );
324 Sys_Printf( " (%i)\n", end - start );
332 ===================================================================
336 ===================================================================
343 #include <abi_mutex.h>
344 #include <sys/types.h>
345 #include <sys/prctl.h>
351 void ThreadSetDefault( void ){
352 if ( numthreads == -1 ) {
353 numthreads = prctl( PR_MAXPPROCS );
355 Sys_Printf( "%i threads\n", numthreads );
356 usconfig( CONF_INITUSERS, numthreads );
360 void ThreadLock( void ){
364 void ThreadUnlock( void ){
365 release_lock( &lck );
374 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
376 int pid[MAX_THREADS];
379 start = I_FloatTime();
383 pacifier = showpacifier;
387 setbuf( stdout, NULL );
392 for ( i = 0 ; i < numthreads - 1 ; i++ )
394 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
395 , NULL, 0x200000 ); // 2 meg stacks
396 if ( pid[i] == -1 ) {
398 Error( "sproc failed" );
404 for ( i = 0 ; i < numthreads - 1 ; i++ )
411 Sys_Printf( " (%i)\n", end - start );
420 =======================================================================
424 =======================================================================
427 #if defined( __linux__ ) || ( defined( __APPLE__ ) && !MAC_STATIC_HACK )
434 void ThreadSetDefault( void ){
435 if ( numthreads == -1 ) { // not set manually
436 #ifdef _SC_NPROCESSORS_ONLN
437 long cpus = sysconf( _SC_NPROCESSORS_ONLN );
443 /* can't detect, so default to four threads */
447 if ( numthreads > 1 ) {
448 Sys_Printf( "threads: %d\n", numthreads );
454 typedef struct pt_mutex_s
457 pthread_mutex_t a_mutex;
462 pt_mutex_t global_lock;
464 void ThreadLock( void ){
465 pt_mutex_t *pt_mutex = &global_lock;
471 pthread_mutex_lock( &pt_mutex->a_mutex );
472 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
477 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
478 pt_mutex->owner = (pthread_t *)pthread_self();
485 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
486 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
487 pt_mutex->owner = (pthread_t *)pthread_self();
494 pthread_mutex_unlock( &pt_mutex->a_mutex );
497 void ThreadUnlock( void ){
498 pt_mutex_t *pt_mutex = &global_lock;
504 pthread_mutex_lock( &pt_mutex->a_mutex );
507 if ( pt_mutex->lock == 0 ) {
508 pt_mutex->owner = NULL;
509 pthread_cond_signal( &pt_mutex->cond );
512 pthread_mutex_unlock( &pt_mutex->a_mutex );
515 void recursive_mutex_init( pthread_mutexattr_t attribs ){
516 pt_mutex_t *pt_mutex = &global_lock;
518 pt_mutex->owner = NULL;
519 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
520 Error( "pthread_mutex_init failed\n" );
522 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
523 Error( "pthread_cond_init failed\n" );
534 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
535 pthread_mutexattr_t mattrib;
537 pthread_t work_threads[MAX_THREADS];
543 start = I_FloatTime();
544 pacifier = showpacifier;
550 pthread_attr_init( &attr );
551 if ( pthread_attr_setstacksize( &attr, 8388608 ) != 0 ) {
553 pthread_attr_getstacksize( &attr, &stacksize );
554 Sys_Printf( "Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0 );
557 if ( numthreads == 1 ) {
565 setbuf( stdout, NULL );
568 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
569 Error( "pthread_mutexattr_init failed" );
571 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
572 Error( "pthread_mutexattr_settype failed" );
574 recursive_mutex_init( mattrib );
576 for ( i = 0 ; i < numthreads ; i++ )
578 /* Default pthread attributes: joinable & non-realtime scheduling */
579 if ( pthread_create( &work_threads[i], &attr, (void*)func, (void*)(size_t)i ) != 0 ) {
580 Error( "pthread_create failed" );
583 for ( i = 0 ; i < numthreads ; i++ )
585 if ( pthread_join( work_threads[i], NULL ) != 0 ) {
586 Error( "pthread_join failed" );
589 pthread_mutexattr_destroy( &mattrib );
595 Sys_Printf( " (%i)\n", end - start );
598 #endif // ifdef __linux__
602 =======================================================================
606 =======================================================================
613 void ThreadSetDefault( void ){
617 void ThreadLock( void ){
620 void ThreadUnlock( void ){
628 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
635 pacifier = showpacifier;
636 start = I_FloatTime();
641 Sys_Printf( " (%i)\n", end - start );