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 ===========================================================================
26 #define MAX_THREADS 64
41 int GetThreadWork (void)
48 if (dispatch == workcount)
54 f = 10*dispatch / workcount;
70 void (*workfunction) (int);
72 void ThreadWorkerFunction (int threadnum)
78 work = GetThreadWork ();
81 //printf ("thread %i, work %i\n", threadnum, work);
86 void RunThreadsOnIndividual (int workcnt, qboolean showpacifier, void(*func)(int))
91 RunThreadsOn (workcnt, showpacifier, ThreadWorkerFunction);
96 ===================================================================
100 ===================================================================
109 CRITICAL_SECTION crit;
112 void ThreadSetDefault (void)
116 if (numthreads == -1) // not set manually
118 GetSystemInfo (&info);
119 numthreads = info.dwNumberOfProcessors;
120 if (numthreads < 1 || numthreads > 32)
124 qprintf ("%i threads\n", numthreads);
128 void ThreadLock (void)
132 EnterCriticalSection (&crit);
134 Error ("Recursive ThreadLock\n");
138 void ThreadUnlock (void)
143 Error ("ThreadUnlock without lock\n");
145 LeaveCriticalSection (&crit);
153 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
155 int threadid[MAX_THREADS];
156 HANDLE threadhandle[MAX_THREADS];
160 start = I_FloatTime ();
164 pacifier = showpacifier;
168 // run threads in parallel
170 InitializeCriticalSection (&crit);
178 for (i=0 ; i<numthreads ; i++)
180 threadhandle[i] = CreateThread(
181 NULL, // LPSECURITY_ATTRIBUTES lpsa,
183 (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr,
184 (LPVOID)i, // LPVOID lpvThreadParm,
185 0, // DWORD fdwCreate,
189 for (i=0 ; i<numthreads ; i++)
190 WaitForSingleObject (threadhandle[i], INFINITE);
192 DeleteCriticalSection (&crit);
195 end = I_FloatTime ();
197 printf (" (%i)\n", end-start);
204 ===================================================================
208 ===================================================================
216 void ThreadSetDefault (void)
218 if (numthreads == -1) // not set manually
227 pthread_mutex_t *my_mutex;
229 void ThreadLock (void)
232 pthread_mutex_lock (my_mutex);
235 void ThreadUnlock (void)
238 pthread_mutex_unlock (my_mutex);
247 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
250 pthread_t work_threads[MAX_THREADS];
251 pthread_addr_t status;
252 pthread_attr_t attrib;
253 pthread_mutexattr_t mattrib;
256 start = I_FloatTime ();
260 pacifier = showpacifier;
264 setbuf (stdout, NULL);
268 my_mutex = malloc (sizeof(*my_mutex));
269 if (pthread_mutexattr_create (&mattrib) == -1)
270 Error ("pthread_mutex_attr_create failed");
271 if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
272 Error ("pthread_mutexattr_setkind_np failed");
273 if (pthread_mutex_init (my_mutex, mattrib) == -1)
274 Error ("pthread_mutex_init failed");
277 if (pthread_attr_create (&attrib) == -1)
278 Error ("pthread_attr_create failed");
279 if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
280 Error ("pthread_attr_setstacksize failed");
282 for (i=0 ; i<numthreads ; i++)
284 if (pthread_create(&work_threads[i], attrib
285 , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
286 Error ("pthread_create failed");
289 for (i=0 ; i<numthreads ; i++)
291 if (pthread_join (work_threads[i], &status) == -1)
292 Error ("pthread_join failed");
297 end = I_FloatTime ();
299 printf (" (%i)\n", end-start);
306 ===================================================================
310 ===================================================================
317 #include <abi_mutex.h>
318 #include <sys/types.h>
319 #include <sys/prctl.h>
325 void ThreadSetDefault (void)
327 if (numthreads == -1)
328 numthreads = prctl(PR_MAXPPROCS);
329 printf ("%i threads\n", numthreads);
331 usconfig (CONF_INITUSERS, numthreads);
335 void ThreadLock (void)
340 void ThreadUnlock (void)
351 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
354 int pid[MAX_THREADS];
357 start = I_FloatTime ();
361 pacifier = showpacifier;
365 setbuf (stdout, NULL);
369 for (i=0 ; i<numthreads-1 ; i++)
371 pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
373 // pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i
378 Error ("sproc failed");
384 for (i=0 ; i<numthreads-1 ; i++)
389 end = I_FloatTime ();
391 printf (" (%i)\n", end-start);
398 =======================================================================
402 =======================================================================
409 void ThreadSetDefault (void)
414 void ThreadLock (void)
418 void ThreadUnlock (void)
427 void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
435 pacifier = showpacifier;
436 start = I_FloatTime ();
439 setbuf (stdout, NULL);
443 end = I_FloatTime ();
445 printf (" (%i)\n", end-start);