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"
26 // The below define is necessary to use
27 // pthreads extensions like pthread_mutexattr_settype
29 #endif // !GDEF_OS_WINDOWS
36 #define MAX_THREADS 64
51 int GetThreadWork( void ){
57 if ( dispatch == workcount ) {
62 f = 40 * dispatch / workcount;
64 Sys_FPrintf( SYS_WRN, "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 );
117 ===================================================================
121 ===================================================================
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 ===================================================================
235 void ThreadSetDefault( void ){
236 if ( numthreads == -1 ) { // not set manually
243 pthread_mutex_t *my_mutex;
245 void ThreadLock( void ){
247 pthread_mutex_lock( my_mutex );
251 void ThreadUnlock( void ){
253 pthread_mutex_unlock( my_mutex );
263 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
265 pthread_t work_threads[MAX_THREADS];
266 pthread_addr_t status;
267 pthread_attr_t attrib;
268 pthread_mutexattr_t mattrib;
271 start = I_FloatTime();
275 pacifier = showpacifier;
279 setbuf( stdout, NULL );
283 my_mutex = safe_malloc( sizeof( *my_mutex ) );
284 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
285 Error( "pthread_mutex_attr_create failed" );
287 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
288 Error( "pthread_mutexattr_setkind_np failed" );
290 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
291 Error( "pthread_mutex_init failed" );
295 if ( pthread_attr_create( &attrib ) == -1 ) {
296 Error( "pthread_attr_create failed" );
298 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
299 Error( "pthread_attr_setstacksize failed" );
302 for ( i = 0 ; i < numthreads ; i++ )
304 if ( pthread_create( &work_threads[i], attrib
305 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
306 Error( "pthread_create failed" );
310 for ( i = 0 ; i < numthreads ; i++ )
312 if ( pthread_join( work_threads[i], &status ) == -1 ) {
313 Error( "pthread_join failed" );
321 Sys_Printf( " (%i)\n", end - start );
329 ===================================================================
333 ===================================================================
339 #include <abi_mutex.h>
340 #include <sys/types.h>
341 #include <sys/prctl.h>
346 void ThreadSetDefault( void ){
347 if ( numthreads == -1 ) {
348 numthreads = prctl( PR_MAXPPROCS );
350 Sys_Printf( "%i threads\n", numthreads );
351 usconfig( CONF_INITUSERS, numthreads );
355 void ThreadLock( void ){
359 void ThreadUnlock( void ){
360 release_lock( &lck );
369 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
371 int pid[MAX_THREADS];
374 start = I_FloatTime();
378 pacifier = showpacifier;
382 setbuf( stdout, NULL );
387 for ( i = 0 ; i < numthreads - 1 ; i++ )
389 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
390 , NULL, 0x200000 ); // 2 meg stacks
391 if ( pid[i] == -1 ) {
393 Error( "sproc failed" );
399 for ( i = 0 ; i < numthreads - 1 ; i++ )
406 Sys_Printf( " (%i)\n", end - start );
411 #elif GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS
414 =======================================================================
418 =======================================================================
425 void ThreadSetDefault( void ){
426 if ( numthreads == -1 ) { // not set manually
427 #ifdef _SC_NPROCESSORS_ONLN
428 long cpus = sysconf( _SC_NPROCESSORS_ONLN );
434 /* can't detect, so default to four threads */
438 if ( numthreads > 1 ) {
439 Sys_Printf( "threads: %d\n", numthreads );
445 typedef struct pt_mutex_s
448 pthread_mutex_t a_mutex;
453 pt_mutex_t global_lock;
455 void ThreadLock( void ){
456 pt_mutex_t *pt_mutex = &global_lock;
462 pthread_mutex_lock( &pt_mutex->a_mutex );
463 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
468 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
469 pt_mutex->owner = (pthread_t *)pthread_self();
476 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
477 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
478 pt_mutex->owner = (pthread_t *)pthread_self();
485 pthread_mutex_unlock( &pt_mutex->a_mutex );
488 void ThreadUnlock( void ){
489 pt_mutex_t *pt_mutex = &global_lock;
495 pthread_mutex_lock( &pt_mutex->a_mutex );
498 if ( pt_mutex->lock == 0 ) {
499 pt_mutex->owner = NULL;
500 pthread_cond_signal( &pt_mutex->cond );
503 pthread_mutex_unlock( &pt_mutex->a_mutex );
506 void recursive_mutex_init( pthread_mutexattr_t attribs ){
507 pt_mutex_t *pt_mutex = &global_lock;
509 pt_mutex->owner = NULL;
510 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
511 Error( "pthread_mutex_init failed\n" );
513 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
514 Error( "pthread_cond_init failed\n" );
525 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
526 pthread_mutexattr_t mattrib;
528 pthread_t work_threads[MAX_THREADS];
534 start = I_FloatTime();
535 pacifier = showpacifier;
541 pthread_attr_init( &attr );
542 if ( pthread_attr_setstacksize( &attr, 8388608 ) != 0 ) {
544 pthread_attr_getstacksize( &attr, &stacksize );
545 Sys_Printf( "Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0 );
548 if ( numthreads == 1 ) {
556 setbuf( stdout, NULL );
559 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
560 Error( "pthread_mutexattr_init failed" );
562 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ERRORCHECK ) != 0 ) {
563 Error( "pthread_mutexattr_settype failed" );
565 recursive_mutex_init( mattrib );
567 for ( i = 0 ; i < numthreads ; i++ )
569 /* Default pthread attributes: joinable & non-realtime scheduling */
570 if ( pthread_create(&work_threads[i], &attr, (void *(*)(void *)) func, (void*)(uintptr_t)i ) != 0 ) {
571 Error( "pthread_create failed" );
574 for ( i = 0 ; i < numthreads ; i++ )
576 if ( pthread_join( work_threads[i], NULL ) != 0 ) {
577 Error( "pthread_join failed" );
580 pthread_mutexattr_destroy( &mattrib );
586 Sys_Printf( " (%i)\n", end - start );
594 =======================================================================
598 =======================================================================
603 void ThreadSetDefault( void ){
607 void ThreadLock( void ){
610 void ThreadUnlock( void ){
618 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
624 pacifier = showpacifier;
625 start = I_FloatTime();
630 Sys_Printf( " (%i)\n", end - start );