AuroraRuntime/Source/Threading/Primitives/AuCriticalSection.cpp

165 lines
3.1 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2021-06-27 21:25:29 +00:00
2022-11-17 07:46:07 +00:00
File: AuCriticalSection.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuCriticalSection.hpp"
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
CriticalSectionImpl::CriticalSectionImpl()
2021-06-27 21:25:29 +00:00
{
this->owner_ = {};
2022-11-17 07:46:07 +00:00
this->count_ = 0;
2021-06-27 21:25:29 +00:00
}
CriticalSectionImpl::~CriticalSectionImpl()
2021-06-27 21:25:29 +00:00
{
}
bool CriticalSectionImpl::HasOSHandle(AuMach &mach)
2021-06-27 21:25:29 +00:00
{
return false;
}
bool CriticalSectionImpl::TryLock()
2021-06-27 21:25:29 +00:00
{
auto cur = GetThreadCookie();
2021-06-27 21:25:29 +00:00
2022-11-17 07:46:07 +00:00
if (this->owner_ == cur)
2021-06-27 21:25:29 +00:00
{
2022-11-17 07:46:07 +00:00
this->count_++;
2021-06-27 21:25:29 +00:00
return true;
}
2022-11-17 07:46:07 +00:00
if (!this->mutex_.TryLock())
2021-06-27 21:25:29 +00:00
{
return false;
}
2022-11-17 07:46:07 +00:00
this->owner_ = cur;
this->count_ = 1;
2021-06-27 21:25:29 +00:00
return true;
}
void CriticalSectionImpl::Lock()
2021-06-27 21:25:29 +00:00
{
auto status = LockMS(0);
2021-06-27 21:25:29 +00:00
SysAssert(status, "Spurious critical section wakeup");
}
bool CriticalSectionImpl::LockMS(AuUInt64 timeout)
2021-06-27 21:25:29 +00:00
{
auto cur = GetThreadCookie();
2021-06-27 21:25:29 +00:00
2022-11-17 07:46:07 +00:00
if (this->owner_ == cur)
2021-06-27 21:25:29 +00:00
{
2022-11-17 07:46:07 +00:00
this->count_++;
2021-06-27 21:25:29 +00:00
return true;
}
if (!this->mutex_.LockMS(timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
bool CriticalSectionImpl::LockNS(AuUInt64 timeout)
{
auto cur = GetThreadCookie();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.LockNS(timeout))
2021-06-27 21:25:29 +00:00
{
return false;
}
2022-11-17 07:46:07 +00:00
this->owner_ = cur;
this->count_ = 1;
2021-06-27 21:25:29 +00:00
return true;
}
2023-08-21 18:17:05 +00:00
bool CriticalSectionImpl::LockAbsNS(AuUInt64 timeout)
{
auto cur = GetThreadCookie();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.LockAbsNS(timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
bool CriticalSectionImpl::LockAbsMS(AuUInt64 timeout)
{
auto cur = GetThreadCookie();
if (this->owner_ == cur)
{
this->count_++;
return true;
}
if (!this->mutex_.LockAbsMS(timeout))
{
return false;
}
this->owner_ = cur;
this->count_ = 1;
return true;
}
void CriticalSectionImpl::Unlock()
2021-06-27 21:25:29 +00:00
{
2022-11-17 07:46:07 +00:00
if (--this->count_ == 0)
2021-06-27 21:25:29 +00:00
{
this->owner_ = {};
2022-11-17 07:46:07 +00:00
this->mutex_.Unlock();
2021-06-27 21:25:29 +00:00
}
}
bool CriticalSectionImpl::HasLockImplementation()
2021-06-27 21:25:29 +00:00
{
return true;
}
AUKN_SYM IWaitable *CriticalSectionNew()
{
return _new CriticalSectionImpl();
2021-06-27 21:25:29 +00:00
}
2022-11-17 07:46:07 +00:00
AUKN_SYM void CriticalSectionRelease(IWaitable *pCriticalSection)
2021-06-27 21:25:29 +00:00
{
AuSafeDelete<CriticalSectionImpl *>(pCriticalSection);
2021-06-27 21:25:29 +00:00
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, CriticalSection, CriticalSectionImpl)
2021-06-27 21:25:29 +00:00
}