From 1f2bd30b14a9b957951397571ccb237c6df02ed6 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 26 May 2019 14:26:35 -0700 Subject: [PATCH] Switch: Fix threading-related crash on second launch --- CHANGES | 1 + include/mgba-util/platform/3ds/threading.h | 4 ++-- include/mgba-util/platform/posix/threading.h | 4 ++-- include/mgba-util/platform/psp2/threading.h | 6 +++--- include/mgba-util/platform/switch/threading.h | 6 +++--- include/mgba-util/platform/windows/threading.h | 4 ++-- include/mgba-util/threading.h | 6 ++++++ src/core/rewind.c | 2 +- src/core/thread.c | 2 +- src/feature/gui/gui-runner.c | 2 +- src/feature/thread-proxy.c | 4 ++-- src/platform/3ds/main.c | 2 +- src/platform/psp2/psp2-context.c | 2 +- 13 files changed, 26 insertions(+), 19 deletions(-) diff --git a/CHANGES b/CHANGES index 4d947d061..965aa411c 100644 --- a/CHANGES +++ b/CHANGES @@ -20,6 +20,7 @@ Emulation fixes: 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 diff --git a/include/mgba-util/platform/3ds/threading.h b/include/mgba-util/platform/3ds/threading.h index 769039615..6ce2432b1 100644 --- a/include/mgba-util/platform/3ds/threading.h +++ b/include/mgba-util/platform/3ds/threading.h @@ -98,8 +98,8 @@ static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) 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) { diff --git a/include/mgba-util/platform/posix/threading.h b/include/mgba-util/platform/posix/threading.h index 38aac8f62..64340c852 100644 --- a/include/mgba-util/platform/posix/threading.h +++ b/include/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) { diff --git a/include/mgba-util/platform/psp2/threading.h b/include/mgba-util/platform/psp2/threading.h index 07388bed8..36364ac95 100644 --- a/include/mgba-util/platform/psp2/threading.h +++ b/include/mgba-util/platform/psp2/threading.h @@ -131,12 +131,12 @@ static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) 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) { diff --git a/include/mgba-util/platform/switch/threading.h b/include/mgba-util/platform/switch/threading.h index 7003317b3..2348b6983 100644 --- a/include/mgba-util/platform/switch/threading.h +++ b/include/mgba-util/platform/switch/threading.h @@ -71,12 +71,12 @@ static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) 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) { diff --git a/include/mgba-util/platform/windows/threading.h b/include/mgba-util/platform/windows/threading.h index 33af88b58..8ea73d3c1 100644 --- a/include/mgba-util/platform/windows/threading.h +++ b/include/mgba-util/platform/windows/threading.h @@ -75,8 +75,8 @@ static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) 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(); } diff --git a/include/mgba-util/threading.h b/include/mgba-util/threading.h index a8b0b2176..3a2a4102f 100644 --- a/include/mgba-util/threading.h +++ b/include/mgba-util/threading.h @@ -29,10 +29,16 @@ CXX_GUARD_START #ifdef _3DS // ctrulib already has a type called Thread #include <3ds/thread.h> +#elif defined(__SWITCH__) +#include #else typedef void* Thread; #endif +#ifdef __SWITCH__ +#include +#else typedef void* Mutex; +#endif typedef void* Condition; static inline int MutexInit(Mutex* mutex) { diff --git a/src/core/rewind.c b/src/core/rewind.c index accb73918..11c39fe2a 100644 --- a/src/core/rewind.c +++ b/src/core/rewind.c @@ -53,7 +53,7 @@ void mCoreRewindContextDeinit(struct mCoreRewindContext* context) { context->onThread = false; MutexUnlock(&context->mutex); ConditionWake(&context->cond); - ThreadJoin(context->thread); + ThreadJoin(&context->thread); MutexDeinit(&context->mutex); ConditionDeinit(&context->cond); } diff --git a/src/core/thread.c b/src/core/thread.c index abd7da973..e9e5cfa7e 100644 --- a/src/core/thread.c +++ b/src/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); diff --git a/src/feature/gui/gui-runner.c b/src/feature/gui/gui-runner.c index 128160cad..b0b1a4ff0 100644 --- a/src/feature/gui/gui-runner.c +++ b/src/feature/gui/gui-runner.c @@ -233,7 +233,7 @@ void mGUIDeinit(struct mGUIRunner* runner) { ConditionWake(&runner->autosave.cond); MutexUnlock(&runner->autosave.mutex); - ThreadJoin(runner->autosave.thread); + ThreadJoin(&runner->autosave.thread); ConditionDeinit(&runner->autosave.cond); MutexDeinit(&runner->autosave.mutex); diff --git a/src/feature/thread-proxy.c b/src/feature/thread-proxy.c index 6dca713ca..29a3161e7 100644 --- a/src/feature/thread-proxy.c +++ b/src/feature/thread-proxy.c @@ -78,7 +78,7 @@ void mVideoThreadProxyDeinit(struct mVideoLogger* logger) { } MutexUnlock(&proxyRenderer->mutex); if (waiting) { - ThreadJoin(proxyRenderer->thread); + ThreadJoin(&proxyRenderer->thread); } RingFIFODeinit(&proxyRenderer->dirtyQueue); ConditionDeinit(&proxyRenderer->fromThreadCond); @@ -94,7 +94,7 @@ void _proxyThreadRecover(struct mVideoThreadProxy* proxyRenderer) { } RingFIFOClear(&proxyRenderer->dirtyQueue); MutexUnlock(&proxyRenderer->mutex); - ThreadJoin(proxyRenderer->thread); + ThreadJoin(&proxyRenderer->thread); proxyRenderer->threadState = PROXY_THREAD_IDLE; ThreadCreate(&proxyRenderer->thread, _proxyThread, proxyRenderer); } diff --git a/src/platform/3ds/main.c b/src/platform/3ds/main.c index 8167b2b27..ea2bc6d45 100644 --- a/src/platform/3ds/main.c +++ b/src/platform/3ds/main.c @@ -958,7 +958,7 @@ int main() { Thread thread2; if (ThreadCreate(&thread2, _core2Test, NULL) == 0) { core2 = true; - ThreadJoin(thread2); + ThreadJoin(&thread2); } mGUIInit(&runner, "3ds"); diff --git a/src/platform/psp2/psp2-context.c b/src/platform/psp2/psp2-context.c index f5d8b1254..6df9181a5 100644 --- a/src/platform/psp2/psp2-context.c +++ b/src/platform/psp2/psp2-context.c @@ -420,7 +420,7 @@ void mPSP2UnloadROM(struct mGUIRunner* runner) { break; } audioContext.running = false; - ThreadJoin(audioThread); + ThreadJoin(&audioThread); } void mPSP2Paused(struct mGUIRunner* runner) {