SDL: PS2 use WaitSemaEx for waiting for semaphore with timeout (7fa9f)

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

(cherry picked from commit 557d8e2f244ea31db35d177580d4cc3141f6e8f8)
---
 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 bba13a5e1ef3d..de48a62446c4e 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 "SDL_error.h"
 #include "SDL_thread.h"
@@ -38,11 +38,6 @@ struct SDL_semaphore
     s32 semid;
 };
 
-static void usercb(struct timer_alarm_t *alarm, void *arg)
-{
-    iReleaseWaitThread((int)arg);
-}
-
 /* Create a semaphore */
 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
 {
@@ -85,8 +80,8 @@ void SDL_DestroySemaphore(SDL_sem *sem)
 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
 {
     int ret;
-    struct timer_alarm_t alarm;
-    InitializeTimerAlarm(&alarm);
+    u64 timeout_usec;
+    u64 *timeout_ptr;
 
     if (sem == NULL) {
         return SDL_InvalidParamError("sem");
@@ -99,12 +94,14 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
         return 0;
     }
 
+    timeout_ptr = NULL;
+
     if (timeout != SDL_MUTEX_MAXWAIT) {
-        SetTimerAlarm(&alarm, MSec2TimerBusClock(timeout), &usercb, (void *)GetThreadId());
+        timeout_usec = timeout * 1000;
+        timeout_ptr = &timeout_usec;
     }
 
-    ret = WaitSema(sem->semid);
-    StopTimerAlarm(&alarm);
+    ret = WaitSemaEx(sem->semid, 1, timeout_ptr);
 
     if (ret < 0) {
         return SDL_MUTEX_TIMEDOUT;