2022-11-17 07:46:07 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuConditionVariable.Win32.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuConditionVariable.Generic.hpp"
|
2023-03-15 00:35:29 +00:00
|
|
|
#include <Time/Time.hpp>
|
|
|
|
#include "SMPYield.hpp"
|
2022-11-17 07:46:07 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICCV)
|
|
|
|
|
2023-03-15 00:35:29 +00:00
|
|
|
#if !defined(NTSTATUS_TIMEOUT)
|
|
|
|
#define NTSTATUS_TIMEOUT 0x102
|
|
|
|
#endif
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
ConditionVariableImpl::ConditionVariableImpl(const AuSPtr<IConditionMutex> &pMutex) :
|
2023-03-15 00:35:29 +00:00
|
|
|
mutex_(AuStaticCast<Win32ConditionMutex>(pMutex))
|
2022-11-17 07:46:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IConditionMutex> ConditionVariableImpl::GetMutex()
|
|
|
|
{
|
|
|
|
return mutex_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConditionVariableImpl::WaitForSignal(AuUInt32 uTimeout)
|
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
return WaitForSignalNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConditionVariableImpl::WaitForSignalNS(AuUInt64 qwTimeout)
|
|
|
|
{
|
|
|
|
bool bRet { true };
|
|
|
|
auto pThatMutex = reinterpret_cast<NT4Mutex *>(&this->mutex_->lock_);
|
2022-11-17 07:46:07 +00:00
|
|
|
|
2023-03-15 00:35:29 +00:00
|
|
|
this->mutex_->Unlock();
|
|
|
|
|
|
|
|
if (qwTimeout)
|
2022-11-17 07:46:07 +00:00
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
auto uEndTimeSteady = AuTime::SteadyClockNS() + qwTimeout;
|
|
|
|
auto uEndTimeWall = AuTime::CurrentClockNS() + qwTimeout;
|
|
|
|
auto uTargetTimeNt = AuTime::ConvertTimestampNs(uEndTimeWall);
|
|
|
|
|
|
|
|
LARGE_INTEGER word;
|
|
|
|
word.QuadPart = uTargetTimeNt;
|
|
|
|
|
|
|
|
AuAtomicAdd(&this->wlist, 1u);
|
|
|
|
bRet = pNtWaitForKeyedEvent(gKeyedEventHandle, &this->wlist, 0, &word) != NTSTATUS_TIMEOUT;
|
|
|
|
if (!bRet)
|
|
|
|
{
|
|
|
|
AuAtomicAdd(&this->expander, 1u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuAtomicAdd(&this->wlist, 1u);
|
|
|
|
pNtWaitForKeyedEvent(gKeyedEventHandle, &this->wlist, 0, nullptr);
|
2022-11-17 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:35:29 +00:00
|
|
|
this->mutex_->Lock();
|
|
|
|
return bRet;
|
2022-11-17 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionVariableImpl::Signal()
|
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
auto expected = this->wlist;
|
|
|
|
AuUInt32 uExpander { this->expander };
|
|
|
|
|
|
|
|
if (expected || uExpander)
|
|
|
|
{
|
|
|
|
AuUInt32 uOffset { 0 };
|
|
|
|
|
|
|
|
while (uExpander && AuAtomicCompareExchange(&this->expander, 0u, uExpander) != uExpander)
|
|
|
|
{
|
|
|
|
SMPPause();
|
|
|
|
uExpander = this->expander;
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:48:37 +00:00
|
|
|
if (uExpander)
|
2023-03-15 00:35:29 +00:00
|
|
|
{
|
|
|
|
expected = this->wlist;
|
2023-03-15 00:48:37 +00:00
|
|
|
while (expected && AuAtomicCompareExchange(&this->wlist, expected - uExpander, expected) != expected)
|
|
|
|
{
|
|
|
|
SMPPause();
|
|
|
|
expected = this->wlist;
|
|
|
|
}
|
2023-03-15 00:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = this->wlist;
|
|
|
|
while (expected)
|
|
|
|
{
|
|
|
|
if (AuAtomicCompareExchange(&this->wlist, expected - 1, expected) == expected)
|
|
|
|
{
|
|
|
|
pNtReleaseKeyedEvent(gKeyedEventHandle, &this->wlist, FALSE, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = this->wlist;
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionVariableImpl::Broadcast()
|
|
|
|
{
|
2023-03-15 00:35:29 +00:00
|
|
|
auto expected = this->wlist;
|
|
|
|
AuUInt32 uExpander { this->expander };
|
|
|
|
|
|
|
|
while (expected || uExpander)
|
|
|
|
{
|
|
|
|
AuUInt32 uOffset { 0 };
|
|
|
|
|
|
|
|
if (uExpander && AuAtomicCompareExchange(&this->expander, 0u, uExpander) != uExpander)
|
|
|
|
{
|
|
|
|
SMPPause();
|
|
|
|
uExpander = this->expander;
|
|
|
|
expected = this->wlist;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:48:37 +00:00
|
|
|
if (uExpander)
|
2023-03-15 00:35:29 +00:00
|
|
|
{
|
|
|
|
expected = this->wlist;
|
2023-03-15 00:48:37 +00:00
|
|
|
while (AuAtomicCompareExchange(&this->wlist, expected - uExpander, expected) != expected)
|
|
|
|
{
|
|
|
|
SMPPause();
|
|
|
|
expected = this->wlist;
|
|
|
|
}
|
2023-03-15 00:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = this->wlist;
|
|
|
|
while (expected)
|
|
|
|
{
|
|
|
|
if (AuAtomicCompareExchange(&this->wlist, expected - 1, expected) == expected)
|
|
|
|
{
|
|
|
|
pNtReleaseKeyedEvent(gKeyedEventHandle, &this->wlist, FALSE, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = this->wlist;
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IConditionVariable *ConditionVariableNew(const AuSPtr<IConditionMutex> &pMutex)
|
|
|
|
{
|
|
|
|
return _new ConditionVariableImpl(pMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void ConditionVariableRelease(IConditionVariable *pCV)
|
|
|
|
{
|
|
|
|
AuSafeDelete<ConditionVariableImpl *>(pCV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|