diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2021-08-29 22:07:13 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2021-08-29 22:07:13 +0300 |
commit | e544cf36300563fc4ed790b80a80b52669529bbb (patch) | |
tree | e72915540a2e8a6ffed09e94fec76fd8d876cb87 /src/microhttpd/mhd_mono_clock.c | |
parent | cc650acad6f6777b35045c3387a264a6dcc5ffab (diff) | |
download | libmicrohttpd-e544cf36300563fc4ed790b80a80b52669529bbb.tar.gz libmicrohttpd-e544cf36300563fc4ed790b80a80b52669529bbb.zip |
Added fallback functions for MHD_monotonic_msec_counter() values
Diffstat (limited to 'src/microhttpd/mhd_mono_clock.c')
-rw-r--r-- | src/microhttpd/mhd_mono_clock.c | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/src/microhttpd/mhd_mono_clock.c b/src/microhttpd/mhd_mono_clock.c index 122aab77..cc93ddfb 100644 --- a/src/microhttpd/mhd_mono_clock.c +++ b/src/microhttpd/mhd_mono_clock.c | |||
@@ -30,20 +30,12 @@ | |||
30 | #undef HAVE_CLOCK_GETTIME | 30 | #undef HAVE_CLOCK_GETTIME |
31 | #endif /* _WIN32 && ! __CYGWIN__ && HAVE_CLOCK_GETTIME */ | 31 | #endif /* _WIN32 && ! __CYGWIN__ && HAVE_CLOCK_GETTIME */ |
32 | 32 | ||
33 | #ifdef HAVE_CLOCK_GETTIME | 33 | #ifdef HAVE_TIME_H |
34 | #include <time.h> | 34 | #include <time.h> |
35 | #endif /* HAVE_CLOCK_GETTIME */ | 35 | #endif /* HAVE_TIME_H */ |
36 | |||
37 | #ifdef HAVE_GETHRTIME | ||
38 | #ifdef HAVE_SYS_TIME_H | 36 | #ifdef HAVE_SYS_TIME_H |
39 | /* Solaris defines gethrtime() in sys/time.h */ | ||
40 | #include <sys/time.h> | 37 | #include <sys/time.h> |
41 | #endif /* HAVE_SYS_TIME_H */ | 38 | #endif /* HAVE_SYS_TIME_H */ |
42 | #ifdef HAVE_TIME_H | ||
43 | /* HP-UX defines gethrtime() in time.h */ | ||
44 | #include <time.h> | ||
45 | #endif /* HAVE_TIME_H */ | ||
46 | #endif /* HAVE_GETHRTIME */ | ||
47 | 39 | ||
48 | #ifdef HAVE_CLOCK_GET_TIME | 40 | #ifdef HAVE_CLOCK_GET_TIME |
49 | #include <mach/mach.h> | 41 | #include <mach/mach.h> |
@@ -65,6 +57,10 @@ static clock_serv_t mono_clock_service = _MHD_INVALID_CLOCK_SERV; | |||
65 | #include <stdint.h> | 57 | #include <stdint.h> |
66 | #endif /* _WIN32 */ | 58 | #endif /* _WIN32 */ |
67 | 59 | ||
60 | #ifndef NULL | ||
61 | #define NULL ((void*)0) | ||
62 | #endif /* ! NULL */ | ||
63 | |||
68 | #ifdef HAVE_CLOCK_GETTIME | 64 | #ifdef HAVE_CLOCK_GETTIME |
69 | #ifdef CLOCK_REALTIME | 65 | #ifdef CLOCK_REALTIME |
70 | #define _MHD_UNWANTED_CLOCK CLOCK_REALTIME | 66 | #define _MHD_UNWANTED_CLOCK CLOCK_REALTIME |
@@ -80,6 +76,10 @@ static clockid_t mono_clock_id = _MHD_UNWANTED_CLOCK; | |||
80 | defined(HAVE_GETHRTIME) | 76 | defined(HAVE_GETHRTIME) |
81 | static time_t mono_clock_start; | 77 | static time_t mono_clock_start; |
82 | #endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GET_TIME || HAVE_GETHRTIME */ | 78 | #endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GET_TIME || HAVE_GETHRTIME */ |
79 | #if defined(HAVE_TIMESPEC_GET) || defined(HAVE_GETTIMEOFDAY) | ||
80 | /* The start value shared for timespec_get() and gettimeofday () */ | ||
81 | static time_t gettime_start; | ||
82 | #endif /* HAVE_TIMESPEC_GET || HAVE_GETTIMEOFDAY */ | ||
83 | static time_t sys_clock_start; | 83 | static time_t sys_clock_start; |
84 | #ifdef HAVE_GETHRTIME | 84 | #ifdef HAVE_GETHRTIME |
85 | static hrtime_t hrtime_start; | 85 | static hrtime_t hrtime_start; |
@@ -332,6 +332,25 @@ MHD_monotonic_sec_counter_init (void) | |||
332 | (void) mono_clock_source; /* avoid compiler warning */ | 332 | (void) mono_clock_source; /* avoid compiler warning */ |
333 | #endif /* HAVE_CLOCK_GET_TIME */ | 333 | #endif /* HAVE_CLOCK_GET_TIME */ |
334 | 334 | ||
335 | #ifdef HAVE_TIMESPEC_GET | ||
336 | if (1) | ||
337 | { | ||
338 | struct timespec tsg; | ||
339 | if (TIME_UTC == timespec_get (&tsg, TIME_UTC)) | ||
340 | gettime_start = tsg.tv_sec; | ||
341 | else | ||
342 | gettime_start = 0; | ||
343 | } | ||
344 | #elif defined (HAVE_GETTIMEOFDAY) | ||
345 | if (1) | ||
346 | { | ||
347 | struct timeval tv; | ||
348 | if (0 == gettimeofday (&tv, NULL)) | ||
349 | gettime_start = tv.tv_sec; | ||
350 | else | ||
351 | gettime_start = 0; | ||
352 | } | ||
353 | #endif /* HAVE_GETTIMEOFDAY */ | ||
335 | sys_clock_start = time (NULL); | 354 | sys_clock_start = time (NULL); |
336 | } | 355 | } |
337 | 356 | ||
@@ -415,9 +434,11 @@ MHD_monotonic_sec_counter (void) | |||
415 | uint64_t | 434 | uint64_t |
416 | MHD_monotonic_msec_counter (void) | 435 | MHD_monotonic_msec_counter (void) |
417 | { | 436 | { |
418 | #ifdef HAVE_CLOCK_GETTIME | 437 | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_TIMESPEC_GET) |
419 | struct timespec ts; | 438 | struct timespec ts; |
439 | #endif /* HAVE_CLOCK_GETTIME || HAVE_TIMESPEC_GET */ | ||
420 | 440 | ||
441 | #ifdef HAVE_CLOCK_GETTIME | ||
421 | if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) && | 442 | if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) && |
422 | (0 == clock_gettime (mono_clock_id, | 443 | (0 == clock_gettime (mono_clock_id, |
423 | &ts)) ) | 444 | &ts)) ) |
@@ -457,5 +478,21 @@ MHD_monotonic_msec_counter (void) | |||
457 | return ((uint64_t) (gethrtime () - hrtime_start)) / 1000000; | 478 | return ((uint64_t) (gethrtime () - hrtime_start)) / 1000000; |
458 | #endif /* HAVE_GETHRTIME */ | 479 | #endif /* HAVE_GETHRTIME */ |
459 | 480 | ||
481 | /* Fallbacks, affected by system time change */ | ||
482 | #ifdef HAVE_TIMESPEC_GET | ||
483 | if (TIME_UTC == timespec_get (&ts, TIME_UTC)) | ||
484 | return (uint64_t) (((uint64_t) (ts.tv_sec - gettime_start)) * 1000 | ||
485 | + (ts.tv_nsec / 1000000)); | ||
486 | #elif defined (HAVE_GETTIMEOFDAY) | ||
487 | if (1) | ||
488 | { | ||
489 | struct timeval tv; | ||
490 | if (0 == gettimeofday (&tv, NULL)) | ||
491 | return (uint64_t) (((uint64_t) (tv.tv_sec - gettime_start)) * 1000 | ||
492 | + (tv.tv_usec / 1000)); | ||
493 | } | ||
494 | #endif /* HAVE_GETTIMEOFDAY */ | ||
495 | |||
496 | /* The last resort fallback with very low resolution */ | ||
460 | return (uint64_t) (time (NULL) - sys_clock_start) * 1000; | 497 | return (uint64_t) (time (NULL) - sys_clock_start) * 1000; |
461 | } | 498 | } |