AuroraRuntime/Source/Threading/Primitives/CriticalSection.cpp

100 lines
1.8 KiB
C++

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