]> mj.ucw.cz Git - libucw.git/commitdiff
Timer: Use monotonic clock on Linux
authorMartin Mares <mj@ucw.cz>
Sat, 11 Feb 2012 20:08:50 +0000 (21:08 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 11 Feb 2012 20:08:50 +0000 (21:08 +0100)
ucw/default.cfg
ucw/libucw.pc
ucw/perl/UCW/Configure/LibUCW.pm
ucw/timer.c

index ff970e0971e2439ccfdcf092891ca146ce1351e2..4d25df7d5d47b7873acf2381525e1bae01403fee 100644 (file)
@@ -59,6 +59,9 @@ Set("CONFIG_UCW_FB_DIRECT");
 # Use epoll (needs support in libc and kernel, default: auto-detect)
 # Set("CONFIG_UCW_EPOLL");
 
+# Use monotonic clock (default: yes on Linux, no elsewhere)
+# Set("CONFIG_UCW_MONOTONIC_CLOCK");
+
 # Which regular expression library should be used? If none is selected, we use BSD regex from libc.
 UnSet("CONFIG_UCW_POSIX_REGEX");
 UnSet("CONFIG_UCW_PCRE");
index b84b783d470d6d106033701c72211f9432d78847..7d1c1542ade4f7e713f154af9cdad52b16e4d360 100644 (file)
@@ -15,6 +15,12 @@ regex=-lpcre
 regex=
 #endif
 
+#ifdef CONFIG_UCW_MONOTONIC_CLOCK
+rt=-lrt
+#else
+rt=
+#endif
+
 # Override if you want to use the -pic version
 picsuffix=
 
@@ -26,4 +32,4 @@ Name: libucw
 Description: A library of utility functions and data structures
 Version: @UCW_VERSION@
 Cflags: -I${incdir}
-Libs: -L${libdir} -lucw${picsuffix} ${threads} ${regex}
+Libs: -L${libdir} -lucw${picsuffix} ${threads} ${regex} ${rt}
index 8349eb687c0b3e58533a37308a49f6d6c34e6cd4..87157005b47baf42de3cd7b54053be7eeb7d3535 100644 (file)
@@ -66,6 +66,11 @@ int main(void)
 FINIS
 });
 
+# Check if we want to use monotonic clock
+TestBool("CONFIG_UCW_MONOTONIC_CLOCK", "Checking for monotonic clock", sub {
+       return Get("CONFIG_LINUX");
+});
+
 # Darwin does not support BSD regexes, fix up
 if (IsSet("CONFIG_DARWIN")) {
        if (!IsSet("CONFIG_UCW_POSIX_REGEX") && !IsSet("CONFIG_UCW_PCRE")) {
index 31688dbe737c7fddc73c8b08713c23463665b015..f653ee5609a1429eb79709cd7822109e70fe5769 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- A Simple Millisecond Timer
  *
- *     (c) 2007 Martin Mares <mj@ucw.cz>
+ *     (c) 2007--2012 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 <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
+#include <time.h>
+
+#ifdef CONFIG_UCW_MONOTONIC_CLOCK
+
+timestamp_t
+get_timestamp(void)
+{
+  struct timespec ts;
+  if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
+    die("clock_gettime failed: %m");
+  return (timestamp_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+}
+
+#else
 
 timestamp_t
 get_timestamp(void)
@@ -25,6 +39,8 @@ get_timestamp(void)
        ;
 }
 
+#endif
+
 void
 init_timer(timestamp_t *timer)
 {
@@ -45,3 +61,15 @@ switch_timer(timestamp_t *oldt, timestamp_t *newt)
   *newt = get_timestamp();
   return MIN(*newt-*oldt, ~0U);
 }
+
+#ifdef TEST
+
+#include <stdio.h>
+
+int main(void)
+{
+  printf("%ju\n", (intmax_t) get_timestamp());
+  return 0;
+}
+
+#endif