[+] IAsyncTimer::SetCatchUp(bool bCatchUp)

This commit is contained in:
Reece Wilson 2024-05-19 17:01:51 +01:00
parent 67894b399b
commit 9b26eea886
3 changed files with 38 additions and 1 deletions

View File

@ -13,6 +13,24 @@ namespace Aurora::Async
{ {
virtual void CancelTimer() = 0; virtual void CancelTimer() = 0;
virtual AuUInt64 GetLastTime() = 0; virtual AuUInt64 GetLastTime() = 0;
/**
* Get tick count (always non-zero after or during initial trigger)
*/
virtual AuUInt64 GetTicks() = 0; virtual AuUInt64 GetTicks() = 0;
/**
* Default: false
* When in catch up mode, timer ticks can be scheduled in the past, otherwise the next time is clamped to the current time.
* Latterly, it is possible to get stuck in a spin with no interval, however if the system lags behind momentarily, recovery is instantaneous.
* On platforms with process suspension, specify no catch up.
* On servers or other tick-sensitive applications (like a clock GUI), specify catch up.
*/
virtual bool IsCatchUp() = 0;
/**
* See: IsCatchUp
*/
virtual void SetCatchUp(bool bCatchUp) = 0;
}; };
} }

View File

@ -40,15 +40,31 @@ namespace Aurora::Async
return this->uTickCount; return this->uTickCount;
} }
bool AsyncFuncTimer::IsCatchUp()
{
return this->bCatchUp;
}
void AsyncFuncTimer::SetCatchUp(bool bCatchUp)
{
this->bCatchUp = bCatchUp;
}
void AsyncFuncTimer::DispatchTask(IWorkItemHandler::ProcessInfo &info) void AsyncFuncTimer::DispatchTask(IWorkItemHandler::ProcessInfo &info)
{ {
info.type = ETickType::eRerun; info.type = ETickType::eRerun;
auto uTickCount = ++this->uTickCount; auto uTickCount = ++this->uTickCount;
this->uLastTickTime = AuTime::SteadyClockNS(); this->uLastTickTime = AuTime::SteadyClockNS();
auto uDelta = this->uLastTickTime - this->uNextTickTime; auto uDelta = this->uLastTickTime - this->uNextTickTime;
this->uNextTickTime += this->uInterval; this->uNextTickTime += this->uInterval;
if (!this->bCatchUp)
{
this->uNextTickTime = AuMax(this->uNextTickTime, this->uLastTickTime); this->uNextTickTime = AuMax(this->uNextTickTime, this->uLastTickTime);
}
info.reschedSteadyClockAbsNs = this->uNextTickTime; info.reschedSteadyClockAbsNs = this->uNextTickTime;
if (this->pCallback->OnTick(uTickCount, uDelta, this->uLastTickTime)) if (this->pCallback->OnTick(uTickCount, uDelta, this->uLastTickTime))

View File

@ -24,6 +24,8 @@ namespace Aurora::Async
void CancelTimer() override; void CancelTimer() override;
AuUInt64 GetLastTime() override; AuUInt64 GetLastTime() override;
AuUInt64 GetTicks() override; AuUInt64 GetTicks() override;
bool IsCatchUp() override;
void SetCatchUp(bool bCatchUp) override;
void DispatchTask(IWorkItemHandler::ProcessInfo &info) override; void DispatchTask(IWorkItemHandler::ProcessInfo &info) override;
void Cleanup() override; void Cleanup() override;
@ -34,5 +36,6 @@ namespace Aurora::Async
AuUInt64 uLastTickTime {}; AuUInt64 uLastTickTime {};
AuUInt64 uInterval {}; AuUInt64 uInterval {};
AuUInt64 uTickCount {}; AuUInt64 uTickCount {};
bool bCatchUp { false };
}; };
} }