Skip to content

Commit

Permalink
rg_system: Added SDL2 mutex support
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Aug 22, 2024
1 parent 83b1937 commit 15fa07e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
41 changes: 41 additions & 0 deletions components/retro-go/rg_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <esp_heap_caps.h>
#include <esp_partition.h>
#include <esp_ota_ops.h>
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions components/retro-go/rg_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 15fa07e

Please sign in to comment.