light: make pthreads support work

Signed-off-by: Kevin Shanahan <kmshanah@disenchant.net>
This commit is contained in:
Kevin Shanahan 2012-12-20 20:14:39 +10:30
parent f85b221da6
commit 6fab0a5358
4 changed files with 45 additions and 30 deletions

View File

@ -46,8 +46,8 @@ STRIP = $(TARGET)-strip
endif endif
else else
EXT= EXT=
DPTHREAD=-DUSE_PTHREADS DPTHREAD=-DUSE_PTHREADS -pthread
LPTHREAD=-lpthread LPTHREAD=-pthread -lpthread
endif endif
#BIN_PFX ?= tyr- #BIN_PFX ?= tyr-
@ -241,7 +241,7 @@ LIGHT_OBJS = \
common/log.o common/log.o
$(BIN_DIR)/$(BIN_PFX)light$(EXT): $(patsubst %,$(BUILD_DIR)/%,$(LIGHT_OBJS)) $(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,$@) $(call do_strip,$@)
####### #######

View File

@ -20,10 +20,10 @@
#ifndef __LIGHT_THREADS_H__ #ifndef __LIGHT_THREADS_H__
#define __LIGHT_THREADS_H__ #define __LIGHT_THREADS_H__
#ifdef __alpha #ifdef USE_PTHREADS
# include <pthread.h> # include <pthread.h>
extern pthread_mutex_t *my_mutex;
extern pthread_mutex_t *my_mutex;
# define LOCK pthread_mutex_lock (my_mutex) # define LOCK pthread_mutex_lock (my_mutex)
# define UNLOCK pthread_mutex_unlock (my_mutex) # define UNLOCK pthread_mutex_unlock (my_mutex)
#else #else
@ -33,7 +33,7 @@ extern pthread_mutex_t *my_mutex;
extern int numthreads; extern int numthreads;
typedef void (threadfunc_t) (void *); typedef void *(threadfunc_t)(void *);
void InitThreads(void); void InitThreads(void);
void RunThreadsOn(threadfunc_t func); void RunThreadsOn(threadfunc_t func);

View File

@ -81,7 +81,7 @@ GetLitFileSpace(int size)
return buf; return buf;
} }
static void static void *
LightThread(void *junk) LightThread(void *junk)
{ {
int i; int i;
@ -90,16 +90,19 @@ LightThread(void *junk)
LOCK; LOCK;
i = bspfileface++; i = bspfileface++;
UNLOCK; UNLOCK;
if (i == numfaces)
logprint("\nLighting Completed.\n\n");
if (i >= numfaces)
return NULL;
if (!facecounter) { if (!facecounter) {
printf("Lighting face %i of %i\r", i, numfaces); printf("Lighting face %i of %i\r", i, numfaces);
fflush(stdout); fflush(stdout);
} }
if (i >= numfaces) {
logprint("\nLighting Completed.\n\n");
return;
}
LightFace(i, nolightface[i], faceoffset[i]); LightFace(i, nolightface[i], faceoffset[i]);
} }
return NULL;
} }
static void static void

View File

@ -20,25 +20,29 @@
#include <common/cmdlib.h> #include <common/cmdlib.h>
#include <light/threads.h> #include <light/threads.h>
#ifdef __alpha #ifdef USE_PTHREADS
static pthread_mutex_t *my_mutex; #define MAX_THREADS 32
int numthreads = 4; pthread_mutex_t *my_mutex;
#else #else
int numthreads = 1; #define MAX_THREADS 1
#endif #endif
int numthreads = 1;
void void
InitThreads(void) InitThreads(void)
{ {
#ifdef __alpha #ifdef USE_PTHREADS
int status;
pthread_mutexattr_t mattrib; pthread_mutexattr_t mattrib;
my_mutex = malloc(sizeof(*my_mutex)); my_mutex = malloc(sizeof(*my_mutex));
if (pthread_mutexattr_create(&mattrib) == -1) status = pthread_mutexattr_init(&mattrib);
Error("pthread_mutex_attr_create failed"); if (status)
if (pthread_mutexattr_setkind_np(&mattrib, MUTEX_FAST_NP) == -1) Error("pthread_mutexattr_init failed");
Error("pthread_mutexattr_setkind_np failed");
if (pthread_mutex_init(my_mutex, mattrib) == -1) status = pthread_mutex_init(my_mutex, &mattrib);
if (status)
Error("pthread_mutex_init failed"); Error("pthread_mutex_init failed");
#endif #endif
} }
@ -51,9 +55,9 @@ InitThreads(void)
void void
RunThreadsOn(threadfunc_t func) RunThreadsOn(threadfunc_t func)
{ {
#ifdef __alpha #ifdef USE_PTHREADS
pthread_t work_threads[256]; int status;
pthread_addr_t status; pthread_t threads[MAX_THREADS];
pthread_attr_t attrib; pthread_attr_t attrib;
int i; int i;
@ -62,22 +66,30 @@ RunThreadsOn(threadfunc_t func)
return; return;
} }
if (pthread_attr_create(&attrib) == -1) status = pthread_attr_init(&attrib);
Error("pthread_attr_create failed"); if (status)
Error("pthread_attr_init failed");
#if 0
if (pthread_attr_setstacksize(&attrib, 0x100000) == -1) if (pthread_attr_setstacksize(&attrib, 0x100000) == -1)
Error("pthread_attr_setstacksize failed"); Error("pthread_attr_setstacksize failed");
#endif
for (i = 0; i < numthreads; i++) { for (i = 0; i < numthreads; i++) {
if (pthread_create(&work_threads[i], attrib, status = pthread_create(&threads[i], &attrib, func, NULL);
(pthread_startroutine_t) func, if (status)
(pthread_addr_t) i) == -1)
Error("pthread_create failed"); Error("pthread_create failed");
} }
for (i = 0; i < numthreads; i++) { 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"); Error("pthread_join failed");
} }
status = pthread_mutex_destroy(my_mutex);
if (status)
Error("pthread_mutex_destroy failed");
#else #else
func(NULL); func(NULL);
#endif #endif