AuroraRuntime/Source/Threading/Primitives/AuCriticalSection.cpp

100 lines
1.9 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuCriticalSection.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuCriticalSection.hpp"
namespace Aurora::Threading::Primitives
{
CriticalSection::CriticalSection()
{
this->owner_ = nullptr;
this->count_ = 0;
}
CriticalSection::~CriticalSection()
{
}
bool CriticalSection::HasOSHandle(AuMach &mach)
{
return false;
}
bool CriticalSection::TryLock()
{
auto cur = Threads::GetThread();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.TryLock())
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
void CriticalSection::Lock()
{
auto status = Lock(0);
SysAssert(status, "Spurious critical section wakeup");
}
bool CriticalSection::Lock(AuUInt64 timeout)
{
auto cur = Threads::GetThread();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!Aurora::Threading::WaitFor(&this->mutex_, timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
void CriticalSection::Unlock()
{
if (--this->count_ == 0)
{
this->owner_ = nullptr;
this->mutex_.Unlock();
}
}
bool CriticalSection::HasLockImplementation()
{
return true;
}
AUKN_SYM IWaitable *CriticalSectionNew()
{
return _new CriticalSection();
}
AUKN_SYM void CriticalSectionRelease(IWaitable *pCriticalSection)
{
AuSafeDelete<CriticalSection *>(pCriticalSection);
}
}