Add LightSemaphore_TryAcquire and fix Acquire

This commit is contained in:
piepie62 2020-06-23 15:54:53 -04:00 committed by fincs
parent 8662687a2a
commit 9974ed1aa3
2 changed files with 25 additions and 1 deletions

8
libctru/include/3ds/synchronization.h Normal file → Executable file
View File

@ -266,6 +266,14 @@ void LightSemaphore_Init(LightSemaphore* semaphore, s16 initial_count, s16 max_c
*/
void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count);
/**
* @brief Attempts to acquire a light semaphore.
* @param semaphore Pointer to the semaphore.
* @param count Acquire count
* @return Zero on success, non-zero on failure
*/
int LightSemaphore_TryAcquire(LightSemaphore* semaphore, s32 count);
/**
* @brief Releases a light semaphore.
* @param semaphore Pointer to the semaphore.

View File

@ -251,7 +251,7 @@ void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count)
for (;;)
{
old_count = __ldrex(&semaphore->current_count);
if (old_count > 0)
if (old_count >= count)
break;
__clrex();
@ -268,6 +268,22 @@ void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count)
} while (__strex(&semaphore->current_count, old_count - count));
}
int LightSemaphore_TryAcquire(LightSemaphore* semaphore, s32 count)
{
s32 old_count;
do
{
old_count = __ldrex(&semaphore->current_count);
if (old_count < count)
{
__clrex();
return 1; // failure
}
} while (__strex(&semaphore->current_count, old_count - count));
return 0; // success
}
void LightSemaphore_Release(LightSemaphore* semaphore, s32 count)
{
s32 old_count, new_count;