diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index a53d67a44..65ebcb323 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -26,6 +26,12 @@ static void* _GBAThreadRun(void* context) { 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 @@ static void* _GBAThreadRun(void* context) { } 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; } diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h index 0ccfe3dcd..57591f6ef 100644 --- a/src/gba/gba-thread.h +++ b/src/gba/gba-thread.h @@ -5,12 +5,17 @@ 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); diff --git a/src/main.c b/src/main.c index 0bd115430..90f3792a2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ #include "gba-thread.h" - +#include "renderers/video-software.h" #include #include @@ -15,8 +15,11 @@ int main(int argc, char** argv) { 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);