| 1 | #include "basis/seadRawPrint.h" |
| 2 | #include "thread/seadSemaphore.h" |
| 3 | |
| 4 | namespace sead |
| 5 | { |
| 6 | Semaphore::Semaphore() = default; |
| 7 | |
| 8 | Semaphore::Semaphore(s32 initial_count) : Semaphore() |
| 9 | { |
| 10 | initialize(initial_count); |
| 11 | } |
| 12 | |
| 13 | Semaphore::Semaphore(s32 initial_count, s32 max_count) : Semaphore() |
| 14 | { |
| 15 | initialize(initial_count, max_count); |
| 16 | } |
| 17 | |
| 18 | Semaphore::Semaphore(Heap* heap) : Semaphore(heap, HeapNullOption::UseSpecifiedOrContainHeap) {} |
| 19 | |
| 20 | Semaphore::Semaphore(Heap* heap, s32 initial_count) : Semaphore(heap) |
| 21 | { |
| 22 | initialize(initial_count); |
| 23 | } |
| 24 | |
| 25 | Semaphore::Semaphore(Heap* heap, s32 initial_count, s32 max_count) : Semaphore(heap) |
| 26 | { |
| 27 | initialize(initial_count, max_count); |
| 28 | } |
| 29 | |
| 30 | Semaphore::Semaphore(Heap* heap, IDisposer::HeapNullOption heap_null_option) |
| 31 | : IDisposer(heap, heap_null_option) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | Semaphore::Semaphore(Heap* heap, IDisposer::HeapNullOption heap_null_option, s32 initial_count) |
| 36 | : Semaphore(heap, heap_null_option) |
| 37 | { |
| 38 | initialize(initial_count); |
| 39 | } |
| 40 | |
| 41 | Semaphore::Semaphore(Heap* heap, IDisposer::HeapNullOption heap_null_option, s32 initial_count, |
| 42 | s32 max_count) |
| 43 | : Semaphore(heap, heap_null_option) |
| 44 | { |
| 45 | initialize(initial_count, max_count); |
| 46 | } |
| 47 | |
| 48 | Semaphore::~Semaphore() |
| 49 | { |
| 50 | nn::os::FinalizeSemaphore(semaphore: &mSemaphoreInner); |
| 51 | setInitialized(false); |
| 52 | } |
| 53 | |
| 54 | void Semaphore::initialize(s32 initial_count, s32 max_count) |
| 55 | { |
| 56 | #ifdef SEAD_DEBUG |
| 57 | SEAD_ASSERT_MSG(!mInitialized, "Semaphore is already initialized." ); |
| 58 | #endif |
| 59 | nn::os::InitializeSemaphore(semaphore: &mSemaphoreInner, initial_count, max_count); |
| 60 | setInitialized(true); |
| 61 | } |
| 62 | |
| 63 | void Semaphore::lock() |
| 64 | { |
| 65 | #ifdef SEAD_DEBUG |
| 66 | SEAD_ASSERT_MSG(mInitialized, "Semaphore is not initialized." ); |
| 67 | #endif |
| 68 | nn::os::AcquireSemaphore(semaphore: &mSemaphoreInner); |
| 69 | } |
| 70 | |
| 71 | bool Semaphore::tryLock() |
| 72 | { |
| 73 | #ifdef SEAD_DEBUG |
| 74 | SEAD_ASSERT_MSG(mInitialized, "Semaphore is not initialized." ); |
| 75 | #endif |
| 76 | return nn::os::TryAcquireSemaphore(semaphore: &mSemaphoreInner); |
| 77 | } |
| 78 | |
| 79 | void Semaphore::unlock() |
| 80 | { |
| 81 | #ifdef SEAD_DEBUG |
| 82 | SEAD_ASSERT_MSG(mInitialized, "Semaphore is not initialized." ); |
| 83 | #endif |
| 84 | nn::os::ReleaseSemaphore(semaphore: &mSemaphoreInner); |
| 85 | } |
| 86 | } // namespace sead |
| 87 | |