/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: Semaphore.Win32.cpp Date: 2021-6-12 Author: Reece ***/ #include #include "Semaphore.Generic.hpp" #include "Semaphore.NT.hpp" #if !defined(_AURUNTIME_GENERIC_SEMAPHORE) namespace Aurora::Threading::Primitives { Semaphore::Semaphore(long intialValue) { value_ = CreateSemaphore(nullptr, intialValue, 1024, nullptr); SysAssert(value_ != NULL, "Win32 CreateMutex Error {}", GetLastError()); } Semaphore::~Semaphore() { CloseHandle(value_); } bool Semaphore::HasOSHandle(AuMach &mach) { mach = reinterpret_cast(value_); return true; } bool Semaphore::HasLockImplementation() { return false; } bool Semaphore::TryLock() { auto status = WaitForSingleObject(value_, 0); SysAssert(status != WAIT_FAILED, "Internal Win32 Error {}", GetLastError()); return status != WAIT_TIMEOUT; // WAIT_OBJECT_0, etc } bool Semaphore::Lock(AuUInt64 timeout) { SysAssertExp(!HasLockImplementation()); return WaitFor(this, timeout); } void Semaphore::Lock() { auto status = Lock(0); SysAssert(status, "Couldn't lock semaphore"); } void Semaphore::Unlock(long count) { auto status = ReleaseSemaphore(value_, count, nullptr); SysAssert(status, "Internal Win32 Error {}", GetLastError()); } void Semaphore::Unlock() { return Unlock(1); } AUKN_SYM ISemaphore *SemaphoreNew(int initialCount) { return _new Semaphore(initialCount); } AUKN_SYM void SemaphoreRelease(ISemaphore *waitable) { SafeDelete(waitable); } } #endif