timer.h (1410B)
1 // SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 #include <cstdint> 6 7 namespace Common { 8 9 class Timer 10 { 11 public: 12 using Value = std::uint64_t; 13 14 Timer(); 15 16 static double GetFrequency(); 17 static Value GetCurrentValue(); 18 19 static double ConvertValueToSeconds(Value value); 20 static double ConvertValueToMilliseconds(Value value); 21 static double ConvertValueToNanoseconds(Value value); 22 static Value ConvertSecondsToValue(double s); 23 static Value ConvertMillisecondsToValue(double s); 24 static Value ConvertNanosecondsToValue(double ns); 25 static void BusyWait(std::uint64_t ns); 26 static void HybridSleep(std::uint64_t ns, std::uint64_t min_sleep_time = UINT64_C(2000000)); 27 static void NanoSleep(std::uint64_t ns); 28 static void SleepUntil(Value value, bool exact); 29 30 void Reset(); 31 void ResetTo(Value value) { m_tvStartValue = value; } 32 33 Value GetStartValue() const { return m_tvStartValue; } 34 35 double GetTimeSeconds() const; 36 double GetTimeMilliseconds() const; 37 double GetTimeNanoseconds() const; 38 39 double GetTimeSecondsAndReset(); 40 double GetTimeMillisecondsAndReset(); 41 double GetTimeNanosecondsAndReset(); 42 43 bool ResetIfSecondsPassed(double s); 44 bool ResetIfMillisecondsPassed(double s); 45 bool ResetIfNanosecondsPassed(double s); 46 47 private: 48 Value m_tvStartValue; 49 }; 50 51 } // namespace Common