72 lines
1.5 KiB
C++
72 lines
1.5 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: Semaphore.Generic.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "Semaphore.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)
|
|
{
|
|
SysAssertExp(!HasLockImplementation());
|
|
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)
|
|
{
|
|
SafeDelete<Semaphore *>(waitable);
|
|
}
|
|
}
|
|
#endif |