From d36f9efe0d0fb028d6b2574b76956ce2ae22ea4f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 11 Feb 2012 21:08:50 +0100 Subject: [PATCH] Timer: Use monotonic clock on Linux --- ucw/default.cfg | 3 +++ ucw/libucw.pc | 8 +++++++- ucw/perl/UCW/Configure/LibUCW.pm | 5 +++++ ucw/timer.c | 30 +++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ucw/default.cfg b/ucw/default.cfg index ff970e09..4d25df7d 100644 --- a/ucw/default.cfg +++ b/ucw/default.cfg @@ -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"); diff --git a/ucw/libucw.pc b/ucw/libucw.pc index b84b783d..7d1c1542 100644 --- a/ucw/libucw.pc +++ b/ucw/libucw.pc @@ -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} diff --git a/ucw/perl/UCW/Configure/LibUCW.pm b/ucw/perl/UCW/Configure/LibUCW.pm index 8349eb68..87157005 100644 --- a/ucw/perl/UCW/Configure/LibUCW.pm +++ b/ucw/perl/UCW/Configure/LibUCW.pm @@ -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")) { diff --git a/ucw/timer.c b/ucw/timer.c index 31688dbe..f653ee56 100644 --- a/ucw/timer.c +++ b/ucw/timer.c @@ -1,7 +1,7 @@ /* * UCW Library -- A Simple Millisecond Timer * - * (c) 2007 Martin Mares + * (c) 2007--2012 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -12,6 +12,20 @@ #include #include #include +#include + +#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 + +int main(void) +{ + printf("%ju\n", (intmax_t) get_timestamp()); + return 0; +} + +#endif -- 2.39.2