Timer.cc 865 B

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