From 260b716dffc15e7d1e9db5c94d0b527f223d3a93 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Sun, 24 Sep 2023 18:46:09 -0400 Subject: [PATCH] Hopefully fix the segfaults in retro_deinit --- src/libretro/platform/thread.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/libretro/platform/thread.cpp b/src/libretro/platform/thread.cpp index 52fa9353..c8dc2c48 100644 --- a/src/libretro/platform/thread.cpp +++ b/src/libretro/platform/thread.cpp @@ -19,9 +19,12 @@ #include #include -#include using Platform::Thread; +struct Platform::Thread { + sthread_t *thread; + bool joined; +}; struct ThreadData { std::function fn; }; @@ -34,20 +37,28 @@ static void function_trampoline(void *param) { Thread *Platform::Thread_Create(std::function func) { #if HAVE_THREADS - return (Thread *) sthread_create(function_trampoline, new ThreadData{std::move(func)}); + return new Thread { + sthread_create(function_trampoline, new ThreadData{std::move(func)}), + false + }; #else return nullptr; #endif } -void Platform::Thread_Free(Thread *thread) { +void Platform::Thread_Wait(Thread *thread) { #if HAVE_THREADS - sthread_detach((sthread_t *) thread); + sthread_join(thread->thread); + thread->joined = true; #endif } -void Platform::Thread_Wait(Thread *thread) { +void Platform::Thread_Free(Thread *thread) { #if HAVE_THREADS - sthread_join((sthread_t *) thread); + if (!thread->joined) + sthread_detach(thread->thread); + + thread->thread = nullptr; + delete thread; #endif }