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