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
28 #endif // !GDEF_OS_WINDOWS
33 #include "her2_threads.h"
35 #define MAX_THREADS 64
50 int GetThreadWork( void ){
56 if ( dispatch == workcount ) {
61 f = 10 * dispatch / workcount;
65 Sys_Printf( "%i...", f );
66 fflush( stdout ); /* ydnar */
78 void ( *workfunction )( int );
80 void ThreadWorkerFunction( int threadnum ){
85 work = GetThreadWork();
89 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
94 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
95 if ( numthreads == -1 ) {
99 RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
106 ===================================================================
110 ===================================================================
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 ===================================================================
224 void ThreadSetDefault( void ){
225 if ( numthreads == -1 ) { // not set manually
232 pthread_mutex_t *my_mutex;
234 void ThreadLock( void ){
236 pthread_mutex_lock( my_mutex );
240 void ThreadUnlock( void ){
242 pthread_mutex_unlock( my_mutex );
252 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
254 pthread_t work_threads[MAX_THREADS];
255 pthread_addr_t status;
256 pthread_attr_t attrib;
257 pthread_mutexattr_t mattrib;
260 start = I_FloatTime();
264 pacifier = showpacifier;
268 setbuf( stdout, NULL );
272 my_mutex = safe_malloc( sizeof( *my_mutex ) );
273 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
274 Error( "pthread_mutex_attr_create failed" );
276 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
277 Error( "pthread_mutexattr_setkind_np failed" );
279 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
280 Error( "pthread_mutex_init failed" );
284 if ( pthread_attr_create( &attrib ) == -1 ) {
285 Error( "pthread_attr_create failed" );
287 if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
288 Error( "pthread_attr_setstacksize failed" );
291 for ( i = 0 ; i < numthreads ; i++ )
293 if ( pthread_create( &work_threads[i], attrib
294 , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
295 Error( "pthread_create failed" );
299 for ( i = 0 ; i < numthreads ; i++ )
301 if ( pthread_join( work_threads[i], &status ) == -1 ) {
302 Error( "pthread_join failed" );
310 Sys_Printf( " (%i)\n", end - start );
318 ===================================================================
322 ===================================================================
326 #include <abi_mutex.h>
327 #include <sys/types.h>
328 #include <sys/prctl.h>
333 void ThreadSetDefault( void ){
334 if ( numthreads == -1 ) {
335 numthreads = prctl( PR_MAXPPROCS );
337 Sys_Printf( "%i threads\n", numthreads );
338 usconfig( CONF_INITUSERS, numthreads );
342 void ThreadLock( void ){
346 void ThreadUnlock( void ){
347 release_lock( &lck );
356 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
358 int pid[MAX_THREADS];
361 start = I_FloatTime();
365 pacifier = showpacifier;
369 setbuf( stdout, NULL );
374 for ( i = 0 ; i < numthreads - 1 ; i++ )
376 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
377 , NULL, 0x200000 ); // 2 meg stacks
378 if ( pid[i] == -1 ) {
380 Error( "sproc failed" );
386 for ( i = 0 ; i < numthreads - 1 ; i++ )
393 Sys_Printf( " (%i)\n", end - start );
398 #elif GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS
401 =======================================================================
405 =======================================================================
410 void ThreadSetDefault( void ){
411 if ( numthreads == -1 ) { // not set manually
412 /* default to one thread, only multi-thread when specifically told to */
415 if ( numthreads > 1 ) {
416 Sys_Printf( "threads: %d\n", numthreads );
422 typedef struct pt_mutex_s
425 pthread_mutex_t a_mutex;
430 pt_mutex_t global_lock;
432 void ThreadLock( void ){
433 pt_mutex_t *pt_mutex = &global_lock;
439 pthread_mutex_lock( &pt_mutex->a_mutex );
440 if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
445 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
446 pt_mutex->owner = (pthread_t *)pthread_self();
453 pthread_cond_wait( &pt_mutex->cond, &pt_mutex->a_mutex );
454 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
455 pt_mutex->owner = (pthread_t *)pthread_self();
462 pthread_mutex_unlock( &pt_mutex->a_mutex );
465 void ThreadUnlock( void ){
466 pt_mutex_t *pt_mutex = &global_lock;
472 pthread_mutex_lock( &pt_mutex->a_mutex );
475 if ( pt_mutex->lock == 0 ) {
476 pt_mutex->owner = NULL;
477 pthread_cond_signal( &pt_mutex->cond );
480 pthread_mutex_unlock( &pt_mutex->a_mutex );
483 void recursive_mutex_init( pthread_mutexattr_t attribs ){
484 pt_mutex_t *pt_mutex = &global_lock;
486 pt_mutex->owner = NULL;
487 if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
488 Error( "pthread_mutex_init failed\n" );
490 if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
491 Error( "pthread_cond_init failed\n" );
502 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
503 pthread_mutexattr_t mattrib;
504 pthread_t work_threads[MAX_THREADS];
507 int i = 0, status = 0;
509 start = I_FloatTime();
510 pacifier = showpacifier;
516 if ( numthreads == 1 ) {
524 setbuf( stdout, NULL );
527 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
528 Error( "pthread_mutexattr_init failed" );
530 #if __GLIBC_MINOR__ == 1
531 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_FAST_NP ) != 0 )
533 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ADAPTIVE_NP ) != 0 )
535 { Error( "pthread_mutexattr_settype failed" ); }
536 recursive_mutex_init( mattrib );
538 for ( i = 0 ; i < numthreads ; i++ )
540 /* Default pthread attributes: joinable & non-realtime scheduling */
541 if ( pthread_create( &work_threads[i], NULL, (void*)func, (void*)i ) != 0 ) {
542 Error( "pthread_create failed" );
545 for ( i = 0 ; i < numthreads ; i++ )
547 if ( pthread_join( work_threads[i], (void **)&status ) != 0 ) {
548 Error( "pthread_join failed" );
551 pthread_mutexattr_destroy( &mattrib );
557 Sys_Printf( " (%i)\n", end - start );
565 =======================================================================
569 =======================================================================
574 void ThreadSetDefault( void ){
578 void ThreadLock( void ){
581 void ThreadUnlock( void ){
589 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
596 pacifier = showpacifier;
597 start = I_FloatTime();
602 Sys_Printf( " (%i)\n", end - start );