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
32 #include "her2_threads.h"
34 #define MAX_THREADS 64
49 int GetThreadWork( void ){
55 if ( dispatch == workcount ) {
60 f = 10 * dispatch / workcount;
64 Sys_Printf( "%i...", f );
65 fflush( stdout ); /* ydnar */
77 void ( *workfunction )( int );
79 void ThreadWorkerFunction( int threadnum ){
84 work = GetThreadWork();
88 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
93 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
94 if ( numthreads == -1 ) {
98 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
103 ===================================================================
107 ===================================================================
116 CRITICAL_SECTION crit;
119 void ThreadSetDefault( void ){
122 if ( numthreads == -1 ) { // not set manually
123 GetSystemInfo( &info );
124 numthreads = info.dwNumberOfProcessors;
125 if ( numthreads < 1 || numthreads > 32 ) {
130 Sys_Printf( "%i threads\n", numthreads );
134 void ThreadLock( void ){
138 EnterCriticalSection( &crit );
140 Error( "Recursive ThreadLock\n" );
145 void ThreadUnlock( void ){
150 Error( "ThreadUnlock without lock\n" );
153 LeaveCriticalSection( &crit );
161 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
162 int threadid[MAX_THREADS];
163 HANDLE threadhandle[MAX_THREADS];
167 start = I_FloatTime();
171 pacifier = showpacifier;
175 // run threads in parallel
177 InitializeCriticalSection( &crit );
179 if ( numthreads == 1 ) { // use same thread
184 for ( i = 0 ; i < numthreads ; i++ )
186 threadhandle[i] = CreateThread(
187 NULL, // LPSECURITY_ATTRIBUTES lpsa,
188 //0, // DWORD cbStack,
190 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
193 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
194 (LPVOID)i, // LPVOID lpvThreadParm,
195 0, // DWORD fdwCreate,
199 for ( i = 0 ; i < numthreads ; i++ )
200 WaitForSingleObject( threadhandle[i], INFINITE );
202 DeleteCriticalSection( &crit );
207 Sys_Printf( " (%i)\n", end - start );
215 ===================================================================
219 ===================================================================
227 void ThreadSetDefault( void ){
228 if ( numthreads == -1 ) { // not set manually
236 pthread_mutex_t *my_mutex;
238 void ThreadLock( void ){
240 pthread_mutex_lock( my_mutex );
244 void ThreadUnlock( void ){
246 pthread_mutex_unlock( my_mutex );
256 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
258 pthread_t work_threads[MAX_THREADS];
259 pthread_addr_t status;
260 pthread_attr_t attrib;
261 pthread_mutexattr_t mattrib;
264 start = I_FloatTime();
268 pacifier = showpacifier;
272 setbuf( stdout, NULL );
276 my_mutex = safe_malloc( sizeof( *my_mutex ) );
277 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
278 Error( "pthread_mutex_attr_create failed" );
280 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
281 Error( "pthread_mutexattr_setkind_np failed" );
283 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
284 Error( "pthread_mutex_init failed" );
288 if ( pthread_attr_create( &attrib ) == -1 ) {
289 Error( "pthread_attr_create failed" );
291 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
292 Error( "pthread_attr_setstacksize failed" );
295 for ( i = 0 ; i < numthreads ; i++ )
297 if ( pthread_create( &work_threads[i], attrib
298 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
299 Error( "pthread_create failed" );
303 for ( i = 0 ; i < numthreads ; i++ )
305 if ( pthread_join( work_threads[i], &status ) == -1 ) {
306 Error( "pthread_join failed" );
314 Sys_Printf( " (%i)\n", end - start );
322 ===================================================================
326 ===================================================================
333 #include <abi_mutex.h>
334 #include <sys/types.h>
335 #include <sys/prctl.h>
341 void ThreadSetDefault( void ){
342 if ( numthreads == -1 ) {
343 numthreads = prctl( PR_MAXPPROCS );
345 Sys_Printf( "%i threads\n", numthreads );
346 usconfig( CONF_INITUSERS, numthreads );
350 void ThreadLock( void ){
354 void ThreadUnlock( void ){
355 release_lock( &lck );
364 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
366 int pid[MAX_THREADS];
369 start = I_FloatTime();
373 pacifier = showpacifier;
377 setbuf( stdout, NULL );
382 for ( i = 0 ; i < numthreads - 1 ; i++ )
384 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
385 , NULL, 0x200000 ); // 2 meg stacks
386 if ( pid[i] == -1 ) {
388 Error( "sproc failed" );
394 for ( i = 0 ; i < numthreads - 1 ; i++ )
401 Sys_Printf( " (%i)\n", end - start );
410 =======================================================================
414 =======================================================================
422 void ThreadSetDefault( void ){
423 if ( numthreads == -1 ) { // not set manually
424 /* default to one thread, only multi-thread when specifically told to */
427 if ( numthreads > 1 ) {
428 Sys_Printf( "threads: %d\n", numthreads );
434 typedef struct pt_mutex_s
437 pthread_mutex_t a_mutex;
442 pt_mutex_t global_lock;
444 void ThreadLock( void ){
445 pt_mutex_t *pt_mutex = &global_lock;
451 pthread_mutex_lock( &pt_mutex->a_mutex );
452 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
457 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
458 pt_mutex->owner = (pthread_t *)pthread_self();
465 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
466 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
467 pt_mutex->owner = (pthread_t *)pthread_self();
474 pthread_mutex_unlock( &pt_mutex->a_mutex );
477 void ThreadUnlock( void ){
478 pt_mutex_t *pt_mutex = &global_lock;
484 pthread_mutex_lock( &pt_mutex->a_mutex );
487 if ( pt_mutex->lock == 0 ) {
488 pt_mutex->owner = NULL;
489 pthread_cond_signal( &pt_mutex->cond );
492 pthread_mutex_unlock( &pt_mutex->a_mutex );
495 void recursive_mutex_init( pthread_mutexattr_t attribs ){
496 pt_mutex_t *pt_mutex = &global_lock;
498 pt_mutex->owner = NULL;
499 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
500 Error( "pthread_mutex_init failed\n" );
502 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
503 Error( "pthread_cond_init failed\n" );
514 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
515 pthread_mutexattr_t mattrib;
516 pthread_t work_threads[MAX_THREADS];
519 int i = 0, status = 0;
521 start = I_FloatTime();
522 pacifier = showpacifier;
528 if ( numthreads == 1 ) {
536 setbuf( stdout, NULL );
539 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
540 Error( "pthread_mutexattr_init failed" );
542 #if __GLIBC_MINOR__ == 1
543 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_FAST_NP ) != 0 )
545 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ADAPTIVE_NP ) != 0 )
547 { Error( "pthread_mutexattr_settype failed" ); }
548 recursive_mutex_init( mattrib );
550 for ( i = 0 ; i < numthreads ; i++ )
552 /* Default pthread attributes: joinable & non-realtime scheduling */
553 if ( pthread_create( &work_threads[i], NULL, (void*)func, (void*)i ) != 0 ) {
554 Error( "pthread_create failed" );
557 for ( i = 0 ; i < numthreads ; i++ )
559 if ( pthread_join( work_threads[i], (void **)&status ) != 0 ) {
560 Error( "pthread_join failed" );
563 pthread_mutexattr_destroy( &mattrib );
569 Sys_Printf( " (%i)\n", end - start );
572 #endif // ifdef __linux__
576 =======================================================================
580 =======================================================================
587 void ThreadSetDefault( void ){
591 void ThreadLock( void ){
594 void ThreadUnlock( void ){
602 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
609 pacifier = showpacifier;
610 start = I_FloatTime();
615 Sys_Printf( " (%i)\n", end - start );