From 96d0be2ec5d4ec46aa7b86438ce554a371a87d29 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Tue, 8 Jul 2008 12:27:20 +0000 Subject: [PATCH] [MERGE] libi2ncommon: (reinhard) added support for POSIX.1b realtime and monotonic clock to timefunc. --- src/timefunc.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ src/timefunc.hxx | 9 +++++++ test/test_timefunc.cpp | 29 +++++++++++++++++++++++ 3 files changed, 98 insertions(+), 0 deletions(-) diff --git a/src/timefunc.cpp b/src/timefunc.cpp index 26fe10a..276a681 100644 --- a/src/timefunc.cpp +++ b/src/timefunc.cpp @@ -18,11 +18,25 @@ #include #include +#include #include +#include #include #include + +// 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&) + + diff --git a/src/timefunc.hxx b/src/timefunc.hxx index c7b4e95..5ae357b 100644 --- a/src/timefunc.hxx +++ b/src/timefunc.hxx @@ -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 diff --git a/test/test_timefunc.cpp b/test/test_timefunc.cpp index 30c0997..bc2aaed 100644 --- a/test/test_timefunc.cpp +++ b/test/test_timefunc.cpp @@ -14,6 +14,9 @@ #include #include +#include + + 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 -- 1.7.1