2021-06-27 21:25:29 +00:00
|
|
|
/***
|
2024-01-29 14:09:59 +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
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
CriticalSectionImpl::CriticalSectionImpl()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
this->owner_ = {};
|
2022-11-17 07:46:07 +00:00
|
|
|
this->count_ = 0;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
CriticalSectionImpl::~CriticalSectionImpl()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool CriticalSectionImpl::HasOSHandle(AuMach &mach)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool CriticalSectionImpl::TryLock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-03-15 00:35: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;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void CriticalSectionImpl::Lock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
auto status = LockMS(0);
|
2021-06-27 21:25:29 +00:00
|
|
|
SysAssert(status, "Spurious critical section wakeup");
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool CriticalSectionImpl::LockMS(AuUInt64 timeout)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-03-15 00:35: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;
|
|
|
|
}
|
|
|
|
|
2023-04-03 07:21:44 +00:00
|
|
|
if (!this->mutex_.LockMS(timeout))
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->owner_ = cur;
|
|
|
|
this->count_ = 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool CriticalSectionImpl::LockNS(AuUInt64 timeout)
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
auto cur = GetThreadCookie();
|
2023-03-12 15:27:28 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
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
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
this->owner_ = {};
|
2022-11-17 07:46:07 +00:00
|
|
|
this->mutex_.Unlock();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool CriticalSectionImpl::HasLockImplementation()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IWaitable *CriticalSectionNew()
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
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
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
AuSafeDelete<CriticalSectionImpl *>(pCriticalSection);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2023-03-21 03:18:09 +00:00
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, CriticalSection, CriticalSectionImpl)
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|