From 9d4ad79af16538fc3a77ff824f5d8d70cd17baa4 Mon Sep 17 00:00:00 2001 From: Cpasjuste Date: Tue, 13 Mar 2018 20:28:44 +0100 Subject: [PATCH] SWITCH: thread fix --- src/thread/switch/SDL_sysmutex.c | 53 +++++-------------------------- src/thread/switch/SDL_syssem.c | 2 +- src/thread/switch/SDL_systhread.c | 10 +++--- 3 files changed, 15 insertions(+), 50 deletions(-) diff --git a/src/thread/switch/SDL_sysmutex.c b/src/thread/switch/SDL_sysmutex.c index 6f2b07aee..3789046a9 100644 --- a/src/thread/switch/SDL_sysmutex.c +++ b/src/thread/switch/SDL_sysmutex.c @@ -29,26 +29,21 @@ struct SDL_mutex { - int recursive; - Uint32 owner; - Mutex mutex; + RMutex mtx; }; /* Create a mutex */ SDL_mutex * SDL_CreateMutex(void) { - SDL_mutex *mutex; - - mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex)); + SDL_mutex *mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex)); if (mutex) { - mutexInit(&mutex->mutex); - mutex->recursive = 0; - mutex->owner = 0; + rmutexInit(&mutex->mtx); } else { SDL_OutOfMemory(); } + return mutex; } @@ -65,26 +60,11 @@ SDL_DestroyMutex(SDL_mutex *mutex) int SDL_mutexP(SDL_mutex *mutex) { - Uint32 this_thread; - if (mutex == NULL) { - SDL_SetError("Passed a NULL mutex"); - return -1; + return SDL_SetError("Passed a NULL mutex"); } - this_thread = (Uint32) SDL_ThreadID(); - if (mutex->owner == this_thread) { - ++mutex->recursive; - } - else { - /* The order of operations is important. - We set the locking thread id after we obtain the lock - so unlocks from other threads will fail. - */ - mutexLock(&mutex->mutex); - mutex->owner = this_thread; - mutex->recursive = 0; - } + rmutexLock(&mutex->mtx); return 0; } @@ -94,28 +74,11 @@ int SDL_mutexV(SDL_mutex *mutex) { if (mutex == NULL) { - SDL_SetError("Passed a NULL mutex"); - return -1; + return SDL_SetError("Passed a NULL mutex"); } - /* If we don't own the mutex, we can't unlock it */ - if (SDL_ThreadID() != mutex->owner) { - SDL_SetError("mutex not owned by this thread"); - return -1; - } + rmutexUnlock(&mutex->mtx); - if (mutex->recursive) { - --mutex->recursive; - } - else { - /* The order of operations is important. - First reset the owner so another thread doesn't lock - the mutex and set the ownership before we reset it, - then release the lock semaphore. - */ - mutex->owner = 0; - mutexUnlock(&mutex->mutex); - } return 0; } diff --git a/src/thread/switch/SDL_syssem.c b/src/thread/switch/SDL_syssem.c index 9863d3637..6a8d20701 100644 --- a/src/thread/switch/SDL_syssem.c +++ b/src/thread/switch/SDL_syssem.c @@ -22,7 +22,7 @@ #if SDL_THREAD_SWITCH -/* Semaphore functions for the VITA. */ +/* Semaphore functions for the SWITCH. */ #include diff --git a/src/thread/switch/SDL_systhread.c b/src/thread/switch/SDL_systhread.c index b7e2b0560..e704d3683 100644 --- a/src/thread/switch/SDL_systhread.c +++ b/src/thread/switch/SDL_systhread.c @@ -25,6 +25,7 @@ /* SWITCH thread management routines for SDL */ #include +#include #include "SDL_error.h" #include "SDL_thread.h" @@ -32,14 +33,15 @@ #define STACK_SIZE 0x2000 -static void ThreadEntry(void *argp) +void +SDL_SYS_RunThread(void *data) { - SDL_RunThread(*(void **) argp); + SDL_RunThread(data); } int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) { - Result res = threadCreate(&thread->handle, ThreadEntry, args, STACK_SIZE, 0x2C, -2); + Result res = threadCreate(&thread->handle, SDL_SYS_RunThread, args, STACK_SIZE, 0x2B, -2); if (res != 0) { return SDL_SetError("threadCreate() failed: 0x%08X", res); } @@ -59,7 +61,7 @@ void SDL_SYS_SetupThread(const char *name) SDL_threadID SDL_ThreadID(void) { - return (SDL_threadID) 0; + return (SDL_threadID) armGetTls(); } void SDL_SYS_WaitThread(SDL_Thread *thread)