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 = 40*dispatch / workcount;
65 Sys_Printf("warning: progress went backwards (should never happen)\n");
74 Sys_Printf("%i", f / 4);
77 fflush( stdout ); /* ydnar */
89 void (*workfunction) (int);
91 void ThreadWorkerFunction (int threadnum)
97 work = GetThreadWork ();
100 //Sys_Printf ("thread %i, work %i\n", threadnum, work);
105 void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))
107 if (numthreads == -1)
110 RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);
115 ===================================================================
119 ===================================================================
128 CRITICAL_SECTION crit;
131 void ThreadSetDefault (void)
135 if (numthreads == -1) // not set manually
137 GetSystemInfo (&info);
138 numthreads = info.dwNumberOfProcessors;
139 if (numthreads < 1 || numthreads > 32)
143 Sys_Printf ("%i threads\n", numthreads);
147 void ThreadLock (void)
151 EnterCriticalSection (&crit);
153 Error ("Recursive ThreadLock\n");
157 void ThreadUnlock (void)
162 Error ("ThreadUnlock without lock\n");
164 LeaveCriticalSection (&crit);
172 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
174 int threadid[MAX_THREADS];
175 HANDLE threadhandle[MAX_THREADS];
179 start = I_FloatTime ();
183 pacifier = showpacifier;
187 // run threads in parallel
189 InitializeCriticalSection (&crit);
197 for (i=0 ; i<numthreads ; i++)
199 threadhandle[i] = CreateThread(
200 NULL, // LPSECURITY_ATTRIBUTES lpsa,
201 //0, // DWORD cbStack,
203 /* ydnar: cranking stack size to eliminate radiosity crash with 1MB stack on win32 */
206 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
207 (LPVOID)i, // LPVOID lpvThreadParm,
208 0, // DWORD fdwCreate,
212 for (i=0 ; i<numthreads ; i++)
213 WaitForSingleObject (threadhandle[i], INFINITE);
215 DeleteCriticalSection (&crit);
218 end = I_FloatTime ();
220 Sys_Printf (" (%i)\n", end-start);
227 ===================================================================
231 ===================================================================
239 void ThreadSetDefault (void)
241 if (numthreads == -1) // not set manually
250 pthread_mutex_t *my_mutex;
252 void ThreadLock (void)
255 pthread_mutex_lock (my_mutex);
258 void ThreadUnlock (void)
261 pthread_mutex_unlock (my_mutex);
270 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
273 pthread_t work_threads[MAX_THREADS];
274 pthread_addr_t status;
275 pthread_attr_t attrib;
276 pthread_mutexattr_t mattrib;
279 start = I_FloatTime ();
283 pacifier = showpacifier;
287 setbuf (stdout, NULL);
291 my_mutex = safe_malloc (sizeof(*my_mutex));
292 if (pthread_mutexattr_create (&mattrib) == -1)
293 Error ("pthread_mutex_attr_create failed");
294 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
295 Error ("pthread_mutexattr_setkind_np failed");
296 if (pthread_mutex_init (my_mutex, mattrib) == -1)
297 Error ("pthread_mutex_init failed");
300 if (pthread_attr_create (&attrib) == -1)
301 Error ("pthread_attr_create failed");
302 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
303 Error ("pthread_attr_setstacksize failed");
305 for (i=0 ; i<numthreads ; i++)
307 if (pthread_create(&work_threads[i], attrib
308 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
309 Error ("pthread_create failed");
312 for (i=0 ; i<numthreads ; i++)
314 if (pthread_join (work_threads[i], &status) == -1)
315 Error ("pthread_join failed");
320 end = I_FloatTime ();
322 Sys_Printf (" (%i)\n", end-start);
329 ===================================================================
333 ===================================================================
340 #include <abi_mutex.h>
341 #include <sys/types.h>
342 #include <sys/prctl.h>
348 void ThreadSetDefault (void)
350 if (numthreads == -1)
351 numthreads = prctl(PR_MAXPPROCS);
352 Sys_Printf ("%i threads\n", numthreads);
353 usconfig (CONF_INITUSERS, numthreads);
357 void ThreadLock (void)
362 void ThreadUnlock (void)
373 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
376 int pid[MAX_THREADS];
379 start = I_FloatTime ();
383 pacifier = showpacifier;
387 setbuf (stdout, NULL);
391 for (i=0 ; i<numthreads-1 ; i++)
393 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
394 , NULL, 0x200000); // 2 meg stacks
398 Error ("sproc failed");
404 for (i=0 ; i<numthreads-1 ; i++)
409 end = I_FloatTime ();
411 Sys_Printf (" (%i)\n", end-start);
419 =======================================================================
423 =======================================================================
426 #if defined(__linux__) || (defined(__APPLE__) && !MAC_STATIC_HACK)
431 void ThreadSetDefault (void)
433 if (numthreads == -1) // not set manually
435 /* default to one thread, only multi-thread when specifically told to */
439 Sys_Printf("threads: %d\n", numthreads);
444 typedef struct pt_mutex_s
447 pthread_mutex_t a_mutex;
452 pt_mutex_t global_lock;
454 void ThreadLock(void)
456 pt_mutex_t *pt_mutex = &global_lock;
461 pthread_mutex_lock(&pt_mutex->a_mutex);
462 if(pthread_equal(pthread_self(), (pthread_t)&pt_mutex->owner))
466 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
468 pt_mutex->owner = (pthread_t *)pthread_self();
475 pthread_cond_wait(&pt_mutex->cond, &pt_mutex->a_mutex);
476 if((!pt_mutex->owner) && (pt_mutex->lock == 0))
478 pt_mutex->owner = (pthread_t *)pthread_self();
485 pthread_mutex_unlock(&pt_mutex->a_mutex);
488 void ThreadUnlock(void)
490 pt_mutex_t *pt_mutex = &global_lock;
495 pthread_mutex_lock(&pt_mutex->a_mutex);
498 if(pt_mutex->lock == 0)
500 pt_mutex->owner = NULL;
501 pthread_cond_signal(&pt_mutex->cond);
504 pthread_mutex_unlock(&pt_mutex->a_mutex);
507 void recursive_mutex_init(pthread_mutexattr_t attribs)
509 pt_mutex_t *pt_mutex = &global_lock;
511 pt_mutex->owner = NULL;
512 if(pthread_mutex_init(&pt_mutex->a_mutex, &attribs) != 0)
513 Error("pthread_mutex_init failed\n");
514 if(pthread_cond_init(&pt_mutex->cond, NULL) != 0)
515 Error("pthread_cond_init failed\n");
525 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
527 pthread_mutexattr_t mattrib;
529 pthread_t work_threads[MAX_THREADS];
535 start = I_FloatTime ();
536 pacifier = showpacifier;
542 pthread_attr_init(&attr);
543 if(pthread_attr_setstacksize(&attr, 8388608) != 0)
546 pthread_attr_getstacksize(&attr, &stacksize);
547 Sys_Printf("Could not set a per-thread stack size of 8 MB, using only %.2f MB\n", stacksize / 1048576.0);
557 setbuf(stdout, NULL);
559 if(pthread_mutexattr_init(&mattrib) != 0)
560 Error("pthread_mutexattr_init failed");
561 if (pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_ERRORCHECK) != 0)
562 Error ("pthread_mutexattr_settype failed");
563 recursive_mutex_init(mattrib);
565 for (i=0 ; i<numthreads ; i++)
567 /* Default pthread attributes: joinable & non-realtime scheduling */
568 if(pthread_create(&work_threads[i], &attr, (void*)func, (void*)(size_t)i) != 0)
569 Error("pthread_create failed");
571 for (i=0 ; i<numthreads ; i++)
573 if(pthread_join(work_threads[i], (void **)&status) != 0)
574 Error("pthread_join failed");
576 pthread_mutexattr_destroy(&mattrib);
580 end = I_FloatTime ();
582 Sys_Printf (" (%i)\n", end-start);
584 #endif // ifdef __linux__
588 =======================================================================
592 =======================================================================
599 void ThreadSetDefault (void)
604 void ThreadLock (void)
608 void ThreadUnlock (void)
617 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
625 pacifier = showpacifier;
626 start = I_FloatTime ();
629 end = I_FloatTime ();
631 Sys_Printf (" (%i)\n", end-start);