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  uint64_t now;
11 
12  using namespace std::chrono;
13  now = duration_cast<microseconds>(
14  steady_clock::now().time_since_epoch()).count();
15 
16  // Other parts of openMSX may crash if this function ever returns a
17  // value that is less than a previously returned value. Hence this
18  // extra check.
19  // Steady_clock _should_ be monotonic. It's implemented in terms of
20  // clock_gettime(CLOCK_MONOTONIC). Unfortunately in older linux
21  // versions we've seen buggy implementation that once in a while did
22  // return time points slightly in the past.
23  if (now < lastTime) return lastTime;
24  lastTime = now;
25  return now;
26 }
27 
28 void sleep(uint64_t us)
29 {
30  std::this_thread::sleep_for(std::chrono::microseconds(us));
31 }
32 
33 } // namespace openmsx::Timer
ALWAYS_INLINE unsigned count(const uint8_t *pIn, const uint8_t *pMatch, const uint8_t *pInLimit)
Definition: lz4.cc:207
void sleep(uint64_t us)
Sleep for the specified amount of time (in us).
Definition: Timer.cc:28
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:7