all repos — mgba @ 1f2bd30b14a9b957951397571ccb237c6df02ed6

mGBA Game Boy Advance Emulator

Switch: Fix threading-related crash on second launch
Vicki Pfau vi@endrift.com
Sun, 26 May 2019 14:26:35 -0700
commit

1f2bd30b14a9b957951397571ccb237c6df02ed6

parent

d839098caef428a564aa83fe8cfbceadd8339da7

M CHANGESCHANGES

@@ -20,6 +20,7 @@ - GB I/O: Filter IE top bits properly (fixes mgba.io/i/1329)

Other fixes: - Qt: Fix some Qt display driver race conditions - Core: Improved lockstep driver reliability (Le Hoang Quyen) + - Switch: Fix threading-related crash on second launch Misc: - GBA Savedata: EEPROM performance fixes - GBA Savedata: Automatically map 1Mbit Flash files as 1Mbit Flash
M include/mgba-util/platform/3ds/threading.hinclude/mgba-util/platform/3ds/threading.h

@@ -98,8 +98,8 @@ *thread = threadCreate(entry, context, 0x8000, 0x18, 2, true);

return !*thread; } -static inline int ThreadJoin(Thread thread) { - return threadJoin(thread, U64_MAX); +static inline int ThreadJoin(Thread* thread) { + return threadJoin(*thread, U64_MAX); } static inline void ThreadSetName(const char* name) {
M include/mgba-util/platform/posix/threading.hinclude/mgba-util/platform/posix/threading.h

@@ -80,8 +80,8 @@ static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {

return pthread_create(thread, 0, entry, context); } -static inline int ThreadJoin(Thread thread) { - return pthread_join(thread, 0); +static inline int ThreadJoin(Thread* thread) { + return pthread_join(*thread, 0); } static inline int ThreadSetName(const char* name) {
M include/mgba-util/platform/psp2/threading.hinclude/mgba-util/platform/psp2/threading.h

@@ -131,12 +131,12 @@ sceKernelStartThread(id, sizeof(args), &args);

return 0; } -static inline int ThreadJoin(Thread thread) { - int res = sceKernelWaitThreadEnd(thread, 0, 0); +static inline int ThreadJoin(Thread* thread) { + int res = sceKernelWaitThreadEnd(*thread, 0, 0); if (res < 0) { return res; } - return sceKernelDeleteThread(thread); + return sceKernelDeleteThread(*thread); } static inline int ThreadSetName(const char* name) {
M include/mgba-util/platform/switch/threading.hinclude/mgba-util/platform/switch/threading.h

@@ -71,12 +71,12 @@ }

return threadStart(thread); } -static inline int ThreadJoin(Thread thread) { - int res = threadWaitForExit(&thread); +static inline int ThreadJoin(Thread* thread) { + int res = threadWaitForExit(thread); if(R_FAILED(res)) { return res; } - return threadClose(&thread); + return threadClose(thread); } static inline void ThreadSetName(const char* name) {
M include/mgba-util/platform/windows/threading.hinclude/mgba-util/platform/windows/threading.h

@@ -75,8 +75,8 @@ *thread = CreateThread(NULL, 0, entry, context, 0, 0);

return GetLastError(); } -static inline int ThreadJoin(Thread thread) { - DWORD error = WaitForSingleObject(thread, INFINITE); +static inline int ThreadJoin(Thread* thread) { + DWORD error = WaitForSingleObject(*thread, INFINITE); if (error == WAIT_FAILED) { return GetLastError(); }
M include/mgba-util/threading.hinclude/mgba-util/threading.h

@@ -29,10 +29,16 @@ #ifdef DISABLE_THREADING

#ifdef _3DS // ctrulib already has a type called Thread #include <3ds/thread.h> +#elif defined(__SWITCH__) +#include <switch/kernel/thread.h> #else typedef void* Thread; #endif +#ifdef __SWITCH__ +#include <switch/kernel/mutex.h> +#else typedef void* Mutex; +#endif typedef void* Condition; static inline int MutexInit(Mutex* mutex) {
M src/core/rewind.csrc/core/rewind.c

@@ -53,7 +53,7 @@ MutexLock(&context->mutex);

context->onThread = false; MutexUnlock(&context->mutex); ConditionWake(&context->cond); - ThreadJoin(context->thread); + ThreadJoin(&context->thread); MutexDeinit(&context->mutex); ConditionDeinit(&context->cond); }
M src/core/thread.csrc/core/thread.c

@@ -413,7 +413,7 @@ void mCoreThreadJoin(struct mCoreThread* threadContext) {

if (!threadContext->impl) { return; } - ThreadJoin(threadContext->impl->thread); + ThreadJoin(&threadContext->impl->thread); MutexDeinit(&threadContext->impl->stateMutex); ConditionDeinit(&threadContext->impl->stateCond);
M src/feature/gui/gui-runner.csrc/feature/gui/gui-runner.c

@@ -233,7 +233,7 @@ runner->autosave.running = false;

ConditionWake(&runner->autosave.cond); MutexUnlock(&runner->autosave.mutex); - ThreadJoin(runner->autosave.thread); + ThreadJoin(&runner->autosave.thread); ConditionDeinit(&runner->autosave.cond); MutexDeinit(&runner->autosave.mutex);
M src/feature/thread-proxy.csrc/feature/thread-proxy.c

@@ -78,7 +78,7 @@ waiting = true;

} MutexUnlock(&proxyRenderer->mutex); if (waiting) { - ThreadJoin(proxyRenderer->thread); + ThreadJoin(&proxyRenderer->thread); } RingFIFODeinit(&proxyRenderer->dirtyQueue); ConditionDeinit(&proxyRenderer->fromThreadCond);

@@ -94,7 +94,7 @@ return;

} RingFIFOClear(&proxyRenderer->dirtyQueue); MutexUnlock(&proxyRenderer->mutex); - ThreadJoin(proxyRenderer->thread); + ThreadJoin(&proxyRenderer->thread); proxyRenderer->threadState = PROXY_THREAD_IDLE; ThreadCreate(&proxyRenderer->thread, _proxyThread, proxyRenderer); }
M src/platform/3ds/main.csrc/platform/3ds/main.c

@@ -958,7 +958,7 @@

Thread thread2; if (ThreadCreate(&thread2, _core2Test, NULL) == 0) { core2 = true; - ThreadJoin(thread2); + ThreadJoin(&thread2); } mGUIInit(&runner, "3ds");
M src/platform/psp2/psp2-context.csrc/platform/psp2/psp2-context.c

@@ -420,7 +420,7 @@ default:

break; } audioContext.running = false; - ThreadJoin(audioThread); + ThreadJoin(&audioThread); } void mPSP2Paused(struct mGUIRunner* runner) {