Wait on thread initialization before returning from thread creation
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Apr 2013 16:40:08 -0700
3 files changed,
32 insertions(+),
2 deletions(-)
M
src/gba/gba-thread.c
→
src/gba/gba-thread.c
@@ -26,6 +26,12 @@ if (threadContext->fd >= 0) {
GBALoadROM(&gba, threadContext->fd); } GBAAttachDebugger(&gba, &debugger); + + threadContext->started = 1; + pthread_mutex_lock(&threadContext->mutex); + pthread_cond_broadcast(&threadContext->cond); + pthread_mutex_unlock(&threadContext->mutex); + ARMDebuggerRun(&debugger); GBADeinit(&gba);@@ -33,5 +39,21 @@ return 0;
} int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) { - return pthread_create(thread, 0, _GBAThreadRun, threadContext); + // TODO: error check + { + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + threadContext->mutex = mutex; + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + threadContext->cond = cond; + } + pthread_mutex_init(&threadContext->mutex, 0); + pthread_cond_init(&threadContext->cond, 0); + + pthread_mutex_lock(&threadContext->mutex); + threadContext->started = 0; + pthread_create(thread, 0, _GBAThreadRun, threadContext); + pthread_cond_wait(&threadContext->cond, &threadContext->mutex); + pthread_mutex_unlock(&threadContext->mutex); + + return 0; }
M
src/gba/gba-thread.h
→
src/gba/gba-thread.h
@@ -5,12 +5,17 @@ #include <pthread.h>
struct GBAThread { // Output + int started; struct GBA* gba; struct ARMDebugger* debugger; // Input struct GBAVideoRenderer* renderer; int fd; + + // Threading state + pthread_mutex_t mutex; + pthread_cond_t cond; }; int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread);
M
src/main.c
→
src/main.c
@@ -1,5 +1,5 @@
#include "gba-thread.h" - +#include "renderers/video-software.h" #include <fcntl.h> #include <errno.h>@@ -15,8 +15,11 @@ sigaddset(&signals, SIGTRAP);
pthread_sigmask(SIG_BLOCK, &signals, 0); struct GBAThread context; + struct GBAVideoSoftwareRenderer renderer; context.fd = fd; + context.renderer = 0; pthread_t thread; + GBAThreadStart(&context, &thread); pthread_join(thread, 0);