Properly isolate threading

This commit is contained in:
Jeffrey Pfau 2013-04-20 16:44:03 -07:00
parent fffe39153f
commit b4cee4c286
3 changed files with 11 additions and 8 deletions

View File

@ -38,7 +38,7 @@ static void* _GBAThreadRun(void* context) {
return 0; return 0;
} }
int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) { int GBAThreadStart(struct GBAThread* threadContext) {
// TODO: error check // TODO: error check
{ {
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
@ -51,9 +51,13 @@ int GBAThreadStart(struct GBAThread* threadContext, pthread_t* thread) {
pthread_mutex_lock(&threadContext->mutex); pthread_mutex_lock(&threadContext->mutex);
threadContext->started = 0; threadContext->started = 0;
pthread_create(thread, 0, _GBAThreadRun, threadContext); pthread_create(&threadContext->thread, 0, _GBAThreadRun, threadContext);
pthread_cond_wait(&threadContext->cond, &threadContext->mutex); pthread_cond_wait(&threadContext->cond, &threadContext->mutex);
pthread_mutex_unlock(&threadContext->mutex); pthread_mutex_unlock(&threadContext->mutex);
return 0; return 0;
} }
void GBAThreadJoin(struct GBAThread* threadContext) {
pthread_join(threadContext->thread, 0);
}

View File

@ -16,8 +16,10 @@ struct GBAThread {
// Threading state // Threading state
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_cond_t cond; 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 #endif

View File

@ -18,11 +18,8 @@ int main(int argc, char** argv) {
struct GBAVideoSoftwareRenderer renderer; struct GBAVideoSoftwareRenderer renderer;
context.fd = fd; context.fd = fd;
context.renderer = 0; context.renderer = 0;
pthread_t thread; GBAThreadStart(&context);
GBAThreadJoin(&context);
GBAThreadStart(&context, &thread);
pthread_join(thread, 0);
close(fd); close(fd);
return 0; return 0;