Core: Migrate thread context TLS to its own key

This commit is contained in:
Vicki Pfau 2024-01-30 01:31:47 -08:00
parent 05bf592dec
commit 1fc37e9962
4 changed files with 54 additions and 14 deletions

View File

@ -44,6 +44,9 @@ struct mStandardLogger {
struct mLogger* mLogGetContext(void);
void mLogSetDefaultLogger(struct mLogger*);
void mLogSetThreadLogger(struct mLogger*);
struct mLogger* mLogGetThreadLogger(void);
int mLogGenerateCategory(const char*, const char*);
const char* mLogCategoryName(int);
const char* mLogCategoryId(int);

View File

@ -128,7 +128,6 @@ void mCoreThreadSetRewinding(struct mCoreThread* threadContext, bool);
void mCoreThreadRewindParamsChanged(struct mCoreThread* threadContext);
struct mCoreThread* mCoreThreadGet(void);
struct mLogger* mCoreThreadLogger(void);
CXX_GUARD_END

View File

@ -6,7 +6,7 @@
#include <mgba/core/log.h>
#include <mgba/core/config.h>
#include <mgba/core/thread.h>
#include <mgba-util/threading.h>
#include <mgba-util/vfs.h>
#define MAX_CATEGORY 64
@ -14,11 +14,56 @@
static struct mLogger* _defaultLogger = NULL;
struct mLogger* mLogGetContext(void) {
struct mLogger* logger = NULL;
#ifndef DISABLE_THREADING
logger = mCoreThreadLogger();
static ThreadLocal _contextKey;
#ifdef USE_PTHREADS
static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
static void _createTLS(void) {
ThreadLocalInitKey(&_contextKey);
}
#elif _WIN32
static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
UNUSED(once);
UNUSED(param);
UNUSED(context);
ThreadLocalInitKey(&_contextKey);
return TRUE;
}
#endif
static void _setupTLS(void) {
#ifdef USE_PTHREADS
pthread_once(&_contextOnce, _createTLS);
#elif _WIN32
InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
#endif
}
#endif
void mLogSetThreadLogger(struct mLogger* logger) {
#ifndef DISABLE_THREADING
_setupTLS();
ThreadLocalSetKey(_contextKey, logger);
#else
UNUSED(logger);
#endif
}
struct mLogger* mLogGetThreadLogger(void) {
#ifndef DISABLE_THREADING
_setupTLS();
return ThreadLocalGetValue(_contextKey);
#else
return NULL;
#endif
}
struct mLogger* mLogGetContext(void) {
struct mLogger* logger = mLogGetThreadLogger();
if (logger) {
return logger;
}

View File

@ -246,6 +246,8 @@ static THREAD_ENTRY _mCoreThreadRun(void* context) {
ThreadLocalSetKey(_contextKey, threadContext);
ThreadSetName("CPU Thread");
mLogSetThreadLogger(&threadContext->logger.d);
#if !defined(_WIN32) && defined(USE_PTHREADS)
sigset_t signals;
sigemptyset(&signals);
@ -762,12 +764,3 @@ struct mCoreThread* mCoreThreadGet(void) {
return NULL;
}
#endif
struct mLogger* mCoreThreadLogger(void) {
struct mCoreThread* thread = mCoreThreadGet();
if (thread) {
return &thread->logger.d;
}
return NULL;
}