Skip to content

Commit

Permalink
Do fixes for Gekko mutexes, implement 3DS mutexes (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
CasualPokePlayer authored Nov 2, 2024
1 parent 580ed79 commit 61fd958
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
35 changes: 30 additions & 5 deletions src/rc_compat.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/rc_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -97,6 +97,14 @@ RC_BEGIN_C_DECLS
CRITICAL_SECTION critical_section;
#endif
} rc_mutex_t;
#elif defined(GEKKO)
#include <ogcsys.h>
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 <pthread.h>
typedef pthread_mutex_t rc_mutex_t;
Expand Down

0 comments on commit 61fd958

Please sign in to comment.