From 61fd95853f4b492b289946a9c5650340d4902533 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Sat, 2 Nov 2024 08:49:02 -0700 Subject: [PATCH] Do fixes for Gekko mutexes, implement 3DS mutexes (#377) --- src/rc_compat.c | 35 ++++++++++++++++++++++++++++++----- src/rc_compat.h | 10 +++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/rc_compat.c b/src/rc_compat.c index e2960a7a..0ab9e4dd 100644 --- a/src/rc_compat.c +++ b/src/rc_compat.c @@ -1,4 +1,4 @@ -#if !defined(RC_NO_THREADS) && !defined(_WIN32) && !defined(GEKKO) && (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) < 500) +#if !defined(RC_NO_THREADS) && !defined(_WIN32) && !defined(GEKKO) && !defined(_3DS) && (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) < 500) /* We'll want to use pthread_mutexattr_settype/PTHREAD_MUTEX_RECURSIVE, but glibc only conditionally exposes pthread_mutexattr_settype and PTHREAD_MUTEX_RECURSIVE depending on feature flags * Defining _XOPEN_SOURCE must be done at the top of the source file, before including any headers * pthread_mutexattr_settype/PTHREAD_MUTEX_RECURSIVE are specified the Single UNIX Specification (Version 2, 1997), along with POSIX later on (IEEE Standard 1003.1-2008), so should cover practically any pthread implementation @@ -177,22 +177,47 @@ void rc_mutex_unlock(rc_mutex_t* mutex) void rc_mutex_init(rc_mutex_t* mutex) { - LWP_MutexInit(mutex, NULL); + /* LWP_MutexInit has the handle passed by reference */ + /* Other LWP_Mutex* calls have the handle passed by value */ + LWP_MutexInit(&mutex->handle, 1); } void rc_mutex_destroy(rc_mutex_t* mutex) { - LWP_MutexDestroy(mutex); + LWP_MutexDestroy(mutex->handle); } void rc_mutex_lock(rc_mutex_t* mutex) { - LWP_MutexLock(mutex); + LWP_MutexLock(mutex->handle); } void rc_mutex_unlock(rc_mutex_t* mutex) { - LWP_MutexUnlock(mutex); + LWP_MutexUnlock(mutex->handle); +} + +#elif defined(_3DS) + +void rc_mutex_init(rc_mutex_t* mutex) +{ + RecursiveLock_Init(mutex); +} + +void rc_mutex_destroy(rc_mutex_t* mutex) +{ + /* Nothing to do here */ + (void)mutex; +} + +void rc_mutex_lock(rc_mutex_t* mutex) +{ + RecursiveLock_Lock(mutex); +} + +void rc_mutex_unlock(rc_mutex_t* mutex) +{ + RecursiveLock_Unlock(mutex); } #else diff --git a/src/rc_compat.h b/src/rc_compat.h index 5cfa100a..3f24df2e 100644 --- a/src/rc_compat.h +++ b/src/rc_compat.h @@ -84,7 +84,7 @@ RC_BEGIN_C_DECLS #define rc_mutex_lock(mutex) #define rc_mutex_unlock(mutex) #else - #if defined(_WIN32) + #if defined(_WIN32) typedef struct rc_mutex_t { #if defined(WINVER) && WINVER >= 0x0600 /* Windows Vista and later can use a slim reader/writer (SRW) lock */ @@ -97,6 +97,14 @@ RC_BEGIN_C_DECLS CRITICAL_SECTION critical_section; #endif } rc_mutex_t; + #elif defined(GEKKO) + #include + typedef struct rc_mutex_t { + mutex_t handle; + } rc_mutex_t; + #elif defined(_3DS) + #include <3ds/synchronization.h> + typedef RecursiveLock rc_mutex_t; #else #include typedef pthread_mutex_t rc_mutex_t;