Move include files in debian packge into 'senf' subdirectory
[senf.git] / Scheduler / ClockService.cci
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ClockService inline non-template implementation */
25
26 // Custom includes
27 #include <boost/date_time/posix_time/posix_time_types.hpp>
28
29 #define prefix_ inline
30 ///////////////////////////////cci.p///////////////////////////////////////
31
32 ///////////////////////////////////////////////////////////////////////////
33 // senf::ClockService
34
35 prefix_ senf::ClockService::clock_type senf::ClockService::now()
36 {
37     return instance().now_m();
38 }
39
40 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock)
41 {
42     return instance().abstime_m(clock);
43 }
44
45 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time)
46 {
47     return instance().clock_m(time);
48 }
49
50 prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(clock_type v)
51 {
52     return v;
53 }
54
55 prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(clock_type v)
56 {
57     return v * nanoseconds(1000);
58 }
59
60 prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(clock_type v)
61 {
62     return v * microseconds(1000);
63 }
64
65 prefix_ senf::ClockService::clock_type senf::ClockService::seconds(clock_type v)
66 {
67     return v * milliseconds(1000);
68 }
69
70 prefix_ senf::ClockService::clock_type senf::ClockService::minutes(clock_type v)
71 {
72     return v * seconds(60);
73 }
74
75 prefix_ senf::ClockService::clock_type senf::ClockService::hours(clock_type v)
76 {
77     return v * minutes(60);
78 }
79
80 prefix_ senf::ClockService::clock_type senf::ClockService::days(clock_type v)
81 {
82     return v * hours(24);
83 }
84
85 prefix_ void senf::ClockService::restart()
86 {
87     instance().restart_m();
88 }
89
90 ////////////////////////////////////////
91 // private members
92
93 prefix_ senf::ClockService::clock_type senf::ClockService::now_m()
94 {
95     // We want to make the normal case (no skew) really fast. This first 'checkSkew' *might*
96     // transiently fail if a SIGALRM is delivered in the midst of the test. updateSkew will
97     // therefore block signals and do the check again to make sure.
98     //
99     // The opposite case (the test returns 'false' even though it should return 'true') is so highly
100     // improbable that it is treated as academic. (it will be catched by the next SIGALRM)
101
102     boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
103     if (checkSkew(time)) 
104         updateSkew(time);
105     
106     // 'clock' will pick up the corrected base_ value if needed.
107     return clock_m(time);
108 }
109
110 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock)
111 {
112 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
113     return base_ + boost::posix_time::nanoseconds(clock);
114 #else
115     return base_ + boost::posix_time::microseconds((clock+500)/1000);
116 #endif
117 }
118
119 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
120 {
121     ///\fixme What happens, if base_ is changed in SIGALRM while reading it here ?
122
123     // Idea: Have *two* base values: one is written by the SIGALRM handler, the other is only
124     // Written by synchronous code. If they differ, we block signals, copy over and continue.  If
125     // they transiently differ because we are reading the SIGALRM value while it is being changed
126     // this does not matter: We will then still copy it over.
127
128     boost::posix_time::time_duration delta (time - base_);
129     return clock_type( delta.ticks() )
130         * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
131 }
132
133 prefix_ bool senf::ClockService::checkSkew(boost::posix_time::ptime time)
134 {
135     boost::posix_time::ptime h (heartbeat_); // reduce chance for race condition
136     return time < h || (time - h) > boost::posix_time::seconds(2*CheckInterval);
137 }
138
139 prefix_ void senf::ClockService::clockSkew(boost::posix_time::ptime time,
140                                            boost::posix_time::ptime expected)
141 {
142     base_ += (time - expected);
143 }
144
145 ///////////////////////////////cci.e///////////////////////////////////////
146 #undef prefix_
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // comment-column: 40
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // End: