[MERGE] libi2ncommon: (reinhard) added support for POSIX.1b realtime and monotonic...
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 8 Jul 2008 12:27:20 +0000 (12:27 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 8 Jul 2008 12:27:20 +0000 (12:27 +0000)
src/timefunc.cpp
src/timefunc.hxx
test/test_timefunc.cpp

index 26fe10a..276a681 100644 (file)
 #include <algorithm>
 
 #include <time.h>
+#include <unistd.h>
 #include <sys/timeb.h>
+#include <sys/syscall.h>
 
 #include <timefunc.hxx>
 #include <i18n.h>
 
+
+// define missing POSIX.1b constants...
+
+#ifndef CLOCK_REALTIME
+#define CLOCK_REALTIME 0
+#endif
+#ifndef CLOCK_MONOTONIC
+#define CLOCK_MONOTONIC 1
+#endif
+
+
+
 using namespace std;
 
 double prec_time(void)
@@ -788,3 +802,49 @@ Intervals& Intervals::operator-=(const Intervals& other)
     }
     return *this;
 } // eo operator-=(const Intervals&)
+
+
+
+/*
+** clock funcs:
+*/
+
+
+/**
+ * @brief fetches the value from the monotonic clock source.
+ * @param[out] seconds the seconds.
+ * @param[out] nano_seconds the nano seconds.
+ * @return @a true if the clock was successfully read.
+ */
+bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
+{
+    struct timespec tp[1];
+    int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
+    if (0 == res)
+    {
+        seconds= tp->tv_sec;
+        nano_seconds= tp->tv_nsec;
+    }
+    return (res==0);
+} // eo monotonic_clock_gettime(long int&,long int&)
+
+
+/**
+ * @brief fetches the value from the monotonic clock source.
+ * @param[out] seconds the seconds.
+ * @param[out] nano_seconds the nano seconds.
+ * @return @a true if the clock was successfully read.
+ */
+bool realtime_clock_gettime(long int& seconds, long int& nano_seconds)
+{
+    struct timespec tp[1];
+    int res= ::syscall(__NR_clock_gettime, CLOCK_REALTIME, tp);
+    if (0 == res)
+    {
+        seconds= tp->tv_sec;
+        nano_seconds= tp->tv_nsec;
+    }
+    return (res==0);
+} // eo realtime_clock_gettime(long int&,long int&)
+
+
index c7b4e95..5ae357b 100644 (file)
@@ -304,6 +304,15 @@ class Intervals
 }; // eo Intervals
 
 
+/*
+** clock funcs:
+*/
+
+
+bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds);
+
+bool realtime_clock_gettime(long int& seconds, long int& nano_seconds);
+
 
 
 #endif
index 30c0997..bc2aaed 100644 (file)
@@ -14,6 +14,9 @@
 #include <timefunc.hxx>
 #include <filefunc.hxx>
 
+#include <unistd.h>
+
+
 using namespace std;
 using namespace I2n;
 using namespace CppUnit;
@@ -45,6 +48,8 @@ class TestTimeFunc : public TestFixture
 
    CPPUNIT_TEST(IntervalComparisons);
 
+   CPPUNIT_TEST(MonotonicClock);
+
    CPPUNIT_TEST_SUITE_END();
 
 protected:
@@ -339,6 +344,30 @@ public:
        CPPUNIT_ASSERT_EQUAL( false, intervals2.contains( intervals1 ));
    } // eo IntervalComparisons()
 
+
+
+   void MonotonicClock()
+   {
+       long sec0, nsec0;
+       long sec1, nsec1;
+
+       bool res = monotonic_clock_gettime(sec0,nsec0);
+       CPPUNIT_ASSERT_EQUAL( true, res );
+
+       usleep(250000);
+       res= monotonic_clock_gettime(sec1,nsec1);
+       CPPUNIT_ASSERT_EQUAL( true, res);
+
+       long delta_sec = sec1 - sec0;
+       long delta_nsec= nsec1 - nsec0;
+
+       long delta_millisec= ( delta_nsec / 1000000L) + delta_sec * 1000L;
+
+       CPPUNIT_ASSERT( delta_millisec >= 250 - /*fuzz*/ 1);
+       CPPUNIT_ASSERT( delta_millisec < 300 );
+   } // eo MonotonicClock()
+
+
    
 }; // eo class TestTimeFunc