// $Id$ // // Copyright (C) 2007 // Fraunhofer Institute for Open Communication Systems (FOKUS) // // The contents of this file are subject to the Fraunhofer FOKUS Public License // Version 1.0 (the "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // http://senf.berlios.de/license.html // // The Fraunhofer FOKUS Public License Version 1.0 is based on, // but modifies the Mozilla Public License Version 1.1. // See the full license text for the amendments. // // Software distributed under the License is distributed on an "AS IS" basis, // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License // for the specific language governing rights and limitations under the License. // // The Original Code is Fraunhofer FOKUS code. // // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. // (registered association), Hansastraße 27 c, 80686 Munich, Germany. // All Rights Reserved. // // Contributor(s): // Stefan Bund /** \file \brief ClockService inline non-template implementation */ // Custom includes #include #include #include #include #define prefix_ inline //-///////////////////////////////////////////////////////////////////////////////////////////////// //-///////////////////////////////////////////////////////////////////////////////////////////////// // senf::ClockService prefix_ senf::ClockService::clock_type senf::ClockService::now() { struct timespec spec; if (clock_gettime(CLOCK_MONOTONIC, &spec) < 0) SENF_THROW_SYSTEM_EXCEPTION("clock_gettime()"); return SENF_INT2CLOCKTYPE(spec.tv_sec * 1000000000LL + spec.tv_nsec); } //-///////////////////////////////////////////////////////////////////////////////////////////////// // private members namespace senf { namespace scheduler { ClockService::clock_type now(); } } prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time) { if (scheduler::now() - baseClock_ > clock_type(1000000000ll)) restart_m(); boost::posix_time::time_duration delta (time - baseAbstime_); return baseClock_ + clock_type( delta.ticks() ) * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() ); } prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock) { if (clock == 0) return abstime_type(); if (scheduler::now() - baseClock_ > clock_type(1000000000ll)) restart_m(); #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG return baseAbstime_ + boost::posix_time::nanoseconds(clock-baseClock_); #else return baseAbstime_ + boost::posix_time::microseconds((clock.value() - baseClock_.value() + 500 )/1000); #endif } prefix_ senf::ClockService::ClockService() { restart_m(); } prefix_ void senf::ClockService::restart_m() { baseAbstime_ = boost::posix_time::microsec_clock::universal_time(); baseClock_ = now(); } // public members prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock) { return instance().abstime_m(clock); } prefix_ senf::ClockService::reltime_type senf::ClockService::reltime(clock_type clock) { #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG return boost::posix_time::nanoseconds(clock); #else return boost::posix_time::microseconds((clock.value() + 500)/1000); #endif } prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time) { return instance().clock_m(time); } prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time) { return clock( boost::posix_time::from_time_t(time) ); } prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type v) { return SENF_INT2CLOCKTYPE(v); } prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(nanoseconds(1000))); } prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(microseconds(1000))); } prefix_ senf::ClockService::clock_type senf::ClockService::seconds(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(milliseconds(1000))); } prefix_ senf::ClockService::clock_type senf::ClockService::minutes(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(seconds(60))); } prefix_ senf::ClockService::clock_type senf::ClockService::hours(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(minutes(60))); } prefix_ senf::ClockService::clock_type senf::ClockService::days(int64_type v) { return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(hours(24))); } prefix_ senf::ClockService::int64_type senf::ClockService::in_nanoseconds(clock_type v) { return SENF_CLOCKTYPEVAL(v); } prefix_ senf::ClockService::int64_type senf::ClockService::in_microseconds(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(nanoseconds(1000))); } prefix_ senf::ClockService::int64_type senf::ClockService::in_milliseconds(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(microseconds(1000))); } prefix_ senf::ClockService::int64_type senf::ClockService::in_seconds(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(milliseconds(1000))); } prefix_ senf::ClockService::int64_type senf::ClockService::in_minutes(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(seconds(60))); } prefix_ senf::ClockService::int64_type senf::ClockService::in_hours(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(minutes(60))) ; } prefix_ senf::ClockService::int64_type senf::ClockService::in_days(clock_type v) { return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(hours(24))) ; } prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time) { return from_time_t(time.tv_sec) + microseconds(time.tv_usec); } prefix_ void senf::ClockService::restart() { instance().restart_m(); } //-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_ // Local Variables: // mode: c++ // fill-column: 100 // comment-column: 40 // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" // compile-command: "scons -u test" // End: