Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Timer.cpp « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1311edf7b1d09acfe230c059ae58c350b025ba6b (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2005-2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include <boost/version.hpp>

// Not all Boost versions can use a header-only
// boost_system without build failures.
//
// See mumble-voip/mumble#2366.
//
// This was fixed in Boost 1.56.0.
//
// See:
//  https://github.com/boostorg/system/blob/boost-1.56.0/include/boost/system/error_code.hpp#L514-L516
// vs.
//  https://github.com/boostorg/system/blob/boost-1.55.0/include/boost/system/error_code.hpp#L515-L517
#if BOOST_VERSION >= 105600 && !defined(__MINGW32__)
#	define USE_BOOST_CHRONO
#endif

#include "Timer.h"

Timer::Timer(bool start) {
	uiStart = start ? now() : 0;
}

quint64 Timer::elapsed() const {
	Q_ASSERT(uiStart != 0);
	return now() - uiStart;
}

bool Timer::isElapsed(quint64 us) {
	Q_ASSERT(uiStart != 0);
	if (elapsed() > us) {
		uiStart += us;
		return true;
	}
	return false;
}

quint64 Timer::restart() {
	quint64 n = now();
	quint64 e = n - uiStart;
	uiStart   = n;
	return e;
}

bool Timer::isStarted() const {
	return uiStart != 0;
}

bool Timer::operator<(const Timer &other) const {
	return uiStart > other.uiStart;
}

bool Timer::operator>(const Timer &other) const {
	return uiStart < other.uiStart;
}

#ifdef USE_BOOST_CHRONO
// Ensure boost_system is header only.
#	define BOOST_ERROR_CODE_HEADER_ONLY
// Ensure boost_chrono is header only.
#	define BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
#	define BOOST_CHRONO_HEADER_ONLY

#	include <boost/chrono.hpp>

quint64 Timer::now() {
	using namespace boost::chrono;
	time_point< steady_clock > now                     = steady_clock::now();
	time_point< steady_clock >::duration epochDuration = now.time_since_epoch();
	microseconds epochDurationUsec                     = duration_cast< microseconds >(epochDuration);
	return static_cast< quint64 >(epochDurationUsec.count());
}
#elif defined(Q_OS_WIN)
#	include "win.h"

quint64 Timer::now() {
	static double scale = 0;

	if (scale == 0) {
		LARGE_INTEGER freq;
		QueryPerformanceFrequency(&freq);
		scale = 1000000. / freq.QuadPart;
	}

	LARGE_INTEGER li;
	QueryPerformanceCounter(&li);
	quint64 e = li.QuadPart;

	return static_cast< quint64 >(e * scale);
}
#elif defined(Q_OS_UNIX)
#	include <errno.h>
#	include <string.h>
#	include <sys/time.h>
#	include <unistd.h>
#	if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
quint64 Timer::now() {
	struct timespec ts;
	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
		qFatal("Timer: clock_gettime() failed: (%i) %s", errno, strerror(errno));
	}
	quint64 e = ts.tv_sec * 1000000LL;
	e += ts.tv_nsec / 1000LL;
	return e;
}
#	else
quint64 Timer::now() {
	struct timeval tv;
	gettimeofday(&tv, nullptr);
	quint64 e = tv.tv_sec * 1000000LL;
	e += tv.tv_usec;
	return e;
}
#	endif
#else
quint64 Timer::now() {
	static QTime ticker;
	quint64 elapsed = ticker.elapsed();
	return elapsed * 1000LL;
}
#endif