Ensure thread-local storage is created before it is needed
Jeffrey Pfau jeffrey@endrift.com
Fri, 25 Oct 2013 03:15:31 -0700
1 files changed,
7 insertions(+),
6 deletions(-)
jump to
M
src/gba/gba-thread.c
→
src/gba/gba-thread.c
@@ -7,15 +7,15 @@
#include <stdlib.h> #include <signal.h> -static pthread_key_t contextKey; +static pthread_key_t _contextKey; +static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT; static void _createTLS(void) { - pthread_key_create(&contextKey, 0); + pthread_key_create(&_contextKey, 0); } static void* _GBAThreadRun(void* context) { - static pthread_once_t once = PTHREAD_ONCE_INIT; - pthread_once(&once, _createTLS); + pthread_once(&_contextOnce, _createTLS); #ifdef USE_DEBUGGER struct ARMDebugger debugger;@@ -31,7 +31,7 @@
GBAInit(&gba); threadContext->gba = &gba; gba.sync = &threadContext->sync; - pthread_setspecific(contextKey, threadContext); + pthread_setspecific(_contextKey, threadContext); if (threadContext->renderer) { GBAVideoAssociateRenderer(&gba.video, threadContext->renderer); }@@ -226,7 +226,8 @@ pthread_mutex_unlock(&threadContext->sync.videoFrameMutex);
} struct GBAThread* GBAThreadGetContext(void) { - return pthread_getspecific(contextKey); + pthread_once(&_contextOnce, _createTLS); + return pthread_getspecific(_contextKey); } void GBASyncPostFrame(struct GBASync* sync) {