4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief ClockService inline non-template implementation */
27 #include <boost/date_time/posix_time/posix_time_types.hpp>
29 #define prefix_ inline
30 ///////////////////////////////cci.p///////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////
35 prefix_ senf::ClockService::clock_type senf::ClockService::now()
37 return instance().now_m();
40 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock)
42 return instance().abstime_m(clock);
45 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time)
47 return instance().clock_m(time);
50 prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time)
52 return clock( boost::posix_time::from_time_t(time) );
55 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
57 return from_time_t(time.tv_sec) + ClockService::microseconds(time.tv_usec);
60 prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type v)
65 prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(int64_type v)
67 return v * nanoseconds(1000);
70 prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type v)
72 return v * microseconds(1000);
75 prefix_ senf::ClockService::clock_type senf::ClockService::seconds(int64_type v)
77 return v * milliseconds(1000);
80 prefix_ senf::ClockService::clock_type senf::ClockService::minutes(int64_type v)
82 return v * seconds(60);
85 prefix_ senf::ClockService::clock_type senf::ClockService::hours(int64_type v)
87 return v * minutes(60);
90 prefix_ senf::ClockService::clock_type senf::ClockService::days(int64_type v)
95 prefix_ senf::ClockService::clock_type senf::ClockService::in_nanoseconds(int64_type v)
100 prefix_ senf::ClockService::clock_type senf::ClockService::in_microseconds(int64_type v)
102 return v / nanoseconds(1000);
105 prefix_ senf::ClockService::clock_type senf::ClockService::in_milliseconds(int64_type v)
107 return v / microseconds(1000);
110 prefix_ senf::ClockService::clock_type senf::ClockService::in_seconds(int64_type v)
112 return v / milliseconds(1000);
115 prefix_ senf::ClockService::clock_type senf::ClockService::in_minutes(int64_type v)
117 return v / seconds(60);
120 prefix_ senf::ClockService::clock_type senf::ClockService::in_hours(int64_type v)
122 return v / minutes(60);
125 prefix_ senf::ClockService::clock_type senf::ClockService::in_days(int64_type v)
127 return v / hours(24);
130 prefix_ void senf::ClockService::restart()
132 instance().restart_m();
135 ////////////////////////////////////////
138 prefix_ senf::ClockService::clock_type senf::ClockService::now_m()
140 // We want to make the normal case (no skew) really fast. This first 'checkSkew' *might*
141 // transiently fail if a SIGALRM is delivered in the midst of the test. updateSkew will
142 // therefore block signals and do the check again to make sure.
144 // The opposite case (the test returns 'false' even though it should return 'true') is so highly
145 // improbable that it is treated as academic. (it will be catched by the next SIGALRM)
147 boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
151 // 'clock' will pick up the corrected base_ value if needed.
152 return clock_m(time);
155 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock)
157 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
158 return base_ + boost::posix_time::nanoseconds(clock);
160 return base_ + boost::posix_time::microseconds((clock+500)/1000);
164 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
166 ///\fixme What happens, if base_ is changed in SIGALRM while reading it here ?
168 // Idea: Have *two* base values: one is written by the SIGALRM handler, the other is only
169 // Written by synchronous code. If they differ, we block signals, copy over and continue. If
170 // they transiently differ because we are reading the SIGALRM value while it is being changed
171 // this does not matter: We will then still copy it over.
173 boost::posix_time::time_duration delta (time - base_);
174 return clock_type( delta.ticks() )
175 * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
178 prefix_ bool senf::ClockService::checkSkew(boost::posix_time::ptime time)
180 boost::posix_time::ptime h (heartbeat_); // reduce chance for race condition
181 return time < h || (time - h) > boost::posix_time::seconds(2*CheckInterval);
184 prefix_ void senf::ClockService::clockSkew(boost::posix_time::ptime time,
185 boost::posix_time::ptime expected)
187 base_ += (time - expected);
190 ///////////////////////////////cci.e///////////////////////////////////////
197 // comment-column: 40
198 // c-file-style: "senf"
199 // indent-tabs-mode: nil
200 // ispell-local-dictionary: "american"
201 // compile-command: "scons -u test"