openMSX
Timer.cc
Go to the documentation of this file.
1 #include "Timer.hh"
2 #include <chrono>
3 #include <thread>
4 
5 namespace openmsx::Timer {
6 
7 uint64_t getTime()
8 {
9  static uint64_t lastTime = 0;
10 
11  using namespace std::chrono;
12  uint64_t now = duration_cast<microseconds>(
13  steady_clock::now().time_since_epoch()).count();
14 
15  // Other parts of openMSX may crash if this function ever returns a
16  // value that is less than a previously returned value. Hence this
17  // extra check.
18  // Steady_clock _should_ be monotonic. It's implemented in terms of
19  // clock_gettime(CLOCK_MONOTONIC). Unfortunately in older linux
20  // versions we've seen buggy implementation that once in a while did
21  // return time points slightly in the past.
22  if (now < lastTime) return lastTime;
23  lastTime = now;
24  return now;
25 }
26 
27 void sleep(uint64_t us)
28 {
29  std::this_thread::sleep_for(std::chrono::microseconds(us));
30 }
31 
32 } // namespace openmsx::Timer
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:7
void sleep(uint64_t us)
Sleep for the specified amount of time (in us).
Definition: Timer.cc:27