From e21ac9b1bda89eea1b8ae8402a4edc3afb2de8d8 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Feb 2007 22:02:21 +0100 Subject: [PATCH] Replaced the old timing functions by something more useful. get_timer() using global state was making it unusable in many cases, especially in library routines. I decided to make the callers specify an explicit context (a timer) instead and re-used the idea of millisecond timestamps from mainloop.c. I've moved timestamp_t to config.h, introduced global get_timestamp() and replaced the timer functions by ones with an explicit timestamp_t * argument. --- lib/config.h | 3 ++- lib/lib.h | 8 ++++---- lib/mainloop.h | 1 - lib/timer.c | 47 +++++++++++++++++++---------------------------- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/lib/config.h b/lib/config.h index 72be2f98..22140f20 100644 --- a/lib/config.h +++ b/lib/config.h @@ -37,7 +37,8 @@ typedef uint64_t u64; /* exactly 64 bits, unsigned */ typedef int64_t s64; /* exactly 64 bits, signed */ typedef unsigned int uns; /* at least 32 bits */ -typedef u32 sh_time_t; /* Timestamp */ +typedef u32 sh_time_t; /* seconds since UNIX epoch */ +typedef s64 timestamp_t; /* milliseconds since UNIX epoch */ #ifdef CONFIG_LARGE_FILES /* File positions */ typedef s64 sh_off_t; diff --git a/lib/lib.h b/lib/lib.h index 554d9ecb..d8a1ddff 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -192,11 +192,11 @@ uns prev_table_prime(uns x); /* timer.c */ -struct timeval; +timestamp_t get_timestamp(void); -void init_timer(void); -uns get_timer(void); -void get_last_timeval(struct timeval *tv); +void init_timer(timestamp_t *timer); +uns get_timer(timestamp_t *timer); +uns switch_timer(timestamp_t *old, timestamp_t *new); /* regex.c */ diff --git a/lib/mainloop.h b/lib/mainloop.h index f321c2b5..136d90e0 100644 --- a/lib/mainloop.h +++ b/lib/mainloop.h @@ -12,7 +12,6 @@ #include "lib/clists.h" -typedef s64 timestamp_t; /* We measure time in milliseconds */ extern timestamp_t main_now; /* Current time in milliseconds since UNIX epoch */ extern sh_time_t main_now_seconds; /* Current time in seconds since the epoch */ extern uns main_shutdown; diff --git a/lib/timer.c b/lib/timer.c index 86a2d198..761ab30e 100644 --- a/lib/timer.c +++ b/lib/timer.c @@ -1,7 +1,7 @@ /* - * UCW Library -- Execution Timing + * UCW Library -- A Simple Millisecond Timer * - * (c) 1997 Martin Mares + * (c) 2007 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -13,40 +13,31 @@ #include #include -static struct timeval last_tv; - -uns -get_timer(void) +timestamp_t +get_timestamp(void) { struct timeval tv; - uns diff; - gettimeofday(&tv, NULL); - if (tv.tv_sec < last_tv.tv_sec - || tv.tv_sec == last_tv.tv_sec && tv.tv_usec < last_tv.tv_usec) - diff = 0; - else - { - if (tv.tv_sec == last_tv.tv_sec) - diff = (tv.tv_usec - last_tv.tv_usec + 500) / 1000; - else - { - diff = 1000 * (tv.tv_sec - last_tv.tv_sec - 1); - diff += (1000500 - last_tv.tv_usec + tv.tv_usec) / 1000; - } - } - last_tv = tv; - return diff; + return (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; } void -init_timer(void) +init_timer(timestamp_t *timer) { - gettimeofday(&last_tv, NULL); + *timer = get_timestamp(); } -void -get_last_timeval(struct timeval *tv) +uns +get_timer(timestamp_t *timer) +{ + timestamp_t t = *timer; + *timer = get_timestamp(); + return MIN(*timer-t, ~0U); +} + +uns +switch_timer(timestamp_t *old, timestamp_t *new) { - *tv = last_tv; + *new = get_timestamp(); + return MIN(*new-*old, ~0U); } -- 2.39.2