AuroraRuntime/Source/IO/Loop/LSLocalEvent.cpp

292 lines
6.6 KiB
C++
Raw Normal View History

2023-10-21 08:04:48 +00:00
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LSLocalEvent.cpp
Date: 2023-10-21
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "LSLocalEvent.hpp"
#include <Source/Threading/Primitives/SMTYield.hpp>
namespace Aurora::IO::Loop
{
LSLocalEvent::LSLocalEvent()
{
}
LSLocalEvent::~LSLocalEvent()
{
}
bool LSLocalEvent::TryInit(bool bTriggered, bool bAtomicRelease, bool bPermitMultipleTriggers)
{
if (!LSSemaphore::TryInit(1))
{
return false;
}
this->bTriggered_ = bTriggered;
this->bAtomicRelease_ = bAtomicRelease;
this->bPermitMultipleTriggers_= bPermitMultipleTriggers;
return true;
}
bool LSLocalEvent::OnTrigger(AuUInt handle)
{
while (true)
{
EventBits bits;
bits.state = AuAtomicLoad(&this->state_);
if (bits.bAtomicRelease)
{
if (bits.bTriggered)
{
auto next = bits;
next.bTriggered = 0;
if (AuAtomicCompareExchange(&this->state_, next.state, bits.state) != bits.state)
{
continue;
}
return true;
}
else
{
(void)LSSemaphore::OnTrigger(0);
AuAtomicAdd(&this->uLaterAlwaysOn, 1u);
2023-10-21 08:04:48 +00:00
return false;
}
}
else
{
if (bits.bTriggered)
{
LSSemaphore::AddOne();
return true;
}
else
{
(void)LSSemaphore::OnTrigger(0);
AuAtomicAdd(&this->uLaterAlwaysOn, 1u);
2023-10-21 08:04:48 +00:00
return false;
}
}
}
}
bool LSLocalEvent::IsSignaled()
{
return this->TryTake();
}
ELoopSource LSLocalEvent::GetType()
{
2023-12-18 07:37:48 +00:00
return ELoopSource::eSourceFastEvent;
2023-10-21 08:04:48 +00:00
}
bool LSLocalEvent::Set()
{
EventBits bits, next;
while (true)
{
bits.state = AuAtomicLoad(&this->state_);
if (!bits.bPermitMultipleTriggers)
{
if (bits.bTriggered)
{
return false;
}
}
next.state = bits.state;
next.bTriggered = 1;
if (AuAtomicCompareExchange(&this->state_, next.state, bits.state) == bits.state)
{
break;
}
}
if (auto count = AuAtomicLoad(&this->uApproxSleepCount))
{
if (bits.bAtomicRelease)
{
LSSemaphore::AddOne();
}
else
{
for (AU_ITERATE_N(n, count))
{
LSSemaphore::AddOne();
}
}
}
return true;
}
void LSLocalEvent::OnPresleep()
{
AuAtomicAdd(&this->uApproxSleepCount, 1u);
EventBits bits;
bits.state = AuAtomicLoad(&this->state_);
if (!bits.bTriggered)
{
return;
}
while (true)
{
auto uState = AuAtomicLoad(&this->uLaterAlwaysOn);
if (!uState)
{
return;
}
if (AuAtomicCompareExchange(&this->uLaterAlwaysOn, uState - 1, uState) == uState)
{
break;
}
}
LSSemaphore::AddOne();
2023-10-21 08:04:48 +00:00
}
void LSLocalEvent::OnFinishSleep()
{
AuAtomicSub(&this->uApproxSleepCount, 1u);
}
bool LSLocalEvent::Reset()
{
while (true)
{
EventBits bits, next;
bits.state = AuAtomicLoad(&this->state_);
if (!bits.bTriggered)
{
return false;
}
next.state = bits.state;
next.bTriggered = 0;
if (AuAtomicCompareExchange(&this->state_, next.state, bits.state) == bits.state)
{
break;
}
}
return true;
}
bool LSLocalEvent::TryTakeNoSpin()
{
EventBits bits;
bits.state = AuAtomicLoad(&this->state_);
if (bits.bAtomicRelease)
{
if (bits.bTriggered)
{
auto next = bits;
next.bTriggered = 0;
return AuAtomicCompareExchange(&this->state_, next.state, bits.state) == bits.state;
}
else
{
return false;
}
}
else
{
return bits.bTriggered;
}
}
bool LSLocalEvent::TryTakeSpin()
{
return Threading::Primitives::DoTryIfAlderLake([&]
2023-10-21 08:04:48 +00:00
{
return this->TryTakeNoSpin();
}, &this->state_);
2023-10-21 08:04:48 +00:00
}
2023-12-01 10:33:55 +00:00
bool LSLocalEvent::IsSignaledNoSpinIfUserland()
2023-10-21 08:04:48 +00:00
{
return this->TryTakeNoSpin();
}
2023-12-01 10:33:55 +00:00
bool LSLocalEvent::TryTake()
{
return this->TryTakeSpin();
}
bool LSLocalEvent::TryTakeWaitNS(AuUInt64 uEndTime)
2023-10-21 08:04:48 +00:00
{
if (this->TryTakeSpin())
{
return true;
}
while (!this->TryTakeNoSpin())
{
if (!uEndTime)
2023-10-21 08:04:48 +00:00
{
if (LSSemaphore::WaitOn(0))
{
return true;
}
}
else
{
auto uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
return false;
}
auto uDeltaMs = AuNSToMS<AuInt64>(uEndTime - uStartTime);
if (uDeltaMs &&
LSSemaphore::WaitOn(uDeltaMs))
{
return true;
}
2023-10-26 16:25:40 +00:00
else if (!uDeltaMs)
{
if (this->TryTakeSpin())
{
return true;
}
}
2023-10-21 08:04:48 +00:00
}
}
return true;
}
AUKN_SYM AuSPtr<ILSEvent> NewLSEvent(bool bTriggered, bool bAtomicRelease, bool bPermitMultipleTriggers)
{
auto pMutex = AuMakeShared<LSLocalEvent>();
if (!pMutex)
{
SysPushErrorGeneric();
return {};
}
if (!pMutex->TryInit(bTriggered, bAtomicRelease, bPermitMultipleTriggers))
{
SysPushErrorNested();
return {};
}
return pMutex;
}
}