X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ftimer.c;h=761ab30e9be86ab1cb7e7f6613769e6263f70529;hb=534019614dd611e7df29a0c8c59f2a869a2c0c39;hp=7aacaf686b35625e62eff917119ab544de116836;hpb=1571781022499a9d0c32d249f89945d034d1cbff;p=libucw.git diff --git a/lib/timer.c b/lib/timer.c index 7aacaf68..761ab30e 100644 --- a/lib/timer.c +++ b/lib/timer.c @@ -1,7 +1,10 @@ /* - * Sherlock 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. */ #include "lib/lib.h" @@ -10,34 +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) +{ + *timer = get_timestamp(); +} + +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) { - gettimeofday(&last_tv, NULL); + *new = get_timestamp(); + return MIN(*new-*old, ~0U); }