common: add the ability to pass an arg to spawned threads

Signed-off-by: Kevin Shanahan <kmshanah@disenchant.net>
This commit is contained in:
Kevin Shanahan 2013-08-20 19:27:40 +09:30
parent 6ddb109dde
commit d0617760f5
5 changed files with 10 additions and 10 deletions

View File

@ -112,7 +112,7 @@ ThreadUnlock(void)
* ============= * =============
*/ */
void void
RunThreadsOn(int start, int workcnt, void *(func)(void *)) RunThreadsOn(int start, int workcnt, void *(func)(void *), void *arg)
{ {
uintptr_t i; /* avoid warning due to cast for the CreateThread API */ uintptr_t i; /* avoid warning due to cast for the CreateThread API */
DWORD *threadid; DWORD *threadid;
@ -135,7 +135,7 @@ RunThreadsOn(int start, int workcnt, void *(func)(void *))
threadhandle[i] = CreateThread(NULL, threadhandle[i] = CreateThread(NULL,
0, 0,
(LPTHREAD_START_ROUTINE)func, (LPTHREAD_START_ROUTINE)func,
(LPVOID)i, (LPVOID)arg,
0, 0,
&threadid[i]); &threadid[i]);
} }
@ -207,7 +207,7 @@ ThreadUnlock(void)
* ============= * =============
*/ */
void void
RunThreadsOn(int start, int workcnt, void *(func)(void *)) RunThreadsOn(int start, int workcnt, void *(func)(void *), void *arg)
{ {
pthread_t *threads; pthread_t *threads;
pthread_mutexattr_t mattrib; pthread_mutexattr_t mattrib;
@ -246,7 +246,7 @@ RunThreadsOn(int start, int workcnt, void *(func)(void *))
threads_active = true; threads_active = true;
for (i = 0; i < numthreads; i++) { for (i = 0; i < numthreads; i++) {
status = pthread_create(&threads[i], &attrib, func, NULL); status = pthread_create(&threads[i], &attrib, func, arg);
if (status) if (status)
Error("pthread_create failed"); Error("pthread_create failed");
} }
@ -291,13 +291,13 @@ void ThreadUnlock(void) {}
* ============= * =============
*/ */
void void
RunThreadsOn(int start, int workcnt, void *(func)(void *)) RunThreadsOn(int start, int workcnt, void *(func)(void *), void *arg)
{ {
dispatch = start; dispatch = start;
workcount = workcnt; workcount = workcnt;
oldpercent = -1; oldpercent = -1;
func(0); func(arg);
logprint("\n"); logprint("\n");
} }

View File

@ -9,7 +9,7 @@ int GetDefaultThreads(void);
int GetMaxThreads(void); /* returns 0 if no limit specified */ int GetMaxThreads(void); /* returns 0 if no limit specified */
int GetThreadWork(void); int GetThreadWork(void);
int GetThreadWork_Locked__(void); /* caller must take care of locking */ int GetThreadWork_Locked__(void); /* caller must take care of locking */
void RunThreadsOn(int start, int workcnt, void *(func)(void *)); void RunThreadsOn(int start, int workcnt, void *(func)(void *), void *arg);
void ThreadLock(void); void ThreadLock(void);
void ThreadUnlock(void); void ThreadUnlock(void);

View File

@ -194,7 +194,7 @@ LightWorld(void)
lit_file_p = lit_filebase; lit_file_p = lit_filebase;
lit_file_end = lit_filebase + 3 * (MAX_MAP_LIGHTING / 4); lit_file_end = lit_filebase + 3 * (MAX_MAP_LIGHTING / 4);
RunThreadsOn(0, numfaces, LightThread); RunThreadsOn(0, numfaces, LightThread, NULL);
logprint("Lighting Completed.\n\n"); logprint("Lighting Completed.\n\n");
lightdatasize = file_p - filebase; lightdatasize = file_p - filebase;

View File

@ -497,5 +497,5 @@ BasePortalThread(void *dummy)
void void
BasePortalVis(void) BasePortalVis(void)
{ {
RunThreadsOn(0, numportals * 2, BasePortalThread); RunThreadsOn(0, numportals * 2, BasePortalThread, NULL);
} }

View File

@ -722,7 +722,7 @@ CalcPortalVis(void)
if (p->status == pstat_done) if (p->status == pstat_done)
startcount++; startcount++;
} }
RunThreadsOn(startcount, numportals * 2, LeafThread); RunThreadsOn(startcount, numportals * 2, LeafThread, NULL);
if (verbose) { if (verbose) {
logprint("portalcheck: %i portaltest: %i portalpass: %i\n", logprint("portalcheck: %i portaltest: %i portalpass: %i\n",