From b4cee4c2862758582fcb1dc25d202d4d5bbcaf2c Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 20 Apr 2013 16:44:03 -0700 Subject: [PATCH] Properly isolate threading --- src/gba/gba-thread.c | 8 ++++++-- src/gba/gba-thread.h | 4 +++- src/main.c | 7 ++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 65ebcb323..666d2d72c 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -38,7 +38,7 @@ static void* _GBAThreadRun(void* context) { return 0; } -int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) { +int GBAThreadStart(struct GBAThread* threadContext) { // TODO: error check { pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -51,9 +51,13 @@ int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) { pthread_mutex_lock(&threadContext->mutex); threadContext->started = 0; - pthread_create(thread, 0, _GBAThreadRun, threadContext); + pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext); pthread_cond_wait(&threadContext->cond, &threadContext->mutex); pthread_mutex_unlock(&threadContext->mutex); return 0; } + +void GBAThreadJoin(struct GBAThread* threadContext) { + pthread_join(threadContext->thread, 0); +} diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h index 57591f6ef..ee20c43fb 100644 --- a/src/gba/gba-thread.h +++ b/src/gba/gba-thread.h @@ -16,8 +16,10 @@ struct GBAThread { // Threading state pthread_mutex_t mutex; pthread_cond_t cond; + pthread_t thread; }; -int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread); +int GBAThreadStart(struct GBAThread* threadContext); +void GBAThreadJoin(struct GBAThread* threadContext); #endif diff --git a/src/main.c b/src/main.c index 90f3792a2..908b9cb74 100644 --- a/src/main.c +++ b/src/main.c @@ -18,11 +18,8 @@ int main(int argc, char** argv) { struct GBAVideoSoftwareRenderer renderer; context.fd = fd; context.renderer = 0; - pthread_t thread; - - GBAThreadStart(&context, &thread); - - pthread_join(thread, 0); + GBAThreadStart(&context); + GBAThreadJoin(&context); close(fd); return 0;