SWITCH: thread fix

This commit is contained in:
Cpasjuste 2018-03-13 20:28:44 +01:00 committed by Dave Murphy
parent b926068855
commit 9d4ad79af1
3 changed files with 15 additions and 50 deletions

View File

@ -29,26 +29,21 @@
struct SDL_mutex struct SDL_mutex
{ {
int recursive; RMutex mtx;
Uint32 owner;
Mutex mutex;
}; };
/* Create a mutex */ /* Create a mutex */
SDL_mutex * SDL_mutex *
SDL_CreateMutex(void) SDL_CreateMutex(void)
{ {
SDL_mutex *mutex; SDL_mutex *mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if (mutex) { if (mutex) {
mutexInit(&mutex->mutex); rmutexInit(&mutex->mtx);
mutex->recursive = 0;
mutex->owner = 0;
} }
else { else {
SDL_OutOfMemory(); SDL_OutOfMemory();
} }
return mutex; return mutex;
} }
@ -65,26 +60,11 @@ SDL_DestroyMutex(SDL_mutex *mutex)
int int
SDL_mutexP(SDL_mutex *mutex) SDL_mutexP(SDL_mutex *mutex)
{ {
Uint32 this_thread;
if (mutex == NULL) { if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex"); return SDL_SetError("Passed a NULL mutex");
return -1;
} }
this_thread = (Uint32) SDL_ThreadID(); rmutexLock(&mutex->mtx);
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;
}
return 0; return 0;
} }
@ -94,28 +74,11 @@ int
SDL_mutexV(SDL_mutex *mutex) SDL_mutexV(SDL_mutex *mutex)
{ {
if (mutex == NULL) { if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex"); return SDL_SetError("Passed a NULL mutex");
return -1;
} }
/* If we don't own the mutex, we can't unlock it */ rmutexUnlock(&mutex->mtx);
if (SDL_ThreadID() != mutex->owner) {
SDL_SetError("mutex not owned by this thread");
return -1;
}
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; return 0;
} }

View File

@ -22,7 +22,7 @@
#if SDL_THREAD_SWITCH #if SDL_THREAD_SWITCH
/* Semaphore functions for the VITA. */ /* Semaphore functions for the SWITCH. */
#include <stdio.h> #include <stdio.h>

View File

@ -25,6 +25,7 @@
/* SWITCH thread management routines for SDL */ /* SWITCH thread management routines for SDL */
#include <stdio.h> #include <stdio.h>
#include <switch.h>
#include "SDL_error.h" #include "SDL_error.h"
#include "SDL_thread.h" #include "SDL_thread.h"
@ -32,14 +33,15 @@
#define STACK_SIZE 0x2000 #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) 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) { if (res != 0) {
return SDL_SetError("threadCreate() failed: 0x%08X", res); return SDL_SetError("threadCreate() failed: 0x%08X", res);
} }
@ -59,7 +61,7 @@ void SDL_SYS_SetupThread(const char *name)
SDL_threadID SDL_ThreadID(void) SDL_threadID SDL_ThreadID(void)
{ {
return (SDL_threadID) 0; return (SDL_threadID) armGetTls();
} }
void SDL_SYS_WaitThread(SDL_Thread *thread) void SDL_SYS_WaitThread(SDL_Thread *thread)