diff --git a/libctru/include/3ds/synchronization.h b/libctru/include/3ds/synchronization.h old mode 100644 new mode 100755 index bbd18f4..85c2e5c --- a/libctru/include/3ds/synchronization.h +++ b/libctru/include/3ds/synchronization.h @@ -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. diff --git a/libctru/source/synchronization.c b/libctru/source/synchronization.c index 9750bfc..65a387f 100644 --- a/libctru/source/synchronization.c +++ b/libctru/source/synchronization.c @@ -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;