Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Scheduler / TimerSource.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 TimerSource non-inline non-template implementation */
25
26 #include "TimerSource.hh"
27 //#include "TimerSource.ih"
28
29 // Custom includes
30 #include "IdleEvent.hh"
31 #ifdef HAVE_TIMERFD_CREATE
32 #include TIMERFD_H_PATH
33 #endif
34 #include "senf/Utils/IgnoreValue.hh"
35
36 //#include "TimerSource.mpp"
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::scheduler::detail::TimerSource
42
43 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
44 {}
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 // senf::scheduler::detail::POSIXTimerSource
48
49 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
50     : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
51 {
52     if (pipe(timerPipe_) < 0)
53         SENF_THROW_SYSTEM_EXCEPTION("pipe()");
54     senf::scheduler::detail::FdManager::instance().set(
55         timerPipe_[0], detail::FdManager::EV_READ, this);
56
57     sigemptyset(&sigSet_);
58     sigaddset(&sigSet_, SIGALRM);
59     sigprocmask(SIG_BLOCK, &sigSet_, 0);
60
61     struct sigaction act;
62     act.sa_sigaction = &sigHandler;
63     act.sa_mask = sigSet_;
64     act.sa_flags = SA_SIGINFO | SA_RESTART;
65     if (sigaction(SIGALRM, &act, 0) < 0)
66         SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
67
68     struct sigevent ev;
69     ::memset(&ev, 0, sizeof(ev));
70     ev.sigev_notify = SIGEV_SIGNAL;
71     ev.sigev_signo = SIGALRM;
72     ev.sigev_value.sival_ptr = this;
73     if (timer_create(CLOCK_MONOTONIC, &ev, &timerId_) < 0)
74         SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
75 }
76
77 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
78 {
79     timer_delete(timerId_);
80     ::signal(SIGALRM, SIG_IGN);
81     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
82     senf::scheduler::detail::FdManager::instance().remove(timerPipe_[0]);
83     close(timerPipe_[0]);
84     close(timerPipe_[1]);
85 }
86
87 prefix_ void
88 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
89 {
90     if (! timeoutEnabled_ || timeout_ != timeout) {
91         timeout_ = timeout;
92         if (timeout_ <= 0)
93             timeout_ = 1;
94         timeoutEnabled_ = true;
95         reschedule();
96     }
97 }
98
99 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
100 {
101     if (timeoutEnabled_) {
102         timeoutEnabled_ = false;
103         reschedule();
104     }
105 }
106
107 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
108 {
109     if (! signalEnabled_) {
110         signalEnabled_ = true;
111         sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
112     }
113 }
114
115 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
116 {
117     if (signalEnabled_) {
118         signalEnabled_ = false;
119         sigprocmask(SIG_BLOCK, &sigSet_, 0);
120     }
121 }
122
123 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
124                                                                    ::siginfo_t * siginfo,
125                                                                    void *)
126 {
127     if (siginfo->si_value.sival_ptr == 0)
128         return;
129     static char data = '\xD0';
130     // If the write fails there's not much we can do anyways ...
131     senf::IGNORE( write(static_cast<POSIXTimerSource*>(siginfo->si_value.sival_ptr)->timerPipe_[1],
132                         &data, sizeof(data)) );
133 }
134
135 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
136 {
137     char data;
138     // This should never fail since we are reading a single character from a signaled
139     // filedescriptor
140     senf::IGNORE( read(timerPipe_[0], &data, sizeof(data)) );
141     timeoutEnabled_ = false;
142 }
143
144 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
145 {
146     struct itimerspec timer;
147     memset(&timer, 0, sizeof(timer));
148     if (timeoutEnabled_) {
149         timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
150         timer.it_value.tv_nsec = ClockService::in_nanoseconds(
151             timeout_ - ClockService::seconds(timer.it_value.tv_sec));
152     }
153     if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
154         SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
155 }
156
157 //-/////////////////////////////////////////////////////////////////////////////////////////////////
158 // senf::scheduler::detail::PollTimerSource
159
160 prefix_ void senf::scheduler::detail::PollTimerSource::timeout(ClockService::clock_type timeout)
161 {
162     ClockService::clock_type now (ClockService::now());
163     int delay (ClockService::in_milliseconds(timeout-now)+1);
164     IdleEventDispatcher::instance().timeout(delay<0?0:delay);
165 }
166
167 prefix_ void senf::scheduler::detail::PollTimerSource::notimeout()
168 {
169     IdleEventDispatcher::instance().timeout(-1);
170 }
171
172 prefix_ void senf::scheduler::detail::PollTimerSource::enable()
173 {}
174
175 prefix_ void senf::scheduler::detail::PollTimerSource::disable()
176 {}
177
178 //-/////////////////////////////////////////////////////////////////////////////////////////////////
179 // senf::scheduler::detail::TimerFDTimerSource
180
181 #ifdef HAVE_TIMERFD_CREATE
182 prefix_ senf::scheduler::detail::TimerFDTimerSource::TimerFDTimerSource()
183     : timerfd_ (-1), timeoutEnabled_ (false), timeout_ (0)
184 {
185     timerfd_ = timerfd_create(CLOCK_MONOTONIC, 0);
186     if (timerfd_ < 0)
187         SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
188     senf::scheduler::detail::FdManager::instance().set(
189         timerfd_, detail::FdManager::EV_READ, this);
190 }
191
192 prefix_ senf::scheduler::detail::TimerFDTimerSource::~TimerFDTimerSource()
193 {
194     senf::scheduler::detail::FdManager::instance().remove(timerfd_);
195     close(timerfd_);
196 }
197
198 prefix_ void
199 senf::scheduler::detail::TimerFDTimerSource::timeout(ClockService::clock_type timeout)
200 {
201     if (!timeoutEnabled_ || timeout_ != timeout) {
202         timeout_ = timeout;
203         if (timeout_ <= 0)
204             timeout_ = 1;
205         timeoutEnabled_ = true;
206         reschedule();
207     }
208 }
209
210 prefix_ void senf::scheduler::detail::TimerFDTimerSource::notimeout()
211 {
212     if (timeoutEnabled_) {
213         timeoutEnabled_ = false;
214         reschedule();
215     }
216 }
217
218 prefix_ void senf::scheduler::detail::TimerFDTimerSource::enable()
219 {}
220
221 prefix_ void senf::scheduler::detail::TimerFDTimerSource::disable()
222 {}
223
224 namespace {
225
226     struct TimerFdCheck
227     {
228         TimerFdCheck();
229         bool timerFdOk;
230     };
231
232     TimerFdCheck::TimerFdCheck()
233         : timerFdOk (false)
234     {
235         int fd (timerfd_create(CLOCK_MONOTONIC, 0));
236         if (fd == -1) {
237             if (errno != EINVAL)
238                 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
239         }
240         else {
241             timerFdOk = true;
242             close(fd);
243         }
244     }
245
246 }
247 prefix_ bool senf::scheduler::detail::TimerFDTimerSource::haveTimerFD()
248 {
249     static TimerFdCheck check;
250     return check.timerFdOk;
251 }
252
253 prefix_ void senf::scheduler::detail::TimerFDTimerSource::signal(int events)
254 {
255     uint64_t expirations (0);
256     // We ignore the return value since we ignore the value read anyways
257     senf::IGNORE( read(timerfd_, &expirations, sizeof(expirations)) );
258 }
259
260 prefix_ void senf::scheduler::detail::TimerFDTimerSource::reschedule()
261 {
262     struct itimerspec timer;
263     memset(&timer, 0, sizeof(timer));
264     if (timeoutEnabled_) {
265         timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
266         timer.it_value.tv_nsec = ClockService::in_nanoseconds(
267             timeout_ - ClockService::seconds(timer.it_value.tv_sec));
268     }
269     if (timerfd_settime(timerfd_, TFD_TIMER_ABSTIME, &timer, 0)<0)
270         SENF_THROW_SYSTEM_EXCEPTION("timerfd_settime()");
271 }
272 #endif
273
274 //-/////////////////////////////////////////////////////////////////////////////////////////////////
275 #undef prefix_
276 //#include "TimerSource.mpp"
277
278
279 // Local Variables:
280 // mode: c++
281 // fill-column: 100
282 // comment-column: 40
283 // c-file-style: "senf"
284 // indent-tabs-mode: nil
285 // ispell-local-dictionary: "american"
286 // compile-command: "scons -u test"
287 // End: