]> mj.ucw.cz Git - libucw.git/blobdiff - lib/timer.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / timer.c
index c355d3b7de1bd5d54e5fac5290bf4dad2624b047..761ab30e9be86ab1cb7e7f6613769e6263f70529 100644 (file)
@@ -1,43 +1,43 @@
 /*
 /*
- *     Sherlock Library -- Prime Number Tests
+ *     UCW Library -- A Simple Millisecond Timer
  *
  *
- *     (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.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 "lib/lib.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
 
-#include "lib.h"
-
-static struct timeval last_tv;
-
-uns
-get_timer(void)
+timestamp_t
+get_timestamp(void)
 {
   struct timeval tv;
 {
   struct timeval tv;
-  uns diff;
-
   gettimeofday(&tv, NULL);
   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
 }
 
 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);
 }
 }