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 TimerSource non-inline non-template implementation */
26 #include "TimerSource.hh"
27 //#include "TimerSource.ih"
32 #include <sys/timerfd.h>
35 //#include "TimerSource.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::scheduler::detail::TimerSource
42 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
45 ///////////////////////////////////////////////////////////////////////////
46 // senf::scheduler::detail::POSIXTimerSource
48 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
49 : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
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);
56 sigemptyset(&sigSet_);
57 sigaddset(&sigSet_, SIGALRM);
58 sigprocmask(SIG_BLOCK, &sigSet_, 0);
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()");
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()");
76 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
78 timer_delete(timerId_);
79 ::signal(SIGALRM, SIG_IGN);
80 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
81 senf::scheduler::detail::FdManager::instance().remove(timerPipe_[0]);
87 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
89 if (! timeoutEnabled_ || timeout_ != timeout) {
93 timeoutEnabled_ = true;
98 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
100 if (timeoutEnabled_) {
101 timeoutEnabled_ = false;
106 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
108 if (! signalEnabled_) {
109 signalEnabled_ = true;
110 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
114 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
116 if (signalEnabled_) {
117 signalEnabled_ = false;
118 sigprocmask(SIG_BLOCK, &sigSet_, 0);
122 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
123 ::siginfo_t * siginfo,
126 if (siginfo->si_value.sival_ptr == 0)
128 static char data = '\xD0';
129 write(static_cast<POSIXTimerSource*>(siginfo->si_value.sival_ptr)->timerPipe_[1],
130 &data, sizeof(data));
133 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
136 read(timerPipe_[0], &data, sizeof(data));
137 timeoutEnabled_ = false;
140 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
142 struct itimerspec timer;
143 memset(&timer, 0, sizeof(timer));
144 if (timeoutEnabled_) {
145 timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
146 timer.it_value.tv_nsec = ClockService::in_nanoseconds(
147 timeout_ - ClockService::seconds(timer.it_value.tv_sec));
149 if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
150 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
153 ///////////////////////////////////////////////////////////////////////////
154 // senf::scheduler::detail::PollTimerSource
156 prefix_ void senf::scheduler::detail::PollTimerSource::timeout(ClockService::clock_type timeout)
158 ClockService::clock_type now (ClockService::now());
159 int delay (ClockService::in_milliseconds(timeout-now)+1);
160 FileDispatcher::instance().timeout(delay<0?0:delay);
163 prefix_ void senf::scheduler::detail::PollTimerSource::notimeout()
165 FileDispatcher::instance().timeout(-1);
168 prefix_ void senf::scheduler::detail::PollTimerSource::enable()
171 prefix_ void senf::scheduler::detail::PollTimerSource::disable()
174 ///////////////////////////////////////////////////////////////////////////
175 // senf::scheduler::detail::TimerFDTimerSource
178 prefix_ senf::scheduler::detail::TimerFDTimerSource::TimerFDTimerSource()
179 : timerfd_ (-1), timeoutEnabled_ (false), timeout_ (0)
181 timerfd_ = timerfd_create(CLOCK_MONOTONIC, 0);
183 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
184 senf::scheduler::detail::FdManager::instance().set(
185 timerfd_, detail::FdManager::EV_READ, this);
188 prefix_ senf::scheduler::detail::TimerFDTimerSource::~TimerFDTimerSource()
190 senf::scheduler::detail::FdManager::instance().remove(timerfd_);
195 senf::scheduler::detail::TimerFDTimerSource::timeout(ClockService::clock_type timeout)
197 if (!timeoutEnabled_ || timeout_ != timeout) {
201 timeoutEnabled_ = true;
206 prefix_ void senf::scheduler::detail::TimerFDTimerSource::notimeout()
208 if (timeoutEnabled_) {
209 timeoutEnabled_ = false;
214 prefix_ void senf::scheduler::detail::TimerFDTimerSource::enable()
217 prefix_ void senf::scheduler::detail::TimerFDTimerSource::disable()
228 TimerFdCheck::TimerFdCheck()
231 int fd (timerfd_create(CLOCK_MONOTONIC, 0));
234 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
243 prefix_ bool senf::scheduler::detail::TimerFDTimerSource::haveTimerFD()
245 static TimerFdCheck check;
246 return check.timerFdOk;
249 prefix_ void senf::scheduler::detail::TimerFDTimerSource::signal(int events)
251 uint64_t expirations (0);
252 read(timerfd_, &expirations, sizeof(expirations));
255 prefix_ void senf::scheduler::detail::TimerFDTimerSource::reschedule()
257 struct itimerspec timer;
258 memset(&timer, 0, sizeof(timer));
259 if (timeoutEnabled_) {
260 timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
261 timer.it_value.tv_nsec = ClockService::in_nanoseconds(
262 timeout_ - ClockService::seconds(timer.it_value.tv_sec));
264 if (timerfd_settime(timerfd_, TIMER_ABSTIME, &timer, 0)<0)
265 SENF_THROW_SYSTEM_EXCEPTION("timerfd_settime()");
269 ///////////////////////////////cc.e////////////////////////////////////////
271 //#include "TimerSource.mpp"
277 // comment-column: 40
278 // c-file-style: "senf"
279 // indent-tabs-mode: nil
280 // ispell-local-dictionary: "american"
281 // compile-command: "scons -u test"