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;
/* 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 */
#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;
/*
- * UCW Library -- Execution Timing
+ * UCW Library -- A Simple Millisecond Timer
*
- * (c) 1997 Martin Mares <mj@ucw.cz>
+ * (c) 2007 Martin Mares <mj@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
#include <stdlib.h>
#include <sys/time.h>
-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);
}