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