File: Clock.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (86 lines) | stat: -rw-r--r-- 1,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Clock.h"

#if defined(DEATH_TARGET_WINDOWS)
#	include <winsync.h>
#	include <profileapi.h>
#elif defined(DEATH_TARGET_APPLE)
#	include <Availability.h>
#	if __MAC_10_12
#		include <time.h>
#	else
#		include <mach/mach_time.h>
#	endif
#else
#	include <time.h>		// for clock_gettime()
#	include <sys/time.h>	// for gettimeofday()
#endif

namespace nCine
{
	Clock& clock()
	{
		static Clock instance;
		return instance;
	}

	Clock::Clock()
		: _frequency(0UL)
	{
#if defined(DEATH_TARGET_WINDOWS)
		LARGE_INTEGER li;
		if (::QueryPerformanceFrequency(&li)) {
			_frequency = li.LowPart;
			_hasPerfCounter = true;
		} else {
			_frequency = 1000L;
			_hasPerfCounter = false;
		}
#elif defined(DEATH_TARGET_APPLE)
#	if __MAC_10_12
		_frequency = 1.0e9L;
#	else
		mach_timebase_info_data_t info;
		mach_timebase_info(&info);
		_frequency = (info.denom * 1.0e9L) / info.numer;
#	endif
#else
		struct timespec resolution;
		if (clock_getres(CLOCK_MONOTONIC, &resolution) == 0) {
			_frequency = 1.0e9L;
			_hasMonotonicClock = true;
		} else {
			_frequency = 1.0e6L;
			_hasMonotonicClock = false;
		}
#endif
	}

	std::uint64_t Clock::counter() const
	{
#if defined(DEATH_TARGET_WINDOWS)
		if (_hasPerfCounter) {
			LARGE_INTEGER li;
			::QueryPerformanceCounter(&li);
			return li.QuadPart;
		} else {
			return ::GetTickCount64();
		}
#elif defined(DEATH_TARGET_APPLE)
#	if __MAC_10_12
		return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
#	else
		return mach_absolute_time();
#	endif
#else
		if (_hasMonotonicClock) {
			struct timespec now;
			clock_gettime(CLOCK_MONOTONIC, &now);
			return static_cast<std::uint64_t>(now.tv_sec) * _frequency + static_cast<std::uint64_t>(now.tv_nsec);
		} else {
			struct timeval now;
			gettimeofday(&now, nullptr);
			return static_cast<std::uint64_t>(now.tv_sec) * _frequency + static_cast<std::uint64_t>(now.tv_usec);
		}
#endif
	}
}