mirror of https://github.com/encounter/SDL.git
SWITCH: thread fix
This commit is contained in:
parent
b926068855
commit
9d4ad79af1
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
#if SDL_THREAD_SWITCH
|
||||
|
||||
/* Semaphore functions for the VITA. */
|
||||
/* Semaphore functions for the SWITCH. */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
/* SWITCH thread management routines for SDL */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <switch.h>
|
||||
|
||||
#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)
|
||||
|
|
Loading…
Reference in New Issue