AuroraRuntime/Source/Threading/Primitives/AuSemaphore.Generic.cpp

71 lines
1.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSemaphore.Generic.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuSemaphore.Generic.hpp"
#if defined(_AURUNTIME_GENERIC_SEMAPHORE)
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(long intialValue)
{
value_ = intialValue;
}
SemaphoreImpl::~SemaphoreImpl()
{
}
bool SemaphoreImpl::HasOSHandle(AuMach &mach)
{
mach = reinterpret_cast<AuMach>(value_);
return true;
}
bool SemaphoreImpl::HasLockImplementation()
{
return false;
}
bool SemaphoreImpl::TryLock()
{
auto old = value_.load(std::memory_order_relaxed);
return old != 0 && value_.compare_exchange_strong(old, old - 1);
}
bool SemaphoreImpl::Lock(AuUInt64 timeout)
{
return WaitFor(this, timeout);
}
void SemaphoreImpl::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(AuUInt16 count)
{
value_.fetch_add(count);
}
void SemaphoreImpl::Unlock()
{
return Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int initialCount)
{
return _new Semaphore(initialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *waitable)
{
AuSafeDelete<Semaphore *>(waitable);
}
}
#endif