]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/common/threads.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / tools / quake3 / common / threads.c
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #ifndef _WIN32
23 // The below define is necessary to use
24 // pthreads extensions like pthread_mutexattr_settype
25 #define _GNU_SOURCE
26 #include <pthread.h>
27 #endif
28
29 #include "cmdlib.h"
30 #include "mathlib.h"
31 #include "inout.h"
32 #include "qthreads.h"
33
34 #define MAX_THREADS 64
35
36 int dispatch;
37 int workcount;
38 int oldf;
39 qboolean pacifier;
40
41 qboolean threaded;
42
43 /*
44    =============
45    GetThreadWork
46
47    =============
48  */
49 int GetThreadWork( void ){
50         int r;
51         int f;
52
53         ThreadLock();
54
55         if ( dispatch == workcount ) {
56                 ThreadUnlock();
57                 return -1;
58         }
59
60         f = 10 * dispatch / workcount;
61         if ( f != oldf ) {
62                 oldf = f;
63                 if ( pacifier ) {
64                         Sys_Printf( "%i...", f );
65                         fflush( stdout );   /* ydnar */
66                 }
67         }
68
69         r = dispatch;
70         dispatch++;
71         ThreadUnlock();
72
73         return r;
74 }
75
76
77 void ( *workfunction )( int );
78
79 void ThreadWorkerFunction( int threadnum ){
80         int work;
81
82         while ( 1 )
83         {
84                 work = GetThreadWork();
85                 if ( work == -1 ) {
86                         break;
87                 }
88 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
89                 workfunction( work );
90         }
91 }
92
93 void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
94         if ( numthreads == -1 ) {
95                 ThreadSetDefault();
96         }
97         workfunction = func;
98         RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction );
99 }
100
101
102 /*
103    ===================================================================
104
105    WIN32
106
107    ===================================================================
108  */
109 #ifdef WIN32
110
111 #define USED
112
113 #include <windows.h>
114
115 int numthreads = -1;
116 CRITICAL_SECTION crit;
117 static int enter;
118
119 void ThreadSetDefault( void ){
120         SYSTEM_INFO info;
121
122         if ( numthreads == -1 ) { // not set manually
123                 GetSystemInfo( &info );
124                 numthreads = info.dwNumberOfProcessors;
125                 if ( numthreads < 1 || numthreads > 32 ) {
126                         numthreads = 1;
127                 }
128         }
129
130         Sys_Printf( "%i threads\n", numthreads );
131 }
132
133
134 void ThreadLock( void ){
135         if ( !threaded ) {
136                 return;
137         }
138         EnterCriticalSection( &crit );
139         if ( enter ) {
140                 Error( "Recursive ThreadLock\n" );
141         }
142         enter = 1;
143 }
144
145 void ThreadUnlock( void ){
146         if ( !threaded ) {
147                 return;
148         }
149         if ( !enter ) {
150                 Error( "ThreadUnlock without lock\n" );
151         }
152         enter = 0;
153         LeaveCriticalSection( &crit );
154 }
155
156 /*
157    =============
158    RunThreadsOn
159    =============
160  */
161 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
162         int threadid[MAX_THREADS];
163         HANDLE threadhandle[MAX_THREADS];
164         int i;
165         int start, end;
166
167         start = I_FloatTime();
168         dispatch = 0;
169         workcount = workcnt;
170         oldf = -1;
171         pacifier = showpacifier;
172         threaded = qtrue;
173
174         //
175         // run threads in parallel
176         //
177         InitializeCriticalSection( &crit );
178
179         if ( numthreads == 1 ) { // use same thread
180                 func( 0 );
181         }
182         else
183         {
184                 for ( i = 0 ; i < numthreads ; i++ )
185                 {
186                         threadhandle[i] = CreateThread(
187                                 NULL,   // LPSECURITY_ATTRIBUTES lpsa,
188                             //0,                // DWORD cbStack,
189
190                             /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
191                                 ( 4096 * 1024 ),
192
193                                 (LPTHREAD_START_ROUTINE)func,   // LPTHREAD_START_ROUTINE lpStartAddr,
194                                 (LPVOID)i,  // LPVOID lpvThreadParm,
195                                 0,          //   DWORD fdwCreate,
196                                 &threadid[i] );
197                 }
198
199                 for ( i = 0 ; i < numthreads ; i++ )
200                         WaitForSingleObject( threadhandle[i], INFINITE );
201         }
202         DeleteCriticalSection( &crit );
203
204         threaded = qfalse;
205         end = I_FloatTime();
206         if ( pacifier ) {
207                 Sys_Printf( " (%i)\n", end - start );
208         }
209 }
210
211
212 #endif
213
214 /*
215    ===================================================================
216
217    OSF1
218
219    ===================================================================
220  */
221
222 #ifdef __osf__
223 #define USED
224
225 int numthreads = 4;
226
227 void ThreadSetDefault( void ){
228         if ( numthreads == -1 ) { // not set manually
229                 numthreads = 4;
230         }
231 }
232
233
234 #include <pthread.h>
235
236 pthread_mutex_t *my_mutex;
237
238 void ThreadLock( void ){
239         if ( my_mutex ) {
240                 pthread_mutex_lock( my_mutex );
241         }
242 }
243
244 void ThreadUnlock( void ){
245         if ( my_mutex ) {
246                 pthread_mutex_unlock( my_mutex );
247         }
248 }
249
250
251 /*
252    =============
253    RunThreadsOn
254    =============
255  */
256 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
257         int i;
258         pthread_t work_threads[MAX_THREADS];
259         pthread_addr_t status;
260         pthread_attr_t attrib;
261         pthread_mutexattr_t mattrib;
262         int start, end;
263
264         start = I_FloatTime();
265         dispatch = 0;
266         workcount = workcnt;
267         oldf = -1;
268         pacifier = showpacifier;
269         threaded = qtrue;
270
271         if ( pacifier ) {
272                 setbuf( stdout, NULL );
273         }
274
275         if ( !my_mutex ) {
276                 my_mutex = safe_malloc( sizeof( *my_mutex ) );
277                 if ( pthread_mutexattr_create( &mattrib ) == -1 ) {
278                         Error( "pthread_mutex_attr_create failed" );
279                 }
280                 if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) {
281                         Error( "pthread_mutexattr_setkind_np failed" );
282                 }
283                 if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) {
284                         Error( "pthread_mutex_init failed" );
285                 }
286         }
287
288         if ( pthread_attr_create( &attrib ) == -1 ) {
289                 Error( "pthread_attr_create failed" );
290         }
291         if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) {
292                 Error( "pthread_attr_setstacksize failed" );
293         }
294
295         for ( i = 0 ; i < numthreads ; i++ )
296         {
297                 if ( pthread_create( &work_threads[i], attrib
298                                                          , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) {
299                         Error( "pthread_create failed" );
300                 }
301         }
302
303         for ( i = 0 ; i < numthreads ; i++ )
304         {
305                 if ( pthread_join( work_threads[i], &status ) == -1 ) {
306                         Error( "pthread_join failed" );
307                 }
308         }
309
310         threaded = qfalse;
311
312         end = I_FloatTime();
313         if ( pacifier ) {
314                 Sys_Printf( " (%i)\n", end - start );
315         }
316 }
317
318
319 #endif
320
321 /*
322    ===================================================================
323
324    IRIX
325
326    ===================================================================
327  */
328
329 #ifdef _MIPS_ISA
330 #define USED
331
332 #include <task.h>
333 #include <abi_mutex.h>
334 #include <sys/types.h>
335 #include <sys/prctl.h>
336
337
338 int numthreads = -1;
339 abilock_t lck;
340
341 void ThreadSetDefault( void ){
342         if ( numthreads == -1 ) {
343                 numthreads = prctl( PR_MAXPPROCS );
344         }
345         Sys_Printf( "%i threads\n", numthreads );
346         usconfig( CONF_INITUSERS, numthreads );
347 }
348
349
350 void ThreadLock( void ){
351         spin_lock( &lck );
352 }
353
354 void ThreadUnlock( void ){
355         release_lock( &lck );
356 }
357
358
359 /*
360    =============
361    RunThreadsOn
362    =============
363  */
364 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
365         int i;
366         int pid[MAX_THREADS];
367         int start, end;
368
369         start = I_FloatTime();
370         dispatch = 0;
371         workcount = workcnt;
372         oldf = -1;
373         pacifier = showpacifier;
374         threaded = qtrue;
375
376         if ( pacifier ) {
377                 setbuf( stdout, NULL );
378         }
379
380         init_lock( &lck );
381
382         for ( i = 0 ; i < numthreads - 1 ; i++ )
383         {
384                 pid[i] = sprocsp( ( void ( * )( void *, size_t ) )func, PR_SALL, (void *)i
385                                                   , NULL, 0x200000 ); // 2 meg stacks
386                 if ( pid[i] == -1 ) {
387                         perror( "sproc" );
388                         Error( "sproc failed" );
389                 }
390         }
391
392         func( i );
393
394         for ( i = 0 ; i < numthreads - 1 ; i++ )
395                 wait( NULL );
396
397         threaded = qfalse;
398
399         end = I_FloatTime();
400         if ( pacifier ) {
401                 Sys_Printf( " (%i)\n", end - start );
402         }
403 }
404
405
406 #endif
407
408
409 /*
410    =======================================================================
411
412    Linux pthreads
413
414    =======================================================================
415  */
416
417 #ifdef __linux__
418 #define USED
419
420 int numthreads = 4;
421
422 void ThreadSetDefault( void ){
423         if ( numthreads == -1 ) { // not set manually
424                 /* default to one thread, only multi-thread when specifically told to */
425                 numthreads = 1;
426         }
427         if ( numthreads > 1 ) {
428                 Sys_Printf( "threads: %d\n", numthreads );
429         }
430 }
431
432 #include <pthread.h>
433
434 typedef struct pt_mutex_s
435 {
436         pthread_t       *owner;
437         pthread_mutex_t a_mutex;
438         pthread_cond_t cond;
439         unsigned int lock;
440 } pt_mutex_t;
441
442 pt_mutex_t global_lock;
443
444 void ThreadLock( void ){
445         pt_mutex_t *pt_mutex = &global_lock;
446
447         if ( !threaded ) {
448                 return;
449         }
450
451         pthread_mutex_lock( &pt_mutex->a_mutex );
452         if ( pthread_equal( pthread_self(), (pthread_t)&pt_mutex->owner ) ) {
453                 pt_mutex->lock++;
454         }
455         else
456         {
457                 if ( ( !pt_mutex->owner ) && ( pt_mutex->lock == 0 ) ) {
458                         pt_mutex->owner = (pthread_t *)pthread_self();
459                         pt_mutex->lock  = 1;
460                 }
461                 else
462                 {
463                         while ( 1 )
464                         {
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();
468                                         pt_mutex->lock  = 1;
469                                         break;
470                                 }
471                         }
472                 }
473         }
474         pthread_mutex_unlock( &pt_mutex->a_mutex );
475 }
476
477 void ThreadUnlock( void ){
478         pt_mutex_t *pt_mutex = &global_lock;
479
480         if ( !threaded ) {
481                 return;
482         }
483
484         pthread_mutex_lock( &pt_mutex->a_mutex );
485         pt_mutex->lock--;
486
487         if ( pt_mutex->lock == 0 ) {
488                 pt_mutex->owner = NULL;
489                 pthread_cond_signal( &pt_mutex->cond );
490         }
491
492         pthread_mutex_unlock( &pt_mutex->a_mutex );
493 }
494
495 void recursive_mutex_init( pthread_mutexattr_t attribs ){
496         pt_mutex_t *pt_mutex = &global_lock;
497
498         pt_mutex->owner = NULL;
499         if ( pthread_mutex_init( &pt_mutex->a_mutex, &attribs ) != 0 ) {
500                 Error( "pthread_mutex_init failed\n" );
501         }
502         if ( pthread_cond_init( &pt_mutex->cond, NULL ) != 0 ) {
503                 Error( "pthread_cond_init failed\n" );
504         }
505
506         pt_mutex->lock = 0;
507 }
508
509 /*
510    =============
511    RunThreadsOn
512    =============
513  */
514 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
515         pthread_mutexattr_t mattrib;
516         pthread_t work_threads[MAX_THREADS];
517
518         int start, end;
519         int i = 0;
520         void *exit_value;
521
522         start     = I_FloatTime();
523         pacifier  = showpacifier;
524
525         dispatch  = 0;
526         oldf      = -1;
527         workcount = workcnt;
528
529         if ( numthreads == 1 ) {
530                 func( 0 );
531         }
532         else
533         {
534                 threaded  = qtrue;
535
536                 if ( pacifier ) {
537                         setbuf( stdout, NULL );
538                 }
539
540                 if ( pthread_mutexattr_init( &mattrib ) != 0 ) {
541                         Error( "pthread_mutexattr_init failed" );
542                 }
543 #if __GLIBC_MINOR__ == 1
544                 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_FAST_NP ) != 0 )
545 #else
546                 if ( pthread_mutexattr_settype( &mattrib, PTHREAD_MUTEX_ADAPTIVE_NP ) != 0 )
547 #endif
548                 { Error( "pthread_mutexattr_settype failed" ); }
549                 recursive_mutex_init( mattrib );
550
551                 for ( i = 0 ; i < numthreads ; i++ )
552                 {
553                         /* Default pthread attributes: joinable & non-realtime scheduling */
554                         if ( pthread_create( &work_threads[i], NULL, (void*)func, (void*)i ) != 0 ) {
555                                 Error( "pthread_create failed" );
556                         }
557                 }
558                 for ( i = 0 ; i < numthreads ; i++ )
559                 {
560                         if ( pthread_join( work_threads[i], &exit_value ) != 0 ) {
561                                 Error( "pthread_join failed" );
562                         }
563                 }
564                 pthread_mutexattr_destroy( &mattrib );
565                 threaded = qfalse;
566         }
567
568         end = I_FloatTime();
569         if ( pacifier ) {
570                 Sys_Printf( " (%i)\n", end - start );
571         }
572 }
573 #endif // ifdef __linux__
574
575
576 /*
577    =======================================================================
578
579    SINGLE THREAD
580
581    =======================================================================
582  */
583
584 #ifndef USED
585
586 int numthreads = 1;
587
588 void ThreadSetDefault( void ){
589         numthreads = 1;
590 }
591
592 void ThreadLock( void ){
593 }
594
595 void ThreadUnlock( void ){
596 }
597
598 /*
599    =============
600    RunThreadsOn
601    =============
602  */
603 void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ){
604         int start, end;
605
606         dispatch = 0;
607         workcount = workcnt;
608         oldf = -1;
609         pacifier = showpacifier;
610         start = I_FloatTime();
611         func( 0 );
612
613         end = I_FloatTime();
614         if ( pacifier ) {
615                 Sys_Printf( " (%i)\n", end - start );
616         }
617 }
618
619 #endif