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 "q2_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_FPrintf( SYS_VRB,"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 // Setting default Threads to 1
\r
120 int numthreads = 1;
\r
121 CRITICAL_SECTION crit;
\r
124 void ThreadSetDefault (void)
\r
128 if (numthreads == -1) // not set manually
\r
130 GetSystemInfo (&info);
\r
131 numthreads = info.dwNumberOfProcessors;
\r
132 if (numthreads < 1 || numthreads > 32)
\r
136 Sys_Printf ("%i threads\n", numthreads);
\r
140 void ThreadLock (void)
\r
144 EnterCriticalSection (&crit);
\r
146 Error ("Recursive ThreadLock\n");
\r
150 void ThreadUnlock (void)
\r
155 Error ("ThreadUnlock without lock\n");
\r
157 LeaveCriticalSection (&crit);
\r
165 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
167 int threadid[MAX_THREADS];
\r
168 HANDLE threadhandle[MAX_THREADS];
\r
172 start = I_FloatTime ();
\r
174 workcount = workcnt;
\r
176 pacifier = showpacifier;
\r
180 // run threads in parallel
\r
182 InitializeCriticalSection (&crit);
\r
184 if (numthreads == 1)
\r
185 { // use same thread
\r
190 for (i=0 ; i<numthreads ; i++)
\r
192 threadhandle[i] = CreateThread(
\r
193 NULL, // LPSECURITY_ATTRIBUTES lpsa,
\r
194 //0, // DWORD cbStack,
\r
196 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
\r
199 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
\r
200 (LPVOID)i, // LPVOID lpvThreadParm,
\r
201 0, // DWORD fdwCreate,
\r
205 for (i=0 ; i<numthreads ; i++)
\r
206 WaitForSingleObject (threadhandle[i], INFINITE);
\r
208 DeleteCriticalSection (&crit);
\r
211 end = I_FloatTime ();
\r
213 Sys_Printf (" (%i)\n", end-start);
\r
220 ===================================================================
\r
224 ===================================================================
\r
230 int numthreads = 4;
\r
232 void ThreadSetDefault (void)
\r
234 if (numthreads == -1) // not set manually
\r
241 #include <pthread.h>
\r
243 pthread_mutex_t *my_mutex;
\r
245 void ThreadLock (void)
\r
248 pthread_mutex_lock (my_mutex);
\r
251 void ThreadUnlock (void)
\r
254 pthread_mutex_unlock (my_mutex);
\r
263 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
266 pthread_t work_threads[MAX_THREADS];
\r
267 pthread_addr_t status;
\r
268 pthread_attr_t attrib;
\r
269 pthread_mutexattr_t mattrib;
\r
272 start = I_FloatTime ();
\r
274 workcount = workcnt;
\r
276 pacifier = showpacifier;
\r
280 setbuf (stdout, NULL);
\r
284 my_mutex = safe_malloc (sizeof(*my_mutex));
\r
285 if (pthread_mutexattr_create (&mattrib) == -1)
\r
286 Error ("pthread_mutex_attr_create failed");
\r
287 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
\r
288 Error ("pthread_mutexattr_setkind_np failed");
\r
289 if (pthread_mutex_init (my_mutex, mattrib) == -1)
\r
290 Error ("pthread_mutex_init failed");
\r
293 if (pthread_attr_create (&attrib) == -1)
\r
294 Error ("pthread_attr_create failed");
\r
295 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
\r
296 Error ("pthread_attr_setstacksize failed");
\r
298 for (i=0 ; i<numthreads ; i++)
\r
300 if (pthread_create(&work_threads[i], attrib
\r
301 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
\r
302 Error ("pthread_create failed");
\r
305 for (i=0 ; i<numthreads ; i++)
\r
307 if (pthread_join (work_threads[i], &status) == -1)
\r
308 Error ("pthread_join failed");
\r
313 end = I_FloatTime ();
\r
315 Sys_Printf (" (%i)\n", end-start);
\r
322 ===================================================================
\r
326 ===================================================================
\r
333 #include <abi_mutex.h>
\r
334 #include <sys/types.h>
\r
335 #include <sys/prctl.h>
\r
338 int numthreads = -1;
\r
341 void ThreadSetDefault (void)
\r
343 if (numthreads == -1)
\r
344 numthreads = prctl(PR_MAXPPROCS);
\r
345 Sys_Printf ("%i threads\n", numthreads);
\r
346 usconfig (CONF_INITUSERS, numthreads);
\r
350 void ThreadLock (void)
\r
355 void ThreadUnlock (void)
\r
357 release_lock (&lck);
\r
366 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
369 int pid[MAX_THREADS];
\r
372 start = I_FloatTime ();
\r
374 workcount = workcnt;
\r
376 pacifier = showpacifier;
\r
380 setbuf (stdout, NULL);
\r
384 for (i=0 ; i<numthreads-1 ; i++)
\r
386 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
\r
387 , NULL, 0x200000); // 2 meg stacks
\r
391 Error ("sproc failed");
\r
397 for (i=0 ; i<numthreads-1 ; i++)
\r
402 end = I_FloatTime ();
\r
404 Sys_Printf (" (%i)\n", end-start);
\r
412 =======================================================================
\r
416 =======================================================================
\r
419 #if defined( __linux__ ) || defined( __APPLE__ )
\r
422 // Setting default Threads to 1
\r
423 int numthreads = 1;
\r
425 void ThreadSetDefault (void)
\r
427 if (numthreads == -1) // not set manually
\r
429 /* default to one thread, only multi-thread when specifically told to */
\r
433 Sys_Printf("threads: %d\n", numthreads);
\r
436 #include <pthread.h>
\r
438 typedef struct pt_mutex_s
\r
441 pthread_mutex_t a_mutex;
\r
442 pthread_cond_t cond;
\r
446 pt_mutex_t global_lock;
\r
448 void ThreadLock(void)
\r
450 pt_mutex_t *pt_mutex = &global_lock;
\r
455 pthread_mutex_lock(&pt_mutex->a_mutex);
\r
456 if(pthread_equal(pthread_self(), (pthread_t)&pt_mutex->owner))
\r
460 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
\r
462 pt_mutex->owner = (pthread_t *)pthread_self();
\r
463 pt_mutex->lock = 1;
\r
469 pthread_cond_wait(&pt_mutex->cond, &pt_mutex->a_mutex);
\r
470 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
\r
472 pt_mutex->owner = (pthread_t *)pthread_self();
\r
473 pt_mutex->lock = 1;
\r
479 pthread_mutex_unlock(&pt_mutex->a_mutex);
\r
482 void ThreadUnlock(void)
\r
484 pt_mutex_t *pt_mutex = &global_lock;
\r
489 pthread_mutex_lock(&pt_mutex->a_mutex);
\r
492 if(pt_mutex->lock == 0)
\r
494 pt_mutex->owner = NULL;
\r
495 pthread_cond_signal(&pt_mutex->cond);
\r
498 pthread_mutex_unlock(&pt_mutex->a_mutex);
\r
501 void recursive_mutex_init(pthread_mutexattr_t attribs)
\r
503 pt_mutex_t *pt_mutex = &global_lock;
\r
505 pt_mutex->owner = NULL;
\r
506 if(pthread_mutex_init(&pt_mutex->a_mutex, &attribs) != 0)
\r
507 Error("pthread_mutex_init failed\n");
\r
508 if(pthread_cond_init(&pt_mutex->cond, NULL) != 0)
\r
509 Error("pthread_cond_init failed\n");
\r
511 pt_mutex->lock = 0;
\r
519 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
521 pthread_mutexattr_t mattrib;
\r
522 pthread_t work_threads[MAX_THREADS];
\r
527 start = I_FloatTime ();
\r
528 pacifier = showpacifier;
\r
532 workcount = workcnt;
\r
534 if(numthreads == 1)
\r
541 setbuf(stdout, NULL);
\r
543 if(pthread_mutexattr_init(&mattrib) != 0)
\r
544 Error("pthread_mutexattr_init failed");
\r
545 #if __GLIBC_MINOR__ == 1
\r
546 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_FAST_NP) != 0)
\r
548 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
\r
550 Error ("pthread_mutexattr_settype failed");
\r
551 recursive_mutex_init(mattrib);
\r
553 for (i=0 ; i<numthreads ; i++)
\r
555 /* Default pthread attributes: joinable & non-realtime scheduling */
\r
556 if(pthread_create(&work_threads[i], NULL, (void*)func, (void*)i) != 0)
\r
557 Error("pthread_create failed");
\r
559 for (i=0 ; i<numthreads ; i++)
\r
561 if(pthread_join(work_threads[i], (void **)&status) != 0)
\r
562 Error("pthread_join failed");
\r
564 pthread_mutexattr_destroy(&mattrib);
\r
568 end = I_FloatTime ();
\r
570 Sys_Printf (" (%i)\n", end-start);
\r
572 #endif // ifdef __linux__
\r
576 =======================================================================
\r
580 =======================================================================
\r
585 int numthreads = 1;
\r
587 void ThreadSetDefault (void)
\r
592 void ThreadLock (void)
\r
596 void ThreadUnlock (void)
\r
605 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
\r
611 workcount = workcnt;
\r
613 pacifier = showpacifier;
\r
614 start = I_FloatTime ();
\r
617 end = I_FloatTime ();
\r
619 Sys_Printf (" (%i)\n", end-start);
\r