diff --git a/Makefile b/Makefile index 279fc8f9..5595d6ae 100644 --- a/Makefile +++ b/Makefile @@ -46,8 +46,8 @@ STRIP = $(TARGET)-strip endif else EXT= -DPTHREAD=-DUSE_PTHREADS -LPTHREAD=-lpthread +DPTHREAD=-DUSE_PTHREADS -pthread +LPTHREAD=-pthread -lpthread endif #BIN_PFX ?= tyr- @@ -241,7 +241,7 @@ LIGHT_OBJS = \ common/log.o $(BIN_DIR)/$(BIN_PFX)light$(EXT): $(patsubst %,$(BUILD_DIR)/%,$(LIGHT_OBJS)) - $(call do_cc_link,-lm) + $(call do_cc_link,-lm $(LPTHREAD)) $(call do_strip,$@) ####### diff --git a/include/light/threads.h b/include/light/threads.h index 263ce2bd..d800183d 100644 --- a/include/light/threads.h +++ b/include/light/threads.h @@ -20,10 +20,10 @@ #ifndef __LIGHT_THREADS_H__ #define __LIGHT_THREADS_H__ -#ifdef __alpha +#ifdef USE_PTHREADS # include -extern pthread_mutex_t *my_mutex; +extern pthread_mutex_t *my_mutex; # define LOCK pthread_mutex_lock (my_mutex) # define UNLOCK pthread_mutex_unlock (my_mutex) #else @@ -33,7 +33,7 @@ extern pthread_mutex_t *my_mutex; extern int numthreads; -typedef void (threadfunc_t) (void *); +typedef void *(threadfunc_t)(void *); void InitThreads(void); void RunThreadsOn(threadfunc_t func); diff --git a/light/light.c b/light/light.c index 14d5a145..1cf89a06 100644 --- a/light/light.c +++ b/light/light.c @@ -81,7 +81,7 @@ GetLitFileSpace(int size) return buf; } -static void +static void * LightThread(void *junk) { int i; @@ -90,16 +90,19 @@ LightThread(void *junk) LOCK; i = bspfileface++; UNLOCK; + if (i == numfaces) + logprint("\nLighting Completed.\n\n"); + if (i >= numfaces) + return NULL; + if (!facecounter) { printf("Lighting face %i of %i\r", i, numfaces); fflush(stdout); } - if (i >= numfaces) { - logprint("\nLighting Completed.\n\n"); - return; - } LightFace(i, nolightface[i], faceoffset[i]); } + + return NULL; } static void diff --git a/light/threads.c b/light/threads.c index 8cf8f1e1..0b9eb87d 100644 --- a/light/threads.c +++ b/light/threads.c @@ -20,25 +20,29 @@ #include #include -#ifdef __alpha -static pthread_mutex_t *my_mutex; -int numthreads = 4; +#ifdef USE_PTHREADS +#define MAX_THREADS 32 +pthread_mutex_t *my_mutex; #else -int numthreads = 1; +#define MAX_THREADS 1 #endif +int numthreads = 1; + void InitThreads(void) { -#ifdef __alpha +#ifdef USE_PTHREADS + int status; pthread_mutexattr_t mattrib; my_mutex = malloc(sizeof(*my_mutex)); - if (pthread_mutexattr_create(&mattrib) == -1) - Error("pthread_mutex_attr_create failed"); - if (pthread_mutexattr_setkind_np(&mattrib, MUTEX_FAST_NP) == -1) - Error("pthread_mutexattr_setkind_np failed"); - if (pthread_mutex_init(my_mutex, mattrib) == -1) + status = pthread_mutexattr_init(&mattrib); + if (status) + Error("pthread_mutexattr_init failed"); + + status = pthread_mutex_init(my_mutex, &mattrib); + if (status) Error("pthread_mutex_init failed"); #endif } @@ -51,9 +55,9 @@ InitThreads(void) void RunThreadsOn(threadfunc_t func) { -#ifdef __alpha - pthread_t work_threads[256]; - pthread_addr_t status; +#ifdef USE_PTHREADS + int status; + pthread_t threads[MAX_THREADS]; pthread_attr_t attrib; int i; @@ -62,22 +66,30 @@ RunThreadsOn(threadfunc_t func) return; } - if (pthread_attr_create(&attrib) == -1) - Error("pthread_attr_create failed"); + status = pthread_attr_init(&attrib); + if (status) + Error("pthread_attr_init failed"); +#if 0 if (pthread_attr_setstacksize(&attrib, 0x100000) == -1) Error("pthread_attr_setstacksize failed"); +#endif for (i = 0; i < numthreads; i++) { - if (pthread_create(&work_threads[i], attrib, - (pthread_startroutine_t) func, - (pthread_addr_t) i) == -1) + status = pthread_create(&threads[i], &attrib, func, NULL); + if (status) Error("pthread_create failed"); } for (i = 0; i < numthreads; i++) { - if (pthread_join(work_threads[i], &status) == -1) + 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 func(NULL); #endif