aboutsummaryrefslogtreecommitdiff
path: root/src/lib/mhd_mono_clock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/mhd_mono_clock.c')
-rw-r--r--src/lib/mhd_mono_clock.c178
1 files changed, 90 insertions, 88 deletions
diff --git a/src/lib/mhd_mono_clock.c b/src/lib/mhd_mono_clock.c
index 97dbfb9f..eee911fb 100644
--- a/src/lib/mhd_mono_clock.c
+++ b/src/lib/mhd_mono_clock.c
@@ -76,7 +76,8 @@ static clockid_t mono_clock_id = _MHD_UNWANTED_CLOCK;
76#endif /* HAVE_CLOCK_GETTIME */ 76#endif /* HAVE_CLOCK_GETTIME */
77 77
78/* sync clocks; reduce chance of value wrap */ 78/* sync clocks; reduce chance of value wrap */
79#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GET_TIME) || defined(HAVE_GETHRTIME) 79#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GET_TIME) || \
80 defined(HAVE_GETHRTIME)
80static time_t mono_clock_start; 81static time_t mono_clock_start;
81#endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GET_TIME || HAVE_GETHRTIME */ 82#endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GET_TIME || HAVE_GETHRTIME */
82static time_t sys_clock_start; 83static time_t sys_clock_start;
@@ -124,9 +125,9 @@ enum _MHD_mono_clock_source
124 */ 125 */
125 _MHD_CLOCK_GETTICKCOUNT64, 126 _MHD_CLOCK_GETTICKCOUNT64,
126 127
127 /** 128 /**
128 * QueryPerformanceCounter() / QueryPerformanceFrequency() 129 * QueryPerformanceCounter() / QueryPerformanceFrequency()
129 */ 130 */
130 _MHD_CLOCK_PERFCOUNTER 131 _MHD_CLOCK_PERFCOUNTER
131}; 132};
132 133
@@ -153,8 +154,8 @@ MHD_monotonic_sec_counter_init (void)
153 /* just a little syntactic trick to get the 154 /* just a little syntactic trick to get the
154 various following ifdef's to work out nicely */ 155 various following ifdef's to work out nicely */
155 if (0) 156 if (0)
156 { 157 {
157 } 158 }
158 else 159 else
159#ifdef HAVE_CLOCK_GETTIME 160#ifdef HAVE_CLOCK_GETTIME
160#ifdef CLOCK_MONOTONIC_COARSE 161#ifdef CLOCK_MONOTONIC_COARSE
@@ -163,11 +164,11 @@ MHD_monotonic_sec_counter_init (void)
163 /* but preferred since it's fast */ 164 /* but preferred since it's fast */
164 if (0 == clock_gettime (CLOCK_MONOTONIC_COARSE, 165 if (0 == clock_gettime (CLOCK_MONOTONIC_COARSE,
165 &ts)) 166 &ts))
166 { 167 {
167 mono_clock_id = CLOCK_MONOTONIC_COARSE; 168 mono_clock_id = CLOCK_MONOTONIC_COARSE;
168 mono_clock_start = ts.tv_sec; 169 mono_clock_start = ts.tv_sec;
169 mono_clock_source = _MHD_CLOCK_GETTIME; 170 mono_clock_source = _MHD_CLOCK_GETTIME;
170 } 171 }
171 else 172 else
172#endif /* CLOCK_MONOTONIC_COARSE */ 173#endif /* CLOCK_MONOTONIC_COARSE */
173#ifdef CLOCK_MONOTONIC_FAST 174#ifdef CLOCK_MONOTONIC_FAST
@@ -175,11 +176,11 @@ MHD_monotonic_sec_counter_init (void)
175 /* Can be affected by frequency adjustment, but preferred since it's fast */ 176 /* Can be affected by frequency adjustment, but preferred since it's fast */
176 if (0 == clock_gettime (CLOCK_MONOTONIC_FAST, 177 if (0 == clock_gettime (CLOCK_MONOTONIC_FAST,
177 &ts)) 178 &ts))
178 { 179 {
179 mono_clock_id = CLOCK_MONOTONIC_FAST; 180 mono_clock_id = CLOCK_MONOTONIC_FAST;
180 mono_clock_start = ts.tv_sec; 181 mono_clock_start = ts.tv_sec;
181 mono_clock_source = _MHD_CLOCK_GETTIME; 182 mono_clock_source = _MHD_CLOCK_GETTIME;
182 } 183 }
183 else 184 else
184#endif /* CLOCK_MONOTONIC_COARSE */ 185#endif /* CLOCK_MONOTONIC_COARSE */
185#ifdef CLOCK_MONOTONIC_RAW 186#ifdef CLOCK_MONOTONIC_RAW
@@ -187,11 +188,11 @@ MHD_monotonic_sec_counter_init (void)
187 /* Not affected by frequency adjustment, but don't count time in suspend */ 188 /* Not affected by frequency adjustment, but don't count time in suspend */
188 if (0 == clock_gettime (CLOCK_MONOTONIC_RAW, 189 if (0 == clock_gettime (CLOCK_MONOTONIC_RAW,
189 &ts)) 190 &ts))
190 { 191 {
191 mono_clock_id = CLOCK_MONOTONIC_RAW; 192 mono_clock_id = CLOCK_MONOTONIC_RAW;
192 mono_clock_start = ts.tv_sec; 193 mono_clock_start = ts.tv_sec;
193 mono_clock_source = _MHD_CLOCK_GETTIME; 194 mono_clock_source = _MHD_CLOCK_GETTIME;
194 } 195 }
195 else 196 else
196#endif /* CLOCK_MONOTONIC_RAW */ 197#endif /* CLOCK_MONOTONIC_RAW */
197#ifdef CLOCK_BOOTTIME 198#ifdef CLOCK_BOOTTIME
@@ -200,11 +201,11 @@ MHD_monotonic_sec_counter_init (void)
200 /* but can be slower value-getting than other clocks */ 201 /* but can be slower value-getting than other clocks */
201 if (0 == clock_gettime (CLOCK_BOOTTIME, 202 if (0 == clock_gettime (CLOCK_BOOTTIME,
202 &ts)) 203 &ts))
203 { 204 {
204 mono_clock_id = CLOCK_BOOTTIME; 205 mono_clock_id = CLOCK_BOOTTIME;
205 mono_clock_start = ts.tv_sec; 206 mono_clock_start = ts.tv_sec;
206 mono_clock_source = _MHD_CLOCK_GETTIME; 207 mono_clock_source = _MHD_CLOCK_GETTIME;
207 } 208 }
208 else 209 else
209#endif /* CLOCK_BOOTTIME */ 210#endif /* CLOCK_BOOTTIME */
210#ifdef CLOCK_MONOTONIC 211#ifdef CLOCK_MONOTONIC
@@ -213,11 +214,11 @@ MHD_monotonic_sec_counter_init (void)
213 /* On Linux it's not truly monotonic as it doesn't count time in suspend */ 214 /* On Linux it's not truly monotonic as it doesn't count time in suspend */
214 if (0 == clock_gettime (CLOCK_MONOTONIC, 215 if (0 == clock_gettime (CLOCK_MONOTONIC,
215 &ts)) 216 &ts))
216 { 217 {
217 mono_clock_id = CLOCK_MONOTONIC; 218 mono_clock_id = CLOCK_MONOTONIC;
218 mono_clock_start = ts.tv_sec; 219 mono_clock_start = ts.tv_sec;
219 mono_clock_source = _MHD_CLOCK_GETTIME; 220 mono_clock_source = _MHD_CLOCK_GETTIME;
220 } 221 }
221 else 222 else
222#endif /* CLOCK_BOOTTIME */ 223#endif /* CLOCK_BOOTTIME */
223#endif /* HAVE_CLOCK_GETTIME */ 224#endif /* HAVE_CLOCK_GETTIME */
@@ -225,15 +226,15 @@ MHD_monotonic_sec_counter_init (void)
225 /* Darwin-specific monotonic clock */ 226 /* Darwin-specific monotonic clock */
226 /* Should be monotonic as clock_set_time function always unconditionally */ 227 /* Should be monotonic as clock_set_time function always unconditionally */
227 /* failed on latest kernels */ 228 /* failed on latest kernels */
228 if ( (KERN_SUCCESS == host_get_clock_service (mach_host_self(), 229 if ( (KERN_SUCCESS == host_get_clock_service (mach_host_self (),
229 SYSTEM_CLOCK, 230 SYSTEM_CLOCK,
230 &mono_clock_service)) && 231 &mono_clock_service)) &&
231 (KERN_SUCCESS == clock_get_time (mono_clock_service, 232 (KERN_SUCCESS == clock_get_time (mono_clock_service,
232 &cur_time)) ) 233 &cur_time)) )
233 { 234 {
234 mono_clock_start = cur_time.tv_sec; 235 mono_clock_start = cur_time.tv_sec;
235 mono_clock_source = _MHD_CLOCK_GET_TIME; 236 mono_clock_source = _MHD_CLOCK_GET_TIME;
236 } 237 }
237 else 238 else
238#endif /* HAVE_CLOCK_GET_TIME */ 239#endif /* HAVE_CLOCK_GET_TIME */
239#ifdef _WIN32 240#ifdef _WIN32
@@ -241,25 +242,25 @@ MHD_monotonic_sec_counter_init (void)
241 /* W32 Vista or later specific monotonic clock */ 242 /* W32 Vista or later specific monotonic clock */
242 /* Available since Vista, ~15ms accuracy */ 243 /* Available since Vista, ~15ms accuracy */
243 if (1) 244 if (1)
244 { 245 {
245 tick_start = GetTickCount64 (); 246 tick_start = GetTickCount64 ();
246 mono_clock_source = _MHD_CLOCK_GETTICKCOUNT64; 247 mono_clock_source = _MHD_CLOCK_GETTICKCOUNT64;
247 } 248 }
248 else 249 else
249#else /* _WIN32_WINNT < 0x0600 */ 250#else /* _WIN32_WINNT < 0x0600 */
250 /* W32 specific monotonic clock */ 251 /* W32 specific monotonic clock */
251 /* Available on Windows 2000 and later */ 252 /* Available on Windows 2000 and later */
252 if (1) 253 if (1)
253 { 254 {
254 LARGE_INTEGER freq; 255 LARGE_INTEGER freq;
255 LARGE_INTEGER perf_counter; 256 LARGE_INTEGER perf_counter;
256 257
257 QueryPerformanceFrequency (&freq); /* never fail on XP and later */ 258 QueryPerformanceFrequency (&freq); /* never fail on XP and later */
258 QueryPerformanceCounter (&perf_counter); /* never fail on XP and later */ 259 QueryPerformanceCounter (&perf_counter); /* never fail on XP and later */
259 perf_freq = freq.QuadPart; 260 perf_freq = freq.QuadPart;
260 perf_start = perf_counter.QuadPart; 261 perf_start = perf_counter.QuadPart;
261 mono_clock_source = _MHD_CLOCK_PERFCOUNTER; 262 mono_clock_source = _MHD_CLOCK_PERFCOUNTER;
262 } 263 }
263 else 264 else
264#endif /* _WIN32_WINNT < 0x0600 */ 265#endif /* _WIN32_WINNT < 0x0600 */
265#endif /* _WIN32 */ 266#endif /* _WIN32 */
@@ -269,11 +270,11 @@ MHD_monotonic_sec_counter_init (void)
269 /* Not preferred due to be potentially resource-hungry */ 270 /* Not preferred due to be potentially resource-hungry */
270 if (0 == clock_gettime (CLOCK_HIGHRES, 271 if (0 == clock_gettime (CLOCK_HIGHRES,
271 &ts)) 272 &ts))
272 { 273 {
273 mono_clock_id = CLOCK_HIGHRES; 274 mono_clock_id = CLOCK_HIGHRES;
274 mono_clock_start = ts.tv_sec; 275 mono_clock_start = ts.tv_sec;
275 mono_clock_source = _MHD_CLOCK_GETTIME; 276 mono_clock_source = _MHD_CLOCK_GETTIME;
276 } 277 }
277 else 278 else
278#endif /* CLOCK_HIGHRES */ 279#endif /* CLOCK_HIGHRES */
279#endif /* HAVE_CLOCK_GETTIME */ 280#endif /* HAVE_CLOCK_GETTIME */
@@ -281,26 +282,26 @@ MHD_monotonic_sec_counter_init (void)
281 /* HP-UX and Solaris monotonic clock */ 282 /* HP-UX and Solaris monotonic clock */
282 /* Not preferred due to be potentially resource-hungry */ 283 /* Not preferred due to be potentially resource-hungry */
283 if (1) 284 if (1)
284 { 285 {
285 hrtime_start = gethrtime (); 286 hrtime_start = gethrtime ();
286 mono_clock_source = _MHD_CLOCK_GETHRTIME; 287 mono_clock_source = _MHD_CLOCK_GETHRTIME;
287 } 288 }
288 else 289 else
289#endif /* HAVE_GETHRTIME */ 290#endif /* HAVE_GETHRTIME */
290 { 291 {
291 /* no suitable clock source was found */ 292 /* no suitable clock source was found */
292 mono_clock_source = _MHD_CLOCK_NO_SOURCE; 293 mono_clock_source = _MHD_CLOCK_NO_SOURCE;
293 } 294 }
294 295
295#ifdef HAVE_CLOCK_GET_TIME 296#ifdef HAVE_CLOCK_GET_TIME
296 if ( (_MHD_CLOCK_GET_TIME != mono_clock_source) && 297 if ( (_MHD_CLOCK_GET_TIME != mono_clock_source) &&
297 (_MHD_INVALID_CLOCK_SERV != mono_clock_service) ) 298 (_MHD_INVALID_CLOCK_SERV != mono_clock_service) )
298 { 299 {
299 /* clock service was initialised but clock_get_time failed */ 300 /* clock service was initialised but clock_get_time failed */
300 mach_port_deallocate (mach_task_self(), 301 mach_port_deallocate (mach_task_self (),
301 mono_clock_service); 302 mono_clock_service);
302 mono_clock_service = _MHD_INVALID_CLOCK_SERV; 303 mono_clock_service = _MHD_INVALID_CLOCK_SERV;
303 } 304 }
304#else 305#else
305 (void) mono_clock_source; /* avoid compiler warning */ 306 (void) mono_clock_source; /* avoid compiler warning */
306#endif /* HAVE_CLOCK_GET_TIME */ 307#endif /* HAVE_CLOCK_GET_TIME */
@@ -317,11 +318,11 @@ MHD_monotonic_sec_counter_finish (void)
317{ 318{
318#ifdef HAVE_CLOCK_GET_TIME 319#ifdef HAVE_CLOCK_GET_TIME
319 if (_MHD_INVALID_CLOCK_SERV != mono_clock_service) 320 if (_MHD_INVALID_CLOCK_SERV != mono_clock_service)
320 { 321 {
321 mach_port_deallocate (mach_task_self(), 322 mach_port_deallocate (mach_task_self (),
322 mono_clock_service); 323 mono_clock_service);
323 mono_clock_service = _MHD_INVALID_CLOCK_SERV; 324 mono_clock_service = _MHD_INVALID_CLOCK_SERV;
324 } 325 }
325#endif /* HAVE_CLOCK_GET_TIME */ 326#endif /* HAVE_CLOCK_GET_TIME */
326} 327}
327 328
@@ -340,37 +341,38 @@ MHD_monotonic_sec_counter (void)
340 struct timespec ts; 341 struct timespec ts;
341 342
342 if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) && 343 if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) &&
343 (0 == clock_gettime (mono_clock_id , 344 (0 == clock_gettime (mono_clock_id,
344 &ts)) ) 345 &ts)) )
345 return ts.tv_sec - mono_clock_start; 346 return ts.tv_sec - mono_clock_start;
346#endif /* HAVE_CLOCK_GETTIME */ 347#endif /* HAVE_CLOCK_GETTIME */
347#ifdef HAVE_CLOCK_GET_TIME 348#ifdef HAVE_CLOCK_GET_TIME
348 if (_MHD_INVALID_CLOCK_SERV != mono_clock_service) 349 if (_MHD_INVALID_CLOCK_SERV != mono_clock_service)
349 { 350 {
350 mach_timespec_t cur_time; 351 mach_timespec_t cur_time;
351 352
352 if (KERN_SUCCESS == clock_get_time(mono_clock_service, 353 if (KERN_SUCCESS == clock_get_time (mono_clock_service,
353 &cur_time)) 354 &cur_time))
354 return cur_time.tv_sec - mono_clock_start; 355 return cur_time.tv_sec - mono_clock_start;
355 } 356 }
356#endif /* HAVE_CLOCK_GET_TIME */ 357#endif /* HAVE_CLOCK_GET_TIME */
357#if defined(_WIN32) 358#if defined(_WIN32)
358#if _WIN32_WINNT >= 0x0600 359#if _WIN32_WINNT >= 0x0600
359 if (1) 360 if (1)
360 return (time_t)(((uint64_t)(GetTickCount64() - tick_start)) / 1000); 361 return (time_t) (((uint64_t) (GetTickCount64 () - tick_start)) / 1000);
361#else /* _WIN32_WINNT < 0x0600 */ 362#else /* _WIN32_WINNT < 0x0600 */
362 if (0 != perf_freq) 363 if (0 != perf_freq)
363 { 364 {
364 LARGE_INTEGER perf_counter; 365 LARGE_INTEGER perf_counter;
365 366
366 QueryPerformanceCounter (&perf_counter); /* never fail on XP and later */ 367 QueryPerformanceCounter (&perf_counter); /* never fail on XP and later */
367 return (time_t)(((uint64_t)(perf_counter.QuadPart - perf_start)) / perf_freq); 368 return (time_t) (((uint64_t) (perf_counter.QuadPart - perf_start))
368 } 369 / perf_freq);
370 }
369#endif /* _WIN32_WINNT < 0x0600 */ 371#endif /* _WIN32_WINNT < 0x0600 */
370#endif /* _WIN32 */ 372#endif /* _WIN32 */
371#ifdef HAVE_GETHRTIME 373#ifdef HAVE_GETHRTIME
372 if (1) 374 if (1)
373 return (time_t)(((uint64_t) (gethrtime () - hrtime_start)) / 1000000000); 375 return (time_t) (((uint64_t) (gethrtime () - hrtime_start)) / 1000000000);
374#endif /* HAVE_GETHRTIME */ 376#endif /* HAVE_GETHRTIME */
375 377
376 return time (NULL) - sys_clock_start; 378 return time (NULL) - sys_clock_start;