2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
23 // The below define is necessary to use
\r
24 // pthreads extensions like pthread_mutexattr_settype
\r
26 #include <pthread.h>
\r
30 #include "mathlib.h"
\r
32 #include "her2_threads.h"
\r
34 #define MAX_THREADS 64
\r
49 int GetThreadWork (void)
\r
56 if (dispatch == workcount)
\r
62 f = 10*dispatch / workcount;
\r
68 Sys_Printf ("%i...", f);
\r
69 fflush( stdout ); /* ydnar */
\r
81 void (*workfunction) (int);
\r
83 void ThreadWorkerFunction (int threadnum)
\r
89 work = GetThreadWork ();
\r
92 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
\r
97 void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))
\r
99 if (numthreads == -1)
\r
100 ThreadSetDefault ();
\r
101 workfunction = func;
\r
102 RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);
\r
107 ===================================================================
\r
111 ===================================================================
\r
117 #include <windows.h>
\r
119 int numthreads = -1;
\r
120 CRITICAL_SECTION crit;
\r
123 void ThreadSetDefault (void)
\r
127 if (numthreads == -1) // not set manually
\r
129 GetSystemInfo (&info);
\r
130 numthreads = info.dwNumberOfProcessors;
\r
131 if (numthreads < 1 || numthreads > 32)
\r
135 Sys_Printf ("%i threads\n", numthreads);
\r
139 void ThreadLock (void)
\r
143 EnterCriticalSection (&crit);
\r
145 Error ("Recursive ThreadLock\n");
\r
149 void ThreadUnlock (void)
\r
154 Error ("ThreadUnlock without lock\n");
\r
156 LeaveCriticalSection (&crit);
\r
164 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
166 int threadid[MAX_THREADS];
\r
167 HANDLE threadhandle[MAX_THREADS];
\r
171 start = I_FloatTime ();
\r
173 workcount = workcnt;
\r
175 pacifier = showpacifier;
\r
179 // run threads in parallel
\r
181 InitializeCriticalSection (&crit);
\r
183 if (numthreads == 1)
\r
184 { // use same thread
\r
189 for (i=0 ; i<numthreads ; i++)
\r
191 threadhandle[i] = CreateThread(
\r
192 NULL, // LPSECURITY_ATTRIBUTES lpsa,
\r
193 //0, // DWORD cbStack,
\r
195 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
\r
198 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
\r
199 (LPVOID)i, // LPVOID lpvThreadParm,
\r
200 0, // DWORD fdwCreate,
\r
204 for (i=0 ; i<numthreads ; i++)
\r
205 WaitForSingleObject (threadhandle[i], INFINITE);
\r
207 DeleteCriticalSection (&crit);
\r
210 end = I_FloatTime ();
\r
212 Sys_Printf (" (%i)\n", end-start);
\r
219 ===================================================================
\r
223 ===================================================================
\r
229 int numthreads = 4;
\r
231 void ThreadSetDefault (void)
\r
233 if (numthreads == -1) // not set manually
\r
240 #include <pthread.h>
\r
242 pthread_mutex_t *my_mutex;
\r
244 void ThreadLock (void)
\r
247 pthread_mutex_lock (my_mutex);
\r
250 void ThreadUnlock (void)
\r
253 pthread_mutex_unlock (my_mutex);
\r
262 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
265 pthread_t work_threads[MAX_THREADS];
\r
266 pthread_addr_t status;
\r
267 pthread_attr_t attrib;
\r
268 pthread_mutexattr_t mattrib;
\r
271 start = I_FloatTime ();
\r
273 workcount = workcnt;
\r
275 pacifier = showpacifier;
\r
279 setbuf (stdout, NULL);
\r
283 my_mutex = safe_malloc (sizeof(*my_mutex));
\r
284 if (pthread_mutexattr_create (&mattrib) == -1)
\r
285 Error ("pthread_mutex_attr_create failed");
\r
286 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
\r
287 Error ("pthread_mutexattr_setkind_np failed");
\r
288 if (pthread_mutex_init (my_mutex, mattrib) == -1)
\r
289 Error ("pthread_mutex_init failed");
\r
292 if (pthread_attr_create (&attrib) == -1)
\r
293 Error ("pthread_attr_create failed");
\r
294 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
\r
295 Error ("pthread_attr_setstacksize failed");
\r
297 for (i=0 ; i<numthreads ; i++)
\r
299 if (pthread_create(&work_threads[i], attrib
\r
300 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
\r
301 Error ("pthread_create failed");
\r
304 for (i=0 ; i<numthreads ; i++)
\r
306 if (pthread_join (work_threads[i], &status) == -1)
\r
307 Error ("pthread_join failed");
\r
312 end = I_FloatTime ();
\r
314 Sys_Printf (" (%i)\n", end-start);
\r
321 ===================================================================
\r
325 ===================================================================
\r
332 #include <abi_mutex.h>
\r
333 #include <sys/types.h>
\r
334 #include <sys/prctl.h>
\r
337 int numthreads = -1;
\r
340 void ThreadSetDefault (void)
\r
342 if (numthreads == -1)
\r
343 numthreads = prctl(PR_MAXPPROCS);
\r
344 Sys_Printf ("%i threads\n", numthreads);
\r
345 usconfig (CONF_INITUSERS, numthreads);
\r
349 void ThreadLock (void)
\r
354 void ThreadUnlock (void)
\r
356 release_lock (&lck);
\r
365 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
368 int pid[MAX_THREADS];
\r
371 start = I_FloatTime ();
\r
373 workcount = workcnt;
\r
375 pacifier = showpacifier;
\r
379 setbuf (stdout, NULL);
\r
383 for (i=0 ; i<numthreads-1 ; i++)
\r
385 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
\r
386 , NULL, 0x200000); // 2 meg stacks
\r
390 Error ("sproc failed");
\r
396 for (i=0 ; i<numthreads-1 ; i++)
\r
401 end = I_FloatTime ();
\r
403 Sys_Printf (" (%i)\n", end-start);
\r
411 =======================================================================
\r
415 =======================================================================
\r
421 int numthreads = 4;
\r
423 void ThreadSetDefault (void)
\r
425 if (numthreads == -1) // not set manually
\r
427 /* default to one thread, only multi-thread when specifically told to */
\r
431 Sys_Printf("threads: %d\n", numthreads);
\r
434 #include <pthread.h>
\r
436 typedef struct pt_mutex_s
\r
439 pthread_mutex_t a_mutex;
\r
440 pthread_cond_t cond;
\r
444 pt_mutex_t global_lock;
\r
446 void ThreadLock(void)
\r
448 pt_mutex_t *pt_mutex = &global_lock;
\r
453 pthread_mutex_lock(&pt_mutex->a_mutex);
\r
454 if(pthread_equal(pthread_self(), (pthread_t)&pt_mutex->owner))
\r
458 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
\r
460 pt_mutex->owner = (pthread_t *)pthread_self();
\r
461 pt_mutex->lock = 1;
\r
467 pthread_cond_wait(&pt_mutex->cond, &pt_mutex->a_mutex);
\r
468 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
\r
470 pt_mutex->owner = (pthread_t *)pthread_self();
\r
471 pt_mutex->lock = 1;
\r
477 pthread_mutex_unlock(&pt_mutex->a_mutex);
\r
480 void ThreadUnlock(void)
\r
482 pt_mutex_t *pt_mutex = &global_lock;
\r
487 pthread_mutex_lock(&pt_mutex->a_mutex);
\r
490 if(pt_mutex->lock == 0)
\r
492 pt_mutex->owner = NULL;
\r
493 pthread_cond_signal(&pt_mutex->cond);
\r
496 pthread_mutex_unlock(&pt_mutex->a_mutex);
\r
499 void recursive_mutex_init(pthread_mutexattr_t attribs)
\r
501 pt_mutex_t *pt_mutex = &global_lock;
\r
503 pt_mutex->owner = NULL;
\r
504 if(pthread_mutex_init(&pt_mutex->a_mutex, &attribs) != 0)
\r
505 Error("pthread_mutex_init failed\n");
\r
506 if(pthread_cond_init(&pt_mutex->cond, NULL) != 0)
\r
507 Error("pthread_cond_init failed\n");
\r
509 pt_mutex->lock = 0;
\r
517 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
519 pthread_mutexattr_t mattrib;
\r
520 pthread_t work_threads[MAX_THREADS];
\r
525 start = I_FloatTime ();
\r
526 pacifier = showpacifier;
\r
530 workcount = workcnt;
\r
532 if(numthreads == 1)
\r
539 setbuf(stdout, NULL);
\r
541 if(pthread_mutexattr_init(&mattrib) != 0)
\r
542 Error("pthread_mutexattr_init failed");
\r
543 #if __GLIBC_MINOR__ == 1
\r
544 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_FAST_NP) != 0)
\r
546 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
\r
548 Error ("pthread_mutexattr_settype failed");
\r
549 recursive_mutex_init(mattrib);
\r
551 for (i=0 ; i<numthreads ; i++)
\r
553 /* Default pthread attributes: joinable & non-realtime scheduling */
\r
554 if(pthread_create(&work_threads[i], NULL, (void*)func, (void*)i) != 0)
\r
555 Error("pthread_create failed");
\r
557 for (i=0 ; i<numthreads ; i++)
\r
559 if(pthread_join(work_threads[i], (void **)&status) != 0)
\r
560 Error("pthread_join failed");
\r
562 pthread_mutexattr_destroy(&mattrib);
\r
566 end = I_FloatTime ();
\r
568 Sys_Printf (" (%i)\n", end-start);
\r
570 #endif // ifdef __linux__
\r
574 =======================================================================
\r
578 =======================================================================
\r
583 int numthreads = 1;
\r
585 void ThreadSetDefault (void)
\r
590 void ThreadLock (void)
\r
594 void ThreadUnlock (void)
\r
603 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
609 workcount = workcnt;
\r
611 pacifier = showpacifier;
\r
612 start = I_FloatTime ();
\r
615 end = I_FloatTime ();
\r
617 Sys_Printf (" (%i)\n", end-start);
\r