Add ability to get thread-specific GBA
Jeffrey Pfau jeffrey@endrift.com
Mon, 30 Sep 2013 01:42:31 -0700
3 files changed,
23 insertions(+),
1 deletions(-)
M
src/gba/gba-thread.c
→
src/gba/gba-thread.c
@@ -7,7 +7,16 @@
#include <stdlib.h> #include <signal.h> +static pthread_key_t contextKey; + +static void createTLS(void) { + pthread_key_create(&contextKey, 0); +} + static void* _GBAThreadRun(void* context) { + static pthread_once_t once = PTHREAD_ONCE_INIT; + pthread_once(&once, createTLS); + struct ARMDebugger debugger; struct GBA gba; struct GBAThread* threadContext = context;@@ -18,11 +27,12 @@ sigfillset(&signals);
pthread_sigmask(SIG_UNBLOCK, &signals, 0); GBAInit(&gba); + threadContext->gba = &gba; + pthread_setspecific(contextKey, threadContext); if (threadContext->renderer) { GBAVideoAssociateRenderer(&gba.video, threadContext->renderer); } - threadContext->gba = &gba; if (threadContext->fd >= 0) { if (threadContext->fname) { char* dotPoint = strrchr(threadContext->fname, '.');@@ -100,3 +110,7 @@
pthread_mutex_destroy(&threadContext->mutex); pthread_cond_destroy(&threadContext->cond); } + +struct GBAThread* GBAThreadGetContext(void) { + return pthread_getspecific(contextKey); +}
M
src/gba/gba-thread.h
→
src/gba/gba-thread.h
@@ -24,5 +24,6 @@ };
int GBAThreadStart(struct GBAThread* threadContext); void GBAThreadJoin(struct GBAThread* threadContext); +struct GBAThread* GBAThreadGetContext(void); #endif
M
src/gba/gba.c
→
src/gba/gba.c
@@ -2,6 +2,7 @@ #include "gba.h"
#include "gba-bios.h" #include "gba-io.h" +#include "gba-thread.h" #include "debugger.h"@@ -368,6 +369,12 @@ return GBAWaitForIRQ(gba);
} void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) { + if (!gba) { + struct GBAThread* threadContext = GBAThreadGetContext(); + if (threadContext) { + gba = threadContext->gba; + } + } if (gba && level < gba->logLevel) { return; }