Add LightEvent_WaitTimeout

This commit is contained in:
Nanquitas 2020-07-14 18:47:01 +02:00 committed by fincs
parent 3874788e85
commit bc6c097dbb
2 changed files with 36 additions and 0 deletions

View File

@ -305,6 +305,14 @@ int LightEvent_TryWait(LightEvent* event);
*/
void LightEvent_Wait(LightEvent* event);
/**
* @brief Waits on a light event until either the event is signaled or the timeout is reached.
* @param event Pointer to the event.
* @param timeout_ns Timeout in nanoseconds.
* @return Non-zero on timeout, zero otherwise.
*/
int LightEvent_WaitTimeout(LightEvent* event, s64 timeout_ns);
/**
* @brief Initializes a light semaphore.
* @param event Pointer to the semaphore.

View File

@ -318,6 +318,34 @@ void LightEvent_Wait(LightEvent* event)
}
}
int LightEvent_WaitTimeout(LightEvent* event, s64 timeout_ns)
{
Result timeoutRes = 0x09401BFE;
Result res = 0;
while (res != timeoutRes)
{
if (event->state == -2)
{
res = syncArbitrateAddressWithTimeout(&event->state, ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0, timeout_ns);
return res == timeoutRes;
}
if (event->state != -1)
{
if (event->state == 1)
return 0;
if (event->state == 0 && LightEvent_TryReset(event))
return 0;
}
res = syncArbitrateAddressWithTimeout(&event->state, ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0, timeout_ns);
}
return res == timeoutRes;
}
void LightSemaphore_Init(LightSemaphore* semaphore, s16 initial_count, s16 max_count)
{
semaphore->current_count = (s32)initial_count;