[*] ITimer comment and win32 patch

This commit is contained in:
Reece Wilson 2023-12-09 12:09:45 +00:00
parent 783bed0d85
commit fe529b31da
3 changed files with 22 additions and 14 deletions

View File

@ -52,6 +52,9 @@ namespace Aurora::IO::Loop
struct ITimer : virtual ILoopSource struct ITimer : virtual ILoopSource
{ {
/* Warning: IO timers use wall time (CurrentClock[M/NS), not SteadyClock[M/N]S()).
Use auasync timers for steady-clock timing and tick timing information. */
virtual void UpdateTime(AuUInt64 absTimeMs) = 0; virtual void UpdateTime(AuUInt64 absTimeMs) = 0;
virtual void UpdateTimeNs(AuUInt64 absTimeNs) = 0; virtual void UpdateTimeNs(AuUInt64 absTimeNs) = 0;
virtual void UpdateTickRateIfAny(AuUInt32 reschedStepMsOrZero = 0, AuUInt32 maxIterationsOrZero = 0) = 0; virtual void UpdateTickRateIfAny(AuUInt32 reschedStepMsOrZero = 0, AuUInt32 maxIterationsOrZero = 0) = 0;

View File

@ -29,17 +29,15 @@ namespace Aurora::IO::Loop
void LSTimer::UpdateTime(AuUInt64 absTimeMs) void LSTimer::UpdateTime(AuUInt64 absTimeMs)
{ {
this->targetTime_ = AuTime::ConvertTimestamp(absTimeMs); UpdateTimeInternalNTWall(AuTime::ConvertTimestamp(absTimeMs));
UpdateTimeInternal(this->targetTime_);
} }
void LSTimer::UpdateTimeNs(AuUInt64 absTimeNs) void LSTimer::UpdateTimeNs(AuUInt64 absTimeNs)
{ {
this->targetTime_ = AuTime::ConvertTimestampNs(absTimeNs); UpdateTimeInternalNTWall(AuTime::ConvertTimestampNs(absTimeNs));
UpdateTimeInternal(this->targetTime_);
} }
void LSTimer::UpdateTimeInternal(AuUInt64 absTimeNs) void LSTimer::UpdateTimeInternalNTWall(AuUInt64 absTimeNs)
{ {
LARGE_INTEGER i; LARGE_INTEGER i;
this->targetTime_ = absTimeNs; this->targetTime_ = absTimeNs;
@ -52,7 +50,7 @@ namespace Aurora::IO::Loop
this->reschedStepNsOrZero_ = AuMSToNS<AuUInt64>(reschedStepMsOrZero); this->reschedStepNsOrZero_ = AuMSToNS<AuUInt64>(reschedStepMsOrZero);
this->maxIterationsOrZero_ = maxIterationsOrZero; this->maxIterationsOrZero_ = maxIterationsOrZero;
this->count_ = 0; this->count_ = 0;
UpdateTimeInternal(this->targetTime_); UpdateTimeInternalNTWall(this->targetTime_);
} }
void LSTimer::UpdateTickRateIfAnyNs(AuUInt64 reschedStepNsOrZero, AuUInt32 maxIterationsOrZero) void LSTimer::UpdateTickRateIfAnyNs(AuUInt64 reschedStepNsOrZero, AuUInt32 maxIterationsOrZero)
@ -60,28 +58,35 @@ namespace Aurora::IO::Loop
this->reschedStepNsOrZero_ = reschedStepNsOrZero; this->reschedStepNsOrZero_ = reschedStepNsOrZero;
this->maxIterationsOrZero_ = maxIterationsOrZero; this->maxIterationsOrZero_ = maxIterationsOrZero;
this->count_ = 0; this->count_ = 0;
UpdateTimeInternal(this->targetTime_); UpdateTimeInternalNTWall(this->targetTime_);
} }
bool LSTimer::OnTrigger(AuUInt handle) bool LSTimer::OnTrigger(AuUInt handle)
{ {
SysAssert(this->targetTime_ <= AuTime::ConvertTimestampNs(AuTime::CurrentClockNS()));
if (!this->reschedStepNsOrZero_) if (!this->reschedStepNsOrZero_)
{ {
return true; 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_) if (!this->maxIterationsOrZero_)
{ {
this->UpdateTimeInternal(this->targetTime_ + (AuUInt64(this->reschedStepNsOrZero_) / 100ULL)); this->UpdateTimeInternalNTWall(uNextTick);
return true; return true;
} }
bool ok = AuAtomicAdd<AuUInt32>(&this->count_, 1) <= this->maxIterationsOrZero_; if (AuAtomicAdd<AuUInt32>(&this->count_, 1) <= this->maxIterationsOrZero_)
if (ok)
{ {
this->UpdateTimeInternal(this->targetTime_ + (AuUInt64(this->reschedStepNsOrZero_) / 100ULL)); this->UpdateTimeInternalNTWall(uNextTick);
return true; return true;
} }
else else

View File

@ -27,7 +27,7 @@ namespace Aurora::IO::Loop
virtual bool WaitOn(AuUInt32 timeout) override; virtual bool WaitOn(AuUInt32 timeout) override;
virtual ELoopSource GetType() override; virtual ELoopSource GetType() override;
void UpdateTimeInternal(AuUInt64 absTimeNs); void UpdateTimeInternalNTWall(AuUInt64 absTimeNs);
private: private:
AuUInt64 reschedStepNsOrZero_ {0}; AuUInt64 reschedStepNsOrZero_ {0};