diff --git a/components/retro-go/rg_system.c b/components/retro-go/rg_system.c index 25b965ff1..b0e9d0b84 100644 --- a/components/retro-go/rg_system.c +++ b/components/retro-go/rg_system.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -614,6 +615,46 @@ size_t rg_queue_spaces_available(rg_queue_t *queue) return uxQueueSpacesAvailable((QueueHandle_t)queue); } +rg_mutex_t *rg_mutex_create(void) +{ +#if defined(ESP_PLATFORM) + return (rg_mutex_t *)xSemaphoreCreateMutex(); +#elif defined(RG_TARGET_SDL2) + return (rg_mutex_t *)SDL2_CreateMutex(); +#endif +} + +void rg_mutex_free(rg_mutex_t *mutex) +{ + if (!mutex) return; +#if defined(ESP_PLATFORM) + vSemaphoreDelete((QueueHandle_t)mutex); +#elif defined(RG_TARGET_SDL2) + SDL2_DestroyMutex((SDL2_Mutex *)mutex); +#endif +} + +bool rg_mutex_give(rg_mutex_t *mutex) +{ + RG_ASSERT_ARG(mutex); +#if defined(ESP_PLATFORM) + return xSemaphoreGive((QueueHandle_t)mutex) == pdPASS; +#elif defined(RG_TARGET_SDL2) + return SDL_UnlockMutex((SDL2_Mutex *)mutex) == 0; +#endif +} + +bool rg_mutex_take(rg_mutex_t *mutex, int timeoutMS) +{ + RG_ASSERT_ARG(mutex); +#if defined(ESP_PLATFORM) + int timeout = timeoutMS >= 0 ? pdMS_TO_TICKS(timeoutMS) : portMAX_DELAY; + return xSemaphoreTake((QueueHandle_t)mutex, timeout) == pdPASS; +#elif defined(RG_TARGET_SDL2) + return SDL_LockMutex((SDL2_Mutex *)mutex) == 0; +#endif +} + void rg_system_load_time(void) { time_t time_sec = RG_MAX(rtcValue, RG_BUILD_TIME); diff --git a/components/retro-go/rg_system.h b/components/retro-go/rg_system.h index 040ca05ad..edc2a67de 100644 --- a/components/retro-go/rg_system.h +++ b/components/retro-go/rg_system.h @@ -269,11 +269,11 @@ bool rg_queue_peek(rg_queue_t *queue, void *out, int timeoutMS); size_t rg_queue_messages_waiting(rg_queue_t *queue); size_t rg_queue_spaces_available(rg_queue_t *queue); -#define rg_mutex_t rg_queue_t -#define rg_mutex_create() rg_queue_create(1, 0) -#define rg_mutex_free(mutex) rg_queue_free(mutex) -#define rg_mutex_give(mutex) rg_queue_send(mutex, NULL, 0) -#define rg_mutex_take(mutex, timeoutMS) rg_queue_receive(mutex, NULL, timeoutMS) +typedef void rg_mutex_t; +rg_mutex_t *rg_mutex_create(void); +void rg_mutex_free(rg_mutex_t *mutex); +bool rg_mutex_give(rg_mutex_t *mutex); +bool rg_mutex_take(rg_mutex_t *mutex, int timeoutMS); char *rg_emu_get_path(rg_path_type_t type, const char *arg); bool rg_emu_save_state(uint8_t slot);