#include "SDL_internal.h"
#ifdef SDL_THREAD_N3DS
#include <3ds.h>
struct SDL_Semaphore
{
LightSemaphore semaphore;
};
SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_Semaphore *sem;
if (initial_value > SDL_MAX_SINT16) {
SDL_SetError("Initial semaphore value too high for this platform");
return NULL;
}
sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
if (!sem) {
return NULL;
}
LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16);
return sem;
}
void SDL_DestroySemaphore(SDL_Semaphore *sem)
{
SDL_free(sem);
}
static bool WaitOnSemaphoreFor(SDL_Semaphore *sem, Sint64 timeoutNS)
{
Uint64 stop_time = SDL_GetTicksNS() + timeoutNS;
while (SDL_GetTicksNS() < stop_time) {
if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
return true;
}
SDL_DelayNS(SDL_US_TO_NS(100));
}
SDL_DelayNS(1);
return false;
}
bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
{
if (!sem) {
return true;
}
if (timeoutNS < 0) { LightSemaphore_Acquire(&sem->semaphore, 1);
return true;
}
if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
return true;
}
return WaitOnSemaphoreFor(sem, timeoutNS);
}
Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
{
if (!sem) {
return 0;
}
return sem->semaphore.current_count;
}
void SDL_SignalSemaphore(SDL_Semaphore *sem)
{
if (!sem) {
return;
}
LightSemaphore_Release(&sem->semaphore, 1);
}
#endif