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