From 1ba32585dfa5ca73ab4cf3641ec1185da9f8dca4 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 12 Sep 2023 23:43:00 +1000 Subject: [PATCH] Common: Add Timer::ResetIfNPassed() --- src/common/timer.cpp | 33 +++++++++++++++++++++++++++++++++ src/common/timer.h | 4 ++++ 2 files changed, 37 insertions(+) diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 2f9165bd8..c3aaf6009 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -288,6 +288,39 @@ double Timer::GetTimeNanosecondsAndReset() return ret; } +bool Timer::ResetIfSecondsPassed(double s) +{ + const Value value = GetCurrentValue(); + const double ret = ConvertValueToSeconds(value - m_tvStartValue); + if (ret < s) + return false; + + m_tvStartValue = value; + return true; +} + +bool Timer::ResetIfMillisecondsPassed(double s) +{ + const Value value = GetCurrentValue(); + const double ret = ConvertValueToMilliseconds(value - m_tvStartValue); + if (ret < s) + return false; + + m_tvStartValue = value; + return true; +} + +bool Timer::ResetIfNanosecondsPassed(double s) +{ + const Value value = GetCurrentValue(); + const double ret = ConvertValueToNanoseconds(value - m_tvStartValue); + if (ret < s) + return false; + + m_tvStartValue = value; + return true; +} + void Timer::BusyWait(std::uint64_t ns) { const Value start = GetCurrentValue(); diff --git a/src/common/timer.h b/src/common/timer.h index 7b67d6a75..4206cad9a 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -40,6 +40,10 @@ public: double GetTimeMillisecondsAndReset(); double GetTimeNanosecondsAndReset(); + bool ResetIfSecondsPassed(double s); + bool ResetIfMillisecondsPassed(double s); + bool ResetIfNanosecondsPassed(double s); + private: Value m_tvStartValue; };