aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/Clock.cpp
blob: e52cd5db4534ea0273e806e246686bd6c6231011 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// Clock.cpp
//
// Library: Foundation
// Package: DateTime
// Module:  Clock
//
// Copyright (c) 2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Clock.h"
#include "Poco/Exception.h"
#include "Poco/Timestamp.h"
#if defined(__MACH__)
#include <mach/mach.h>
#include <mach/clock.h>
#elif defined(POCO_OS_FAMILY_UNIX)
#include <time.h>
#include <unistd.h>
#elif defined(POCO_VXWORKS)
#include <timers.h>
#elif defined(POCO_OS_FAMILY_WINDOWS)
#include "Poco/UnWindows.h"
#endif
#include <algorithm>
#undef min
#undef max
#include <limits>


#ifndef POCO_HAVE_CLOCK_GETTIME
	#if (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS) || defined(__QNX__)
		#ifndef __APPLE__ // See GitHub issue #1453 - not available before Mac OS 10.12/iOS 10
			#define POCO_HAVE_CLOCK_GETTIME
		#endif
	#endif
#endif


namespace Poco {


const Clock::ClockVal Clock::CLOCKVAL_MIN = std::numeric_limits<Clock::ClockVal>::min();
const Clock::ClockVal Clock::CLOCKVAL_MAX = std::numeric_limits<Clock::ClockVal>::max();


Clock::Clock()
{
	update();
}


Clock::Clock(ClockVal tv)
{
	_clock = tv;
}


Clock::Clock(const Clock& other)
{
	_clock = other._clock;
}


Clock::~Clock()
{
}


Clock& Clock::operator = (const Clock& other)
{
	_clock = other._clock;
	return *this;
}


Clock& Clock::operator = (ClockVal tv)
{
	_clock = tv;
	return *this;
}


void Clock::swap(Clock& timestamp)
{
	std::swap(_clock, timestamp._clock);
}


void Clock::update()
{
#if defined(POCO_OS_FAMILY_WINDOWS)

	LARGE_INTEGER perfCounter;
	LARGE_INTEGER perfFreq;
	if (QueryPerformanceCounter(&perfCounter) && QueryPerformanceFrequency(&perfFreq))
	{
		_clock = resolution()*(perfCounter.QuadPart/perfFreq.QuadPart);
		_clock += (perfCounter.QuadPart % perfFreq.QuadPart)*resolution()/perfFreq.QuadPart;
	}
	else throw Poco::SystemException("cannot get system clock");

#elif defined(__MACH__)

	clock_serv_t cs;
	mach_timespec_t ts;

	host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
	clock_get_time(cs, &ts);
	mach_port_deallocate(mach_task_self(), cs);
	
	_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;

#elif defined(POCO_VXWORKS)

	struct timespec ts;
#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
	if (clock_gettime(CLOCK_MONOTONIC, &ts))
		throw SystemException("cannot get system clock");
#else
	if (clock_gettime(CLOCK_REALTIME, &ts))
		throw SystemException("cannot get system clock");
#endif
	_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;

#elif defined(POCO_HAVE_CLOCK_GETTIME)

	struct timespec ts;
	if (clock_gettime(CLOCK_MONOTONIC, &ts))
		throw SystemException("cannot get system clock");
	_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;

#else

	Poco::Timestamp now;
	_clock = now.epochMicroseconds();
	
#endif
}


Clock::ClockDiff Clock::accuracy()
{
#if defined(POCO_OS_FAMILY_WINDOWS)

	LARGE_INTEGER perfFreq;
	if (QueryPerformanceFrequency(&perfFreq) && perfFreq.QuadPart > 0)
	{
		ClockVal acc = resolution()/perfFreq.QuadPart;
		return acc > 0 ? acc : 1;
	}
	else throw Poco::SystemException("cannot get system clock accuracy");

#elif defined(__MACH__)

	clock_serv_t cs;
	int nanosecs;
	mach_msg_type_number_t n = 1;

	host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
	clock_get_attributes(cs, CLOCK_GET_TIME_RES, (clock_attr_t)&nanosecs, &n);
	mach_port_deallocate(mach_task_self(), cs);
	
	ClockVal acc = nanosecs/1000;
	return acc > 0 ? acc : 1;

#elif defined(POCO_VXWORKS)

	struct timespec ts;
#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
	if (clock_getres(CLOCK_MONOTONIC, &ts))
		throw SystemException("cannot get system clock");
#else
	if (clock_getres(CLOCK_REALTIME, &ts))
		throw SystemException("cannot get system clock");
#endif
	ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
	return acc > 0 ? acc : 1;

#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)

	struct timespec ts;
	if (clock_getres(CLOCK_MONOTONIC, &ts))
		throw SystemException("cannot get system clock");
	
	ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
	return acc > 0 ? acc : 1;

#else

	return 1000;
	
#endif
}

	
bool Clock::monotonic()
{
#if defined(POCO_OS_FAMILY_WINDOWS)

	return true;

#elif defined(__MACH__)

	return true;

#elif defined(POCO_VXWORKS)

#if defined(CLOCK_MONOTONIC) // should be in VxWorks 6.x
	return true;
#else
	return false;
#endif

#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)

	return true;

#else

	return false;
	
#endif
}


} // namespace Poco