Utils: Implement more flexible SystemException
[senf.git] / Scheduler / ClockService.cc
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 non-inline non-template implementation */
25
26 #include "ClockService.hh"
27 //#include "ClockService.ih"
28
29 // Custom includes
30 #include <errno.h>
31 #include <signal.h>
32 #include <sys/time.h>
33 #include <pthread.h>
34 #include "../Utils/Exception.hh"
35
36 //#include "ClockService.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 #define CheckError(op,args) if (op args < 0) throwErrno(# op, errno)
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::ClockService::Impl
44
45 struct senf::ClockService::Impl 
46 {
47     Impl();
48
49     void block();
50     void unblock();
51
52     /// Internal: temporarily block signals (RAII idiom)
53     struct Blocker {
54         Blocker(Impl * i) : impl(i) { impl->block(); }
55         ~Blocker() { impl->unblock(); }
56         Impl * impl;
57     };
58
59     static void timer(int);
60
61     struct sigaction oldaction;
62     struct itimerval olditimer;
63     sigset_t alrm_set;
64 };
65
66 prefix_ senf::ClockService::Impl::Impl()
67 {
68     CheckError( sigemptyset, (&alrm_set) );
69     CheckError( sigaddset, (&alrm_set, SIGALRM) );
70 }
71
72 prefix_ void senf::ClockService::Impl::block()
73 {
74     CheckError( sigprocmask, (SIG_BLOCK, &alrm_set, 0) );
75 }
76
77 prefix_ void senf::ClockService::Impl::unblock()
78 {
79     CheckError( sigprocmask, (SIG_UNBLOCK, &alrm_set, 0) );
80 }
81
82 prefix_ void senf::ClockService::Impl::timer(int)
83 {
84     ClockService::instance().timer();
85 }
86
87 ///////////////////////////////////////////////////////////////////////////
88 // senf::ClockService
89
90 prefix_ senf::ClockService::~ClockService()
91 {
92     setitimer(ITIMER_REAL, &impl_->olditimer, 0);
93     sigaction(SIGALRM, &impl_->oldaction, 0);
94 }
95
96 ////////////////////////////////////////
97 // private members
98
99 prefix_ senf::ClockService::ClockService()
100     : impl_(new ClockService::Impl())
101 {
102     restart_m(false);
103 }
104
105 prefix_ void senf::ClockService::timer()
106 {
107     boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
108     if (checkSkew(time))
109         clockSkew(time, heartbeat_ + boost::posix_time::seconds(
110                       ClockService::CheckInterval));
111     heartbeat_ = time;
112 }
113
114 prefix_ void senf::ClockService::restart_m(bool restart)
115 {
116     if (restart)
117         // if any syscall fails, the alarm signal stays blocked which is correct
118         impl_->block(); 
119
120     base_ = boost::posix_time::microsec_clock::universal_time();
121     heartbeat_ = base_;
122
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) );
128
129     restartTimer(restart);
130     
131     impl_->unblock();
132 }
133
134 prefix_ void senf::ClockService::restartTimer(bool restart)
135 {
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) );
142 }
143
144 prefix_ void senf::ClockService::updateSkew(boost::posix_time::ptime time)
145 {
146     Impl::Blocker alrmBlocker (impl_.get());
147     
148     // Make a second 'checkSkew' test, this time with SIGALRM blocked. See
149     // senf::ClockService::now_i()
150
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)));
158         heartbeat_ = time;
159         restartTimer();
160     }
161 }
162
163 ///////////////////////////////cc.e////////////////////////////////////////
164 #undef prefix_
165 //#include "ClockService.mpp"
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
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"
176 // End: