2 Copyright (C) 1999-2006 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
23 // The below define is necessary to use
24 // pthreads extensions like pthread_mutexattr_settype
34 #define MAX_THREADS 64
49 int GetThreadWork (void)
56 if (dispatch == workcount)
62 f = 10*dispatch / workcount;
68 Sys_Printf ("%i...", f);
69 fflush( stdout ); /* ydnar */
81 void (*workfunction) (int);
83 void ThreadWorkerFunction (int threadnum)
89 work = GetThreadWork ();
92 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
97 void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))
102 RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);
107 ===================================================================
111 ===================================================================
120 CRITICAL_SECTION crit;
123 void ThreadSetDefault (void)
127 if (numthreads == -1) // not set manually
129 GetSystemInfo (&info);
130 numthreads = info.dwNumberOfProcessors;
131 if (numthreads < 1 || numthreads > 32)
135 Sys_Printf ("%i threads\n", numthreads);
139 void ThreadLock (void)
143 EnterCriticalSection (&crit);
145 Error ("Recursive ThreadLock\n");
149 void ThreadUnlock (void)
154 Error ("ThreadUnlock without lock\n");
156 LeaveCriticalSection (&crit);
164 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
166 int threadid[MAX_THREADS];
167 HANDLE threadhandle[MAX_THREADS];
171 start = I_FloatTime ();
175 pacifier = showpacifier;
179 // run threads in parallel
181 InitializeCriticalSection (&crit);
189 for (i=0 ; i<numthreads ; i++)
191 threadhandle[i] = CreateThread(
192 NULL, // LPSECURITY_ATTRIBUTES lpsa,
193 //0, // DWORD cbStack,
195 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
198 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
199 (LPVOID)i, // LPVOID lpvThreadParm,
200 0, // DWORD fdwCreate,
204 for (i=0 ; i<numthreads ; i++)
205 WaitForSingleObject (threadhandle[i], INFINITE);
207 DeleteCriticalSection (&crit);
210 end = I_FloatTime ();
212 Sys_Printf (" (%i)\n", end-start);
219 ===================================================================
223 ===================================================================
231 void ThreadSetDefault (void)
233 if (numthreads == -1) // not set manually
242 pthread_mutex_t *my_mutex;
244 void ThreadLock (void)
247 pthread_mutex_lock (my_mutex);
250 void ThreadUnlock (void)
253 pthread_mutex_unlock (my_mutex);
262 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
265 pthread_t work_threads[MAX_THREADS];
266 pthread_addr_t status;
267 pthread_attr_t attrib;
268 pthread_mutexattr_t mattrib;
271 start = I_FloatTime ();
275 pacifier = showpacifier;
279 setbuf (stdout, NULL);
283 my_mutex = safe_malloc (sizeof(*my_mutex));
284 if (pthread_mutexattr_create (&mattrib) == -1)
285 Error ("pthread_mutex_attr_create failed");
286 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
287 Error ("pthread_mutexattr_setkind_np failed");
288 if (pthread_mutex_init (my_mutex, mattrib) == -1)
289 Error ("pthread_mutex_init failed");
292 if (pthread_attr_create (&attrib) == -1)
293 Error ("pthread_attr_create failed");
294 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
295 Error ("pthread_attr_setstacksize failed");
297 for (i=0 ; i<numthreads ; i++)
299 if (pthread_create(&work_threads[i], attrib
300 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
301 Error ("pthread_create failed");
304 for (i=0 ; i<numthreads ; i++)
306 if (pthread_join (work_threads[i], &status) == -1)
307 Error ("pthread_join failed");
312 end = I_FloatTime ();
314 Sys_Printf (" (%i)\n", end-start);
321 ===================================================================
325 ===================================================================
332 #include <abi_mutex.h>
333 #include <sys/types.h>
334 #include <sys/prctl.h>
340 void ThreadSetDefault (void)
342 if (numthreads == -1)
343 numthreads = prctl(PR_MAXPPROCS);
344 Sys_Printf ("%i threads\n", numthreads);
345 usconfig (CONF_INITUSERS, numthreads);
349 void ThreadLock (void)
354 void ThreadUnlock (void)
365 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
368 int pid[MAX_THREADS];
371 start = I_FloatTime ();
375 pacifier = showpacifier;
379 setbuf (stdout, NULL);
383 for (i=0 ; i<numthreads-1 ; i++)
385 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
386 , NULL, 0x200000); // 2 meg stacks
390 Error ("sproc failed");
396 for (i=0 ; i<numthreads-1 ; i++)
401 end = I_FloatTime ();
403 Sys_Printf (" (%i)\n", end-start);
411 =======================================================================
415 =======================================================================
418 #if defined(__linux__) || defined(__APPLE__)
423 void ThreadSetDefault (void)
425 if (numthreads == -1) // not set manually
427 /* default to one thread, only multi-thread when specifically told to */
431 Sys_Printf("threads: %d\n", numthreads);
436 typedef struct pt_mutex_s
439 pthread_mutex_t a_mutex;
444 pt_mutex_t global_lock;
446 void ThreadLock(void)
448 pt_mutex_t *pt_mutex = &global_lock;
453 pthread_mutex_lock(&pt_mutex->a_mutex);
454 if(pthread_equal(pthread_self(), (pthread_t)&pt_mutex->owner))
458 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
460 pt_mutex->owner = (pthread_t *)pthread_self();
467 pthread_cond_wait(&pt_mutex->cond, &pt_mutex->a_mutex);
468 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
470 pt_mutex->owner = (pthread_t *)pthread_self();
477 pthread_mutex_unlock(&pt_mutex->a_mutex);
480 void ThreadUnlock(void)
482 pt_mutex_t *pt_mutex = &global_lock;
487 pthread_mutex_lock(&pt_mutex->a_mutex);
490 if(pt_mutex->lock == 0)
492 pt_mutex->owner = NULL;
493 pthread_cond_signal(&pt_mutex->cond);
496 pthread_mutex_unlock(&pt_mutex->a_mutex);
499 void recursive_mutex_init(pthread_mutexattr_t attribs)
501 pt_mutex_t *pt_mutex = &global_lock;
503 pt_mutex->owner = NULL;
504 if(pthread_mutex_init(&pt_mutex->a_mutex, &attribs) != 0)
505 Error("pthread_mutex_init failed\n");
506 if(pthread_cond_init(&pt_mutex->cond, NULL) != 0)
507 Error("pthread_cond_init failed\n");
517 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
519 pthread_mutexattr_t mattrib;
520 pthread_t work_threads[MAX_THREADS];
525 start = I_FloatTime ();
526 pacifier = showpacifier;
539 setbuf(stdout, NULL);
541 if(pthread_mutexattr_init(&mattrib) != 0)
542 Error("pthread_mutexattr_init failed");
543 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_ERRORCHECK) != 0)
544 Error ("pthread_mutexattr_settype failed");
545 recursive_mutex_init(mattrib);
547 for (i=0 ; i<numthreads ; i++)
549 /* Default pthread attributes: joinable & non-realtime scheduling */
550 if(pthread_create(&work_threads[i], NULL, (void*)func, (void*)i) != 0)
551 Error("pthread_create failed");
553 for (i=0 ; i<numthreads ; i++)
555 if(pthread_join(work_threads[i], (void **)&status) != 0)
556 Error("pthread_join failed");
558 pthread_mutexattr_destroy(&mattrib);
562 end = I_FloatTime ();
564 Sys_Printf (" (%i)\n", end-start);
566 #endif // ifdef __linux__
570 =======================================================================
574 =======================================================================
581 void ThreadSetDefault (void)
586 void ThreadLock (void)
590 void ThreadUnlock (void)
599 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
607 pacifier = showpacifier;
608 start = I_FloatTime ();
611 end = I_FloatTime ();
613 Sys_Printf (" (%i)\n", end-start);