Console: Implement short-option and non-option parsing
[senf.git] / Scheduler / ClockService.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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 ////////////////////////////////////////
36 // private members
37
38 prefix_ bool senf::ClockService::checkSkew(boost::posix_time::ptime time)
39 {
40     boost::posix_time::ptime h (heartbeat_); // reduce chance for race condition
41     return time < h || (time - h) > boost::posix_time::seconds(2*CheckInterval);
42 }
43
44 prefix_ void senf::ClockService::clockSkew(boost::posix_time::ptime time,
45                                            boost::posix_time::ptime expected)
46 {
47     base_ += (time - expected);
48 }
49
50 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
51 {
52     ///\fixme What happens, if base_ is changed in SIGALRM while reading it here ?
53
54     // Idea: Have *two* base values: one is written by the SIGALRM handler, the other is only
55     // Written by synchronous code. If they differ, we block signals, copy over and continue.  If
56     // they transiently differ because we are reading the SIGALRM value while it is being changed
57     // this does not matter: We will then still copy it over.
58
59     boost::posix_time::time_duration delta (time - base_);
60     return clock_type( delta.ticks() )
61         * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
62 }
63
64 prefix_ senf::ClockService::clock_type senf::ClockService::now_m()
65 {
66     // We want to make the normal case (no skew) really fast. This first 'checkSkew' *might*
67     // transiently fail if a SIGALRM is delivered in the midst of the test. updateSkew will
68     // therefore block signals and do the check again to make sure.
69     //
70     // The opposite case (the test returns 'false' even though it should return 'true') is so highly
71     // improbable that it is treated as academic. (it will be catched by the next SIGALRM)
72
73     boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
74     if (checkSkew(time)) 
75         updateSkew(time);
76     
77     // 'clock' will pick up the corrected base_ value if needed.
78     return clock_m(time);
79 }
80
81 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock)
82 {
83 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
84     return base_ + boost::posix_time::nanoseconds(clock);
85 #else
86     return base_ + boost::posix_time::microseconds((clock+500)/1000);
87 #endif
88 }
89
90 // public members
91
92 prefix_ senf::ClockService::clock_type senf::ClockService::now()
93 {
94     return instance().now_m();
95 }
96
97 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock)
98 {
99     return instance().abstime_m(clock);
100 }
101
102 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time)
103 {
104     return instance().clock_m(time);
105 }
106
107 prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time)
108 {
109     return clock( boost::posix_time::from_time_t(time) );
110 }
111
112 prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type v)
113 {
114     return v;
115 }
116
117 prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(int64_type v)
118 {
119     return v * nanoseconds(1000);
120 }
121
122 prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type v)
123 {
124     return v * microseconds(1000);
125 }
126
127 prefix_ senf::ClockService::clock_type senf::ClockService::seconds(int64_type v)
128 {
129     return v * milliseconds(1000);
130 }
131
132 prefix_ senf::ClockService::clock_type senf::ClockService::minutes(int64_type v)
133 {
134     return v * seconds(60);
135 }
136
137 prefix_ senf::ClockService::clock_type senf::ClockService::hours(int64_type v)
138 {
139     return v * minutes(60);
140 }
141
142 prefix_ senf::ClockService::clock_type senf::ClockService::days(int64_type v)
143 {
144     return v * hours(24);
145 }
146
147 prefix_ senf::ClockService::clock_type senf::ClockService::in_nanoseconds(int64_type v)
148 {
149     return v;
150 }
151
152 prefix_ senf::ClockService::clock_type senf::ClockService::in_microseconds(int64_type v)
153 {
154     return v / nanoseconds(1000);
155 }
156
157 prefix_ senf::ClockService::clock_type senf::ClockService::in_milliseconds(int64_type v)
158 {
159     return v / microseconds(1000);
160 }
161
162 prefix_ senf::ClockService::clock_type senf::ClockService::in_seconds(int64_type v)
163 {
164     return v / milliseconds(1000);
165 }
166
167 prefix_ senf::ClockService::clock_type senf::ClockService::in_minutes(int64_type v)
168 {
169     return v / seconds(60);
170 }
171
172 prefix_ senf::ClockService::clock_type senf::ClockService::in_hours(int64_type v)
173 {
174     return v / minutes(60);
175 }
176
177 prefix_ senf::ClockService::clock_type senf::ClockService::in_days(int64_type v)
178 {
179     return v / hours(24);
180 }
181
182 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
183 {
184     return from_time_t(time.tv_sec) + ClockService::microseconds(time.tv_usec);
185 }
186
187 prefix_ void senf::ClockService::restart()
188 {
189     instance().restart_m();
190 }
191
192 ///////////////////////////////cci.e///////////////////////////////////////
193 #undef prefix_
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
199 // comment-column: 40
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"
204 // End: