diff options
| author | Shawn Hoffman <godisgovernment@gmail.com> | 2022-07-18 11:48:20 -0700 |
|---|---|---|
| committer | Shawn Hoffman <godisgovernment@gmail.com> | 2022-08-02 22:24:06 -0700 |
| commit | fec61f89a3bce9bf857a15ed4789629624e66584 (patch) | |
| tree | 68db3cd27ded4cf0416fc280f6a5bfacd5f79c76 /Source/Core/Common/Timer.cpp | |
| parent | 0fe99e9bc8b1dd3c00851749ded34a0fee338321 (diff) | |
Timer: protect usages of ms timers from rollover
Diffstat (limited to 'Source/Core/Common/Timer.cpp')
| -rw-r--r-- | Source/Core/Common/Timer.cpp | 24 |
1 files changed, 5 insertions, 19 deletions
diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 1b80e02880..fb81164390 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -48,8 +48,7 @@ void Timer::Start() void Timer::StartWithOffset(u64 offset) { Start(); - if (m_start_ms > offset) - m_start_ms -= offset; + m_start_ms -= offset; } void Timer::Stop() @@ -60,23 +59,10 @@ void Timer::Stop() u64 Timer::ElapsedMs() const { - // If we have not started yet, return zero - if (m_start_ms == 0) - return 0; - - if (m_running) - { - u64 now = NowMs(); - if (m_start_ms >= now) - return 0; - return now - m_start_ms; - } - else - { - if (m_start_ms >= m_end_ms) - return 0; - return m_end_ms - m_start_ms; - } + const u64 end = m_running ? NowMs() : m_end_ms; + // Can handle up to 1 rollover event (underflow produces correct result) + // If Start() has never been called, will return 0 + return end - m_start_ms; } u64 Timer::GetLocalTimeSinceJan1970() |
