SDL: PS2 use WaitSemaEx for waiting for semaphore with timeout (4914e)

From 4914e5bb78f12079e14a89b58a12b9bf33faf41f Mon Sep 17 00:00:00 2001
From: Julian Uy <[EMAIL REDACTED]>
Date: Sun, 24 Dec 2023 14:49:23 +0000
Subject: [PATCH] PS2 use WaitSemaEx for waiting for semaphore with timeout

---
 src/thread/ps2/SDL_syssem.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/thread/ps2/SDL_syssem.c b/src/thread/ps2/SDL_syssem.c
index ceb191181ee7..09a846d7a71b 100644
--- a/src/thread/ps2/SDL_syssem.c
+++ b/src/thread/ps2/SDL_syssem.c
@@ -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;