AuroraRuntime/Source/Threading/Primitives/AuCriticalSection.cpp
Reece 046b70d7bc [*] [Pre-Win8.1] Optimize for modern nt instead of windows vista synch in legacy path; yes, this is how windows 7 and vista synch is somewhat implemented.
...on apis that predate those kernel revisions. so, technically this might be able to run on xp.
[*] GetThreadCookie optimization for all platforms
2023-03-15 00:35:29 +00:00

121 lines
2.2 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_ = {};
this->count_ = 0;
}
CriticalSection::~CriticalSection()
{
}
bool CriticalSection::HasOSHandle(AuMach &mach)
{
return false;
}
bool CriticalSection::TryLock()
{
auto cur = GetThreadCookie();
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 = GetThreadCookie();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.Lock(timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
bool CriticalSection::LockNS(AuUInt64 timeout)
{
auto cur = GetThreadCookie();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.LockNS(timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
void CriticalSection::Unlock()
{
if (--this->count_ == 0)
{
this->owner_ = {};
this->mutex_.Unlock();
}
}
bool CriticalSection::HasLockImplementation()
{
return true;
}
AUKN_SYM IWaitable *CriticalSectionNew()
{
return _new CriticalSection();
}
AUKN_SYM void CriticalSectionRelease(IWaitable *pCriticalSection)
{
AuSafeDelete<CriticalSection *>(pCriticalSection);
}
}