OXYGENHUB
◆ main openmsx / src / thread / Timer.cc
860 B · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "Timer.hh"
2
3
#include <chrono>
4
#include <thread>
5
6
namespace openmsx::Timer {
7
8
uint64_t getTime()
9
{
10
	static uint64_t lastTime = 0;
11
12
	using namespace std::chrono;
13
	uint64_t 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