PS2 use WaitSemaEx for waiting for semaphore with timeout

This commit is contained in:
Julian Uy 2023-12-24 14:49:23 +00:00 committed by Sam Lantinga
parent bb0e0ae080
commit 4914e5bb78

View File

@ -26,7 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <timer_alarm.h>
#include <kernel_util.h>
#include <kernel.h>
@ -35,11 +35,6 @@ struct SDL_Semaphore
s32 semid;
};
static void usercb(struct timer_alarm_t *alarm, void *arg)
{
iReleaseWaitThread((int)arg);
}
/* Create a semaphore */
SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
{
@ -80,8 +75,8 @@ void SDL_DestroySemaphore(SDL_Semaphore *sem)
int SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
{
int ret;
struct timer_alarm_t alarm;
InitializeTimerAlarm(&alarm);
u64 timeout_usec;
u64 *timeout_ptr;
if (!sem) {
return SDL_InvalidParamError("sem");
@ -94,12 +89,14 @@ int SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
return 0;
}
timeout_ptr = NULL;
if (timeoutNS != -1) { // -1 == wait indefinitely.
SetTimerAlarm(&alarm, MSec2TimerBusClock(SDL_NS_TO_MS(timeoutNS)), &usercb, (void *)GetThreadId());
timeout_usec = SDL_NS_TO_US(timeoutNS);
timeout_ptr = &timeout_usec;
}
ret = WaitSema(sem->semid);
StopTimerAlarm(&alarm);
ret = WaitSemaEx(sem->semid, 1, timeout_ptr);
if (ret < 0) {
return SDL_MUTEX_TIMEDOUT;