Wait on thread initialization before returning from thread creation

This commit is contained in:
Jeffrey Pfau 2013-04-20 16:40:08 -07:00
parent 9ba9fac898
commit fffe39153f
3 changed files with 32 additions and 2 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -1,5 +1,5 @@
#include "gba-thread.h"
#include "renderers/video-software.h"
#include <fcntl.h>
#include <errno.h>
@ -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);