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 non-inline non-template implementation */
26 #include "ClockService.hh"
27 //#include "ClockService.ih"
34 #include "../Utils/Exception.hh"
36 //#include "ClockService.mpp"
38 ///////////////////////////////cc.p////////////////////////////////////////
40 #define CheckError(op,args) if (op args < 0) throwErrno(# op, errno)
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::ClockService::Impl
45 struct senf::ClockService::Impl
52 /// Internal: temporarily block signals (RAII idiom)
54 Blocker(Impl * i) : impl(i) { impl->block(); }
55 ~Blocker() { impl->unblock(); }
59 static void timer(int);
61 struct sigaction oldaction;
62 struct itimerval olditimer;
66 prefix_ senf::ClockService::Impl::Impl()
68 CheckError( sigemptyset, (&alrm_set) );
69 CheckError( sigaddset, (&alrm_set, SIGALRM) );
72 prefix_ void senf::ClockService::Impl::block()
74 CheckError( sigprocmask, (SIG_BLOCK, &alrm_set, 0) );
77 prefix_ void senf::ClockService::Impl::unblock()
79 CheckError( sigprocmask, (SIG_UNBLOCK, &alrm_set, 0) );
82 prefix_ void senf::ClockService::Impl::timer(int)
84 ClockService::instance().timer();
87 ///////////////////////////////////////////////////////////////////////////
90 prefix_ senf::ClockService::~ClockService()
92 setitimer(ITIMER_REAL, &impl_->olditimer, 0);
93 sigaction(SIGALRM, &impl_->oldaction, 0);
96 ////////////////////////////////////////
99 prefix_ senf::ClockService::ClockService()
100 : impl_(new ClockService::Impl())
105 prefix_ void senf::ClockService::timer()
107 boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
109 clockSkew(time, heartbeat_ + boost::posix_time::seconds(
110 ClockService::CheckInterval));
114 prefix_ void senf::ClockService::restart_m(bool restart)
117 // if any syscall fails, the alarm signal stays blocked which is correct
120 base_ = boost::posix_time::microsec_clock::universal_time();
123 struct sigaction action;
124 action.sa_handler = & senf::ClockService::Impl::timer;
125 CheckError( sigemptyset, (&action.sa_mask) );
126 action.sa_flags = SA_RESTART;
127 CheckError( sigaction, (SIGALRM, &action, restart ? 0 : &impl_->oldaction) );
129 restartTimer(restart);
134 prefix_ void senf::ClockService::restartTimer(bool restart)
136 struct itimerval itimer;
137 itimer.it_interval.tv_sec = CheckInterval;
138 itimer.it_interval.tv_usec = 0;
139 itimer.it_value.tv_sec = CheckInterval;
140 itimer.it_value.tv_usec = 0;
141 CheckError( setitimer, (ITIMER_REAL, &itimer, restart ? 0 : &impl_->olditimer) );
144 prefix_ void senf::ClockService::updateSkew(boost::posix_time::ptime time)
146 Impl::Blocker alrmBlocker (impl_.get());
148 // Make a second 'checkSkew' test, this time with SIGALRM blocked. See
149 // senf::ClockService::now_i()
151 if (checkSkew(time)) {
152 struct itimerval itimer;
153 CheckError( getitimer, (ITIMER_REAL, &itimer) );
154 clockSkew(time, (heartbeat_
155 + boost::posix_time::seconds(CheckInterval)
156 - boost::posix_time::seconds(itimer.it_value.tv_sec)
157 - boost::posix_time::microseconds(itimer.it_value.tv_usec)));
163 ///////////////////////////////cc.e////////////////////////////////////////
165 //#include "ClockService.mpp"
171 // comment-column: 40
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"