Add LightSemaphore_TryAcquire and fix Acquire
This commit is contained in:
parent
8662687a2a
commit
9974ed1aa3
8
libctru/include/3ds/synchronization.h
Normal file → Executable file
8
libctru/include/3ds/synchronization.h
Normal file → Executable file
@ -266,6 +266,14 @@ void LightSemaphore_Init(LightSemaphore* semaphore, s16 initial_count, s16 max_c
|
|||||||
*/
|
*/
|
||||||
void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count);
|
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.
|
* @brief Releases a light semaphore.
|
||||||
* @param semaphore Pointer to the semaphore.
|
* @param semaphore Pointer to the semaphore.
|
||||||
|
@ -251,7 +251,7 @@ void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count)
|
|||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
old_count = __ldrex(&semaphore->current_count);
|
old_count = __ldrex(&semaphore->current_count);
|
||||||
if (old_count > 0)
|
if (old_count >= count)
|
||||||
break;
|
break;
|
||||||
__clrex();
|
__clrex();
|
||||||
|
|
||||||
@ -268,6 +268,22 @@ void LightSemaphore_Acquire(LightSemaphore* semaphore, s32 count)
|
|||||||
} while (__strex(&semaphore->current_count, old_count - 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)
|
void LightSemaphore_Release(LightSemaphore* semaphore, s32 count)
|
||||||
{
|
{
|
||||||
s32 old_count, new_count;
|
s32 old_count, new_count;
|
||||||
|
Loading…
Reference in New Issue
Block a user