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

71 lines
1.4 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
{
Semaphore::Semaphore(long intialValue)
{
value_ = intialValue;
}
Semaphore::~Semaphore()
{
}
bool Semaphore::HasOSHandle(AuMach &mach)
{
mach = reinterpret_cast<AuMach>(value_);
return true;
}
bool Semaphore::HasLockImplementation()
{
return false;
}
bool Semaphore::TryLock()
{
auto old = value_.load(std::memory_order_relaxed);
return old != 0 && value_.compare_exchange_strong(old, old - 1);
}
bool Semaphore::Lock(AuUInt64 timeout)
{
return WaitFor(this, timeout);
}
void Semaphore::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock semaphore");
}
void Semaphore::Unlock(long count)
{
value_.fetch_add(count);
}
void Semaphore::Unlock()
{
return Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int initialCount)
{
return _new Semaphore(initialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *waitable)
{
AuSafeDelete<Semaphore *>(waitable);
}
}
#endif