wall_clock.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <chrono>
  5. #include <memory>
  6. #include "common/common_types.h"
  7. namespace Common {
  8. class WallClock {
  9. public:
  10. static constexpr u64 NS_RATIO = 1'000'000'000;
  11. static constexpr u64 US_RATIO = 1'000'000;
  12. static constexpr u64 MS_RATIO = 1'000;
  13. virtual ~WallClock() = default;
  14. /// Returns current wall time in nanoseconds
  15. [[nodiscard]] virtual std::chrono::nanoseconds GetTimeNS() = 0;
  16. /// Returns current wall time in microseconds
  17. [[nodiscard]] virtual std::chrono::microseconds GetTimeUS() = 0;
  18. /// Returns current wall time in milliseconds
  19. [[nodiscard]] virtual std::chrono::milliseconds GetTimeMS() = 0;
  20. /// Returns current wall time in emulated clock cycles
  21. [[nodiscard]] virtual u64 GetClockCycles() = 0;
  22. /// Returns current wall time in emulated cpu cycles
  23. [[nodiscard]] virtual u64 GetCPUCycles() = 0;
  24. virtual void Pause(bool is_paused) = 0;
  25. /// Tells if the wall clock, uses the host CPU's hardware clock
  26. [[nodiscard]] bool IsNative() const {
  27. return is_native;
  28. }
  29. protected:
  30. explicit WallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, bool is_native_)
  31. : emulated_cpu_frequency{emulated_cpu_frequency_},
  32. emulated_clock_frequency{emulated_clock_frequency_}, is_native{is_native_} {}
  33. u64 emulated_cpu_frequency;
  34. u64 emulated_clock_frequency;
  35. private:
  36. bool is_native;
  37. };
  38. [[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
  39. u64 emulated_clock_frequency);
  40. } // namespace Common