From c42f0e26b4278fff79801dfcd759f1b7cc5caeef Mon Sep 17 00:00:00 2001 From: Kevin Shanahan Date: Wed, 26 Dec 2012 10:38:50 +1030 Subject: [PATCH] vis: use the common threads code Signed-off-by: Kevin Shanahan --- Makefile | 3 ++- vis/vis.c | 66 ++++++++++--------------------------------------------- 2 files changed, 13 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index 11f63aa0..3a2b80cd 100644 --- a/Makefile +++ b/Makefile @@ -252,7 +252,8 @@ VIS_OBJS = \ common/cmdlib.o \ common/mathlib.o \ common/bspfile.o \ - common/log.o + common/log.o \ + common/threads.o $(BIN_DIR)/$(BIN_PFX)vis$(EXT): $(patsubst %,$(BUILD_DIR)/%,$(VIS_OBJS)) $(call do_cc_link,-lm $(LPTHREAD)) diff --git a/vis/vis.c b/vis/vis.c index af82e84d..1e337d4d 100644 --- a/vis/vis.c +++ b/vis/vis.c @@ -5,15 +5,7 @@ #include #include - -#ifdef USE_PTHREADS -#define MAX_THREADS 8 -pthread_mutex_t *my_mutex; -#else -#define MAX_THREADS 1 -#endif - -int numthreads = 1; +#include int numportals; int portalleafs; @@ -27,7 +19,6 @@ int c_noclip = 0; qboolean showgetleaf = true; - static byte *vismap; static byte *vismap_p; static byte *vismap_end; // past visfile @@ -39,7 +30,6 @@ byte *uncompressed; // [leafbytes*portalleafs] int leafbytes; // (portalleafs+63)>>3 int leaflongs; - qboolean fastvis; static int verbose = 0; int testlevel = 2; @@ -345,7 +335,7 @@ GetNextPortal(void) portal_t *p, *ret; unsigned min; - LOCK; + ThreadLock(); min = INT_MAX; ret = NULL; @@ -365,7 +355,7 @@ GetNextPortal(void) fflush(stdout); } - UNLOCK; + ThreadUnlock(); return ret; } @@ -424,7 +414,7 @@ PortalCompleted(portal_t *completed) unsigned long changed; byte *bcheck, bmask; - LOCK; + ThreadLock(); completed->status = pstat_done; @@ -481,7 +471,7 @@ PortalCompleted(portal_t *completed) } } - UNLOCK; + ThreadUnlock(); } @@ -639,46 +629,7 @@ CalcPortalVis(void) return; } -#ifdef USE_PTHREADS - { - int status; - pthread_t threads[MAX_THREADS]; - pthread_attr_t attrib; - pthread_mutexattr_t mattrib; - - my_mutex = malloc(sizeof(*my_mutex)); - pthread_mutexattr_init(&mattrib); - -#if 0 - status = pthread_mutexattr_settype(&mattrib, PTHREAD_MUTEX_FAST_NP); - if (status) - Error("pthread_mutexattr_settype failed"); -#endif - pthread_mutex_init(my_mutex, &mattrib); - - status = pthread_attr_init(&attrib); - if (status) - Error("pthread_attr_init failed"); - - for (i = 0; i < numthreads; i++) { - status = pthread_create(&threads[i], &attrib, LeafThread, NULL); - if (status) - Error("pthread_create failed"); - } - - for (i = 0; i < numthreads; i++) { - status = pthread_join(threads[i], NULL); - if (status) - Error("pthread_join failed"); - } - - status = pthread_mutex_destroy(my_mutex); - if (status) - Error("pthread_mutex_destroy failed"); - } -#else - LeafThread(NULL); -#endif /* USE_PTHREADS */ + RunThreadsOn(numportals, false, LeafThread); if (verbose) { logprint("portalcheck: %i portaltest: %i portalpass: %i\n", @@ -1085,6 +1036,9 @@ main(int argc, char **argv) "(Beta version " __DATE__ " " __TIME__ ")" #endif "\n"); + + numthreads = GetDefaultThreads(); + for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-credits")) { logprint("Original source supplied no obligation by iD Software " @@ -1122,6 +1076,8 @@ main(int argc, char **argv) Error("usage: vis [-threads #] [-level 0-4] [-fast] [-v|-vv] " "[-credits] bspfile"); + logprint("running with %d threads\n", numthreads); + start = I_FloatTime(); strcpy(source, argv[i]);