From de685b3cb6f9397818c7f774eddbd802db5dde7a Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sat, 18 Sep 2021 11:39:42 +0200 Subject: [PATCH] Minor changes at timer.c: Improved precision --- src/libtimer/timer.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/libtimer/timer.c b/src/libtimer/timer.c index 955d790..174533f 100644 --- a/src/libtimer/timer.c +++ b/src/libtimer/timer.c @@ -21,7 +21,9 @@ #include #include #include -#include +//#include +#include +#include #include "timer.h" static struct timer *timer_head = NULL; @@ -29,11 +31,11 @@ static struct timer **timer_tail_p = &timer_head; double get_time(void) { - struct timeval tv; + static struct timespec tv; - gettimeofday(&tv, NULL); + clock_gettime(CLOCK_REALTIME, &tv); - return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; + return (double)tv.tv_sec + (double)tv.tv_nsec / 1000000000.0; } void timer_init(struct timer *timer, void (*fn)(struct timer *timer), void *priv) @@ -66,15 +68,11 @@ void timer_exit(struct timer *timer) void timer_start(struct timer *timer, double duration) { - struct timeval tv; - if (!timer->linked) { fprintf(stderr, "Timer is not initialized, aborting!\n"); abort(); } - gettimeofday(&tv, NULL); - timer->duration = duration; timer->timeout = get_time() + duration; }