From 59f7f62b62f2fe484afbd987eac08af27129ac90 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 17 Feb 2024 18:14:49 -0600 Subject: [PATCH] Fix thread detachment on Vita --- src/Platform.c | 14 +++++++++++--- src/PlatformThreads.h | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index 1d6e064..4c2d98a 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -88,7 +88,13 @@ void* ThreadProc(void* context) { ctx->entry(ctx->context); #if defined(__vita__) - ctx->thread->alive = false; + if (ctx->thread->detached) { + free(ctx); + sceKernelExitDeleteThread(0); + } + else { + free(ctx); + } #else free(ctx); #endif @@ -192,6 +198,7 @@ void PltJoinThread(PLT_THREAD* thread) { WaitForSingleObjectEx(thread->handle, INFINITE, FALSE); CloseHandle(thread->handle); #elif defined(__vita__) + LC_ASSERT(!thread->detached); sceKernelWaitThreadEnd(thread->handle, NULL, NULL); sceKernelDeleteThread(thread->handle); #elif defined(__WIIU__) @@ -213,7 +220,8 @@ void PltDetachThread(PLT_THREAD* thread) { // "Closing a thread handle does not terminate the associated thread or remove the thread object." CloseHandle(thread->handle); #elif defined(__vita__) - sceKernelDeleteThread(thread->handle); + LC_ASSERT(!thread->detached); + thread->detached = true; #elif defined(__WIIU__) OSDetachThread(&thread->thread); #elif defined(__3DS__) @@ -261,7 +269,7 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE } #elif defined(__vita__) { - thread->alive = true; + thread->detached = false; thread->context = ctx; ctx->thread = thread; thread->handle = sceKernelCreateThread(name, ThreadProc, 0, 0x40000, 0, 0, NULL); diff --git a/src/PlatformThreads.h b/src/PlatformThreads.h index a8ca351..8a4cbc6 100644 --- a/src/PlatformThreads.h +++ b/src/PlatformThreads.h @@ -19,7 +19,7 @@ typedef struct _PLT_THREAD { int handle; int cancelled; void *context; - bool alive; + bool detached; } PLT_THREAD; #elif defined(__WIIU__) typedef OSFastMutex PLT_MUTEX;