switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / TimerSource.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief TimerSource non-inline non-template implementation */
30
31 #include "TimerSource.hh"
32 //#include "TimerSource.ih"
33
34 // Custom includes
35 #include "IdleEvent.hh"
36 #ifdef HAVE_TIMERFD_CREATE
37 #include TIMERFD_H_PATH
38 #endif
39 #include "senf/Utils/IgnoreValue.hh"
40
41 //#include "TimerSource.mpp"
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 // senf::scheduler::detail::TimerSource
47
48 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
49 {}
50
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::scheduler::detail::POSIXTimerSource
53
54 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
55     : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
56 {
57     if (pipe(timerPipe_) < 0)
58         SENF_THROW_SYSTEM_EXCEPTION("pipe()");
59     FdManager::instance().set( timerPipe_[0], FdManager::EV_READ, this);
60
61     sigemptyset(&sigSet_);
62     sigaddset(&sigSet_, SIGALRM);
63     sigprocmask(SIG_BLOCK, &sigSet_, 0);
64
65     struct sigaction act;
66     act.sa_sigaction = &sigHandler;
67     act.sa_mask = sigSet_;
68     act.sa_flags = SA_SIGINFO | SA_RESTART;
69     if (sigaction(SIGALRM, &act, 0) < 0)
70         SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
71
72     struct sigevent ev;
73     ::memset(&ev, 0, sizeof(ev));
74     ev.sigev_notify = SIGEV_SIGNAL;
75     ev.sigev_signo = SIGALRM;
76     ev.sigev_value.sival_ptr = this;
77     if (timer_create(CLOCK_MONOTONIC, &ev, &timerId_) < 0)
78         SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
79 }
80
81 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
82 {
83     timer_delete(timerId_);
84     ::signal(SIGALRM, SIG_IGN);
85     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
86     FdManager::instance().remove(timerPipe_[0]);
87     close(timerPipe_[0]);
88     close(timerPipe_[1]);
89 }
90
91 prefix_ void
92 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
93 {
94     if (! timeoutEnabled_ || timeout_ != timeout) {
95         timeout_ = timeout;
96         if (timeout_ <= 0)
97             timeout_ = 1;
98         timeoutEnabled_ = true;
99         reschedule();
100     }
101 }
102
103 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
104 {
105     if (timeoutEnabled_) {
106         timeoutEnabled_ = false;
107         reschedule();
108     }
109 }
110
111 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
112 {
113     if (! signalEnabled_) {
114         signalEnabled_ = true;
115         sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
116     }
117 }
118
119 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
120 {
121     if (signalEnabled_) {
122         signalEnabled_ = false;
123         sigprocmask(SIG_BLOCK, &sigSet_, 0);
124     }
125 }
126
127 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
128                                                                    ::siginfo_t * siginfo,
129                                                                    void *)
130 {
131     if (siginfo->si_value.sival_ptr == 0)
132         return;
133     static char data = '\xD0';
134     // If the write fails there's not much we can do anyways ...
135     senf::IGNORE( write(static_cast<POSIXTimerSource*>(siginfo->si_value.sival_ptr)->timerPipe_[1],
136                         &data, sizeof(data)) );
137 }
138
139 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
140 {
141     char data;
142     // This should never fail since we are reading a single character from a signaled
143     // filedescriptor
144     senf::IGNORE( read(timerPipe_[0], &data, sizeof(data)) );
145     timeoutEnabled_ = false;
146 }
147
148 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
149 {
150     struct itimerspec timer;
151     memset(&timer, 0, sizeof(timer));
152     if (timeoutEnabled_) {
153         timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
154         timer.it_value.tv_nsec = ClockService::in_nanoseconds(
155             timeout_ - ClockService::seconds(timer.it_value.tv_sec));
156     }
157     if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
158         SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
159 }
160
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::scheduler::detail::PollTimerSource
163
164 prefix_ void senf::scheduler::detail::PollTimerSource::timeout(ClockService::clock_type timeout)
165 {
166     ClockService::clock_type now (ClockService::now());
167     int delay (ClockService::in_milliseconds(timeout-now)+1);
168     IdleEventDispatcher::instance().timeout(delay<0?0:delay);
169 }
170
171 prefix_ void senf::scheduler::detail::PollTimerSource::notimeout()
172 {
173     IdleEventDispatcher::instance().timeout(-1);
174 }
175
176 prefix_ void senf::scheduler::detail::PollTimerSource::enable()
177 {}
178
179 prefix_ void senf::scheduler::detail::PollTimerSource::disable()
180 {}
181
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 // senf::scheduler::detail::TimerFDTimerSource
184
185 #ifdef HAVE_TIMERFD_CREATE
186 prefix_ senf::scheduler::detail::TimerFDTimerSource::TimerFDTimerSource()
187     : timerfd_ (-1), timeoutEnabled_ (false), timeout_ (0)
188 {
189     timerfd_ = timerfd_create(CLOCK_MONOTONIC, 0);
190     if (timerfd_ < 0)
191         SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
192     FdManager::instance().set( timerfd_, FdManager::EV_READ, this);
193 }
194
195 prefix_ senf::scheduler::detail::TimerFDTimerSource::~TimerFDTimerSource()
196 {
197     FdManager::instance().remove(timerfd_);
198     close(timerfd_);
199 }
200
201 prefix_ void
202 senf::scheduler::detail::TimerFDTimerSource::timeout(ClockService::clock_type timeout)
203 {
204     if (!timeoutEnabled_ || timeout_ != timeout) {
205         timeout_ = timeout;
206         if (timeout_ <= 0)
207             timeout_ = 1;
208         timeoutEnabled_ = true;
209         reschedule();
210     }
211 }
212
213 prefix_ void senf::scheduler::detail::TimerFDTimerSource::notimeout()
214 {
215     if (timeoutEnabled_) {
216         timeoutEnabled_ = false;
217         reschedule();
218     }
219 }
220
221 prefix_ void senf::scheduler::detail::TimerFDTimerSource::enable()
222 {}
223
224 prefix_ void senf::scheduler::detail::TimerFDTimerSource::disable()
225 {}
226
227 namespace {
228
229     struct TimerFdCheck
230     {
231         TimerFdCheck();
232         bool timerFdOk;
233     };
234
235     TimerFdCheck::TimerFdCheck()
236         : timerFdOk (false)
237     {
238         int fd (timerfd_create(CLOCK_MONOTONIC, 0));
239         if (fd == -1) {
240             if (errno != EINVAL)
241                 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
242         }
243         else {
244             timerFdOk = true;
245             close(fd);
246         }
247     }
248
249 }
250 prefix_ bool senf::scheduler::detail::TimerFDTimerSource::haveTimerFD()
251 {
252     static TimerFdCheck check;
253     return check.timerFdOk;
254 }
255
256 prefix_ void senf::scheduler::detail::TimerFDTimerSource::signal(int events)
257 {
258     uint64_t expirations (0);
259     // We ignore the return value since we ignore the value read anyways
260     senf::IGNORE( read(timerfd_, &expirations, sizeof(expirations)) );
261 }
262
263 prefix_ void senf::scheduler::detail::TimerFDTimerSource::reschedule()
264 {
265     struct itimerspec timer;
266     memset(&timer, 0, sizeof(timer));
267     if (timeoutEnabled_) {
268         timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
269         timer.it_value.tv_nsec = ClockService::in_nanoseconds(
270             timeout_ - ClockService::seconds(timer.it_value.tv_sec));
271     }
272     if (timerfd_settime(timerfd_, TFD_TIMER_ABSTIME, &timer, 0)<0)
273         SENF_THROW_SYSTEM_EXCEPTION("timerfd_settime()");
274 }
275 #endif
276
277 //-/////////////////////////////////////////////////////////////////////////////////////////////////
278 #undef prefix_
279 //#include "TimerSource.mpp"
280
281
282 // Local Variables:
283 // mode: c++
284 // fill-column: 100
285 // comment-column: 40
286 // c-file-style: "senf"
287 // indent-tabs-mode: nil
288 // ispell-local-dictionary: "american"
289 // compile-command: "scons -u test"
290 // End: