2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
25 #include "globaldefs.h"
27 #define MAX_THREADS 64
42 int GetThreadWork (void)
49 if (dispatch == workcount)
55 f = 10*dispatch / workcount;
71 void (*workfunction) (int);
73 void ThreadWorkerFunction (int threadnum)
79 work = GetThreadWork ();
82 //printf ("thread %i, work %i\n", threadnum, work);
87 void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))
92 RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);
97 ===================================================================
101 ===================================================================
110 CRITICAL_SECTION crit;
113 void ThreadSetDefault (void)
117 if (numthreads == -1) // not set manually
119 GetSystemInfo (&info);
120 numthreads = info.dwNumberOfProcessors;
121 if (numthreads < 1 || numthreads > 32)
125 qprintf ("%i threads\n", numthreads);
129 void ThreadLock (void)
133 EnterCriticalSection (&crit);
135 Error ("Recursive ThreadLock\n");
139 void ThreadUnlock (void)
144 Error ("ThreadUnlock without lock\n");
146 LeaveCriticalSection (&crit);
154 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
156 int threadid[MAX_THREADS];
157 HANDLE threadhandle[MAX_THREADS];
161 start = I_FloatTime ();
165 pacifier = showpacifier;
169 // run threads in parallel
171 InitializeCriticalSection (&crit);
179 for (i=0 ; i<numthreads ; i++)
181 threadhandle[i] = CreateThread(
182 NULL, // LPSECURITY_ATTRIBUTES lpsa,
184 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
185 (LPVOID)i, // LPVOID lpvThreadParm,
186 0, // DWORD fdwCreate,
190 for (i=0 ; i<numthreads ; i++)
191 WaitForSingleObject (threadhandle[i], INFINITE);
193 DeleteCriticalSection (&crit);
196 end = I_FloatTime ();
198 printf (" (%i)\n", end-start);
205 ===================================================================
209 ===================================================================
217 void ThreadSetDefault (void)
219 if (numthreads == -1) // not set manually
228 pthread_mutex_t *my_mutex;
230 void ThreadLock (void)
233 pthread_mutex_lock (my_mutex);
236 void ThreadUnlock (void)
239 pthread_mutex_unlock (my_mutex);
248 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
251 pthread_t work_threads[MAX_THREADS];
252 pthread_addr_t status;
253 pthread_attr_t attrib;
254 pthread_mutexattr_t mattrib;
257 start = I_FloatTime ();
261 pacifier = showpacifier;
265 setbuf (stdout, NULL);
269 my_mutex = malloc (sizeof(*my_mutex));
270 if (pthread_mutexattr_create (&mattrib) == -1)
271 Error ("pthread_mutex_attr_create failed");
272 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
273 Error ("pthread_mutexattr_setkind_np failed");
274 if (pthread_mutex_init (my_mutex, mattrib) == -1)
275 Error ("pthread_mutex_init failed");
278 if (pthread_attr_create (&attrib) == -1)
279 Error ("pthread_attr_create failed");
280 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
281 Error ("pthread_attr_setstacksize failed");
283 for (i=0 ; i<numthreads ; i++)
285 if (pthread_create(&work_threads[i], attrib
286 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
287 Error ("pthread_create failed");
290 for (i=0 ; i<numthreads ; i++)
292 if (pthread_join (work_threads[i], &status) == -1)
293 Error ("pthread_join failed");
298 end = I_FloatTime ();
300 printf (" (%i)\n", end-start);
307 ===================================================================
311 ===================================================================
318 #include <abi_mutex.h>
319 #include <sys/types.h>
320 #include <sys/prctl.h>
326 void ThreadSetDefault (void)
328 if (numthreads == -1)
329 numthreads = prctl(PR_MAXPPROCS);
330 printf ("%i threads\n", numthreads);
332 usconfig (CONF_INITUSERS, numthreads);
336 void ThreadLock (void)
341 void ThreadUnlock (void)
352 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
355 int pid[MAX_THREADS];
358 start = I_FloatTime ();
362 pacifier = showpacifier;
366 setbuf (stdout, NULL);
370 for (i=0 ; i<numthreads-1 ; i++)
372 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
374 // pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
379 Error ("sproc failed");
385 for (i=0 ; i<numthreads-1 ; i++)
390 end = I_FloatTime ();
392 printf (" (%i)\n", end-start);
399 =======================================================================
403 =======================================================================
410 void ThreadSetDefault (void)
415 void ThreadLock (void)
419 void ThreadUnlock (void)
428 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
436 pacifier = showpacifier;
437 start = I_FloatTime ();
440 setbuf (stdout, NULL);
444 end = I_FloatTime ();
446 printf (" (%i)\n", end-start);