summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Timer.h
blob: 3f5a51ad7f4b7d79765d5abdb1d7f171428f15d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "Common/CommonTypes.h"

namespace Common
{
class Timer
{
public:
  static u64 NowUs();
  static u64 NowMs();

  void Start();
  // Start(), then decrement start time by the offset.
  // Effectively "resumes" a timer
  void StartWithOffset(u64 offset);
  void Stop();
  u64 ElapsedMs() const;

  // The rest of these functions probably belong somewhere else
  static u64 GetLocalTimeSinceJan1970();

  static void IncreaseResolution();
  static void RestoreResolution();

private:
  u64 m_start_ms{0};
  u64 m_end_ms{0};
  bool m_running{false};
};

class PrecisionTimer
{
public:
  PrecisionTimer();
  ~PrecisionTimer();

  PrecisionTimer(const PrecisionTimer&) = delete;
  PrecisionTimer& operator=(const PrecisionTimer&) = delete;

  void SleepUntil(Clock::time_point);

private:
#ifdef _WIN32
  // Using void* to avoid including Windows.h in this header just for HANDLE.
  void* m_timer_handle;
#endif
};

// Similar to std::chrono::steady_clock except this clock
// specifically does *not* count time while the system is suspended.
class SteadyAwakeClock
{
public:
  using rep = s64;
  using period = std::nano;
  using duration = std::chrono::duration<rep, period>;
  using time_point = std::chrono::time_point<SteadyAwakeClock>;

  static constexpr bool is_steady = true;

  static time_point now();
};

}  // Namespace Common