150 lines
4.4 KiB
C++
150 lines
4.4 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: LSTimer.NT.cpp
|
|
Date: 2021-10-1
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "LSTimer.hpp"
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
namespace Aurora::IO::Loop
|
|
{
|
|
LSTimer::LSTimer(AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero, bool bSingleshot, HANDLE handle) :
|
|
LSHandle(AuReinterpretCast<AuUInt>(handle)),
|
|
reschedStepNsOrZero_(AuMSToNS<AuUInt64>(reschedStepMsOrZero)),
|
|
bSingleshot(bSingleshot),
|
|
maxIterationsOrZero_(maxIterationsOrZero)
|
|
{
|
|
this->targetTime_ = AuTime::ConvertTimestamp(AuTime::CurrentClockMS());
|
|
}
|
|
|
|
LSTimer::~LSTimer()
|
|
{
|
|
auto handle = AuReinterpretCast<HANDLE>(this->handle);
|
|
AuWin32CloseHandle(handle);
|
|
this->handle = kInvalidHandle;
|
|
}
|
|
|
|
void LSTimer::UpdateTime(AuUInt64 absTimeMs)
|
|
{
|
|
UpdateTimeInternalNTWall(AuTime::ConvertTimestamp(absTimeMs));
|
|
}
|
|
|
|
void LSTimer::UpdateTimeNs(AuUInt64 absTimeNs)
|
|
{
|
|
UpdateTimeInternalNTWall(AuTime::ConvertTimestampNs(absTimeNs));
|
|
}
|
|
|
|
void LSTimer::UpdateTimeInternalNTWall(AuUInt64 absTimeNs)
|
|
{
|
|
LARGE_INTEGER i;
|
|
this->targetTime_ = absTimeNs;
|
|
i.QuadPart = absTimeNs;
|
|
SysAssert(::SetWaitableTimer(AuReinterpretCast<HANDLE>(this->handle), &i, 0, nullptr, nullptr, false));
|
|
}
|
|
|
|
void LSTimer::UpdateTickRateIfAny(AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero)
|
|
{
|
|
this->reschedStepNsOrZero_ = AuMSToNS<AuUInt64>(reschedStepMsOrZero);
|
|
this->maxIterationsOrZero_ = maxIterationsOrZero;
|
|
this->count_ = 0;
|
|
UpdateTimeInternalNTWall(this->targetTime_);
|
|
}
|
|
|
|
void LSTimer::UpdateTickRateIfAnyNs(AuUInt64 reschedStepNsOrZero, AuUInt32 maxIterationsOrZero)
|
|
{
|
|
this->reschedStepNsOrZero_ = reschedStepNsOrZero;
|
|
this->maxIterationsOrZero_ = maxIterationsOrZero;
|
|
this->count_ = 0;
|
|
UpdateTimeInternalNTWall(this->targetTime_);
|
|
}
|
|
|
|
bool LSTimer::OnTrigger(AuUInt handle)
|
|
{
|
|
if (!this->reschedStepNsOrZero_)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
auto uCurrentTime = AuTime::ConvertTimestampNs(AuTime::CurrentClockNS());
|
|
auto uNextTick = this->targetTime_ + (AuUInt64(this->reschedStepNsOrZero_) / 100ULL);
|
|
|
|
if (this->targetTime_ > uCurrentTime ||
|
|
uCurrentTime > uNextTick)
|
|
{
|
|
this->targetTime_ = uCurrentTime;
|
|
uNextTick = this->targetTime_ + (AuUInt64(this->reschedStepNsOrZero_) / 100ULL);
|
|
}
|
|
|
|
if (!this->maxIterationsOrZero_)
|
|
{
|
|
this->UpdateTimeInternalNTWall(uNextTick);
|
|
return true;
|
|
}
|
|
|
|
if (AuAtomicAdd<AuUInt32>(&this->count_, 1) <= this->maxIterationsOrZero_)
|
|
{
|
|
this->UpdateTimeInternalNTWall(uNextTick);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Stop();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void LSTimer::Stop()
|
|
{
|
|
bool bSuccess = ::CancelWaitableTimer(AuReinterpretCast<HANDLE>(handle));
|
|
SysAssertDbg(bSuccess);
|
|
LARGE_INTEGER i;
|
|
i.QuadPart = AuNumericLimits<AuInt64>::max();
|
|
SysAssert(::SetWaitableTimer(AuReinterpretCast<HANDLE>(this->handle), &i, 0, nullptr, nullptr, false));
|
|
}
|
|
|
|
bool LSTimer::IsSignaled()
|
|
{
|
|
return LSHandle::IsSignaled();
|
|
}
|
|
|
|
bool LSTimer::WaitOn(AuUInt32 timeout)
|
|
{
|
|
return LSHandle::WaitOn(timeout);
|
|
}
|
|
|
|
ELoopSource LSTimer::GetType()
|
|
{
|
|
return ELoopSource::eSourceTimer;
|
|
}
|
|
|
|
AUKN_SYM AuSPtr<ITimer> NewLSTimer(AuUInt64 absStartTimeMs, AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero, bool bSingleshot)
|
|
{
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/threadpoolapiset/nf-threadpoolapiset-setthreadpoolwait
|
|
// TODO: Is that any better with an event?
|
|
|
|
auto handle = CreateWaitableTimerW(nullptr, !bSingleshot, nullptr);
|
|
if (!handle)
|
|
{
|
|
SysPushErrorIO();
|
|
return {};
|
|
}
|
|
|
|
auto object = AuMakeShared<LSTimer>(reschedStepMsOrZero, maxIterationsOrZero, bSingleshot, handle);
|
|
if (!object)
|
|
{
|
|
SysPushErrorMem();
|
|
AuWin32CloseHandle(handle);
|
|
return {};
|
|
}
|
|
|
|
if (absStartTimeMs)
|
|
{
|
|
object->UpdateTime(absStartTimeMs);
|
|
}
|
|
|
|
return object;
|
|
}
|
|
} |