From 04e07442bbc8a5d8515fa1eea52cb15ebd2cc992 Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Fri, 31 Oct 2014 12:52:23 +0100 Subject: Use new get_tick_count() to avoid timing issues on packets --- src/utils.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 5dd871d..ceb65e1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "utils.h" @@ -298,15 +299,26 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form return 1; } +void get_tick_count(struct timeval * tv) +{ + struct timespec ts; + if(0 == clock_gettime(CLOCK_MONOTONIC, &ts)) { + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + } else { + gettimeofday(tv, NULL); + } +} + /** * Get number of milliseconds since the epoch. */ uint64_t mstime64(void) { struct timeval tv; - gettimeofday(&tv, NULL); + get_tick_count(&tv); - // Careful, avoid overflow on 32 bit systems - // time_t could be 4 bytes + // Careful, avoid overflow on 32 bit systems + // time_t could be 4 bytes return ((long long)tv.tv_sec) * 1000LL + ((long long)tv.tv_usec) / 1000LL; } -- cgit v1.1-32-gdbae