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