4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief TimerSource non-inline non-template implementation */
31 #include "TimerSource.hh"
32 //#include "TimerSource.ih"
35 #include "IdleEvent.hh"
36 #ifdef HAVE_TIMERFD_CREATE
37 #include TIMERFD_H_PATH
39 #include "senf/Utils/IgnoreValue.hh"
41 //#include "TimerSource.mpp"
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 // senf::scheduler::detail::TimerSource
48 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::scheduler::detail::POSIXTimerSource
54 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
55 : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
57 if (pipe(timerPipe_) < 0)
58 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
59 FdManager::instance().set( timerPipe_[0], FdManager::EV_READ, this);
61 sigemptyset(&sigSet_);
62 sigaddset(&sigSet_, SIGALRM);
63 sigprocmask(SIG_BLOCK, &sigSet_, 0);
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()");
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()");
81 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
83 timer_delete(timerId_);
84 ::signal(SIGALRM, SIG_IGN);
85 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
86 FdManager::instance().remove(timerPipe_[0]);
92 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
94 if (! timeoutEnabled_ || timeout_ != timeout) {
98 timeoutEnabled_ = true;
103 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
105 if (timeoutEnabled_) {
106 timeoutEnabled_ = false;
111 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
113 if (! signalEnabled_) {
114 signalEnabled_ = true;
115 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
119 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
121 if (signalEnabled_) {
122 signalEnabled_ = false;
123 sigprocmask(SIG_BLOCK, &sigSet_, 0);
127 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
128 ::siginfo_t * siginfo,
131 if (siginfo->si_value.sival_ptr == 0)
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)) );
139 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
142 // This should never fail since we are reading a single character from a signaled
144 senf::IGNORE( read(timerPipe_[0], &data, sizeof(data)) );
145 timeoutEnabled_ = false;
148 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
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));
157 if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
158 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::scheduler::detail::PollTimerSource
164 prefix_ void senf::scheduler::detail::PollTimerSource::timeout(ClockService::clock_type timeout)
166 ClockService::clock_type now (ClockService::now());
167 int delay (ClockService::in_milliseconds(timeout-now)+1);
168 IdleEventDispatcher::instance().timeout(delay<0?0:delay);
171 prefix_ void senf::scheduler::detail::PollTimerSource::notimeout()
173 IdleEventDispatcher::instance().timeout(-1);
176 prefix_ void senf::scheduler::detail::PollTimerSource::enable()
179 prefix_ void senf::scheduler::detail::PollTimerSource::disable()
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 // senf::scheduler::detail::TimerFDTimerSource
185 #ifdef HAVE_TIMERFD_CREATE
186 prefix_ senf::scheduler::detail::TimerFDTimerSource::TimerFDTimerSource()
187 : timerfd_ (-1), timeoutEnabled_ (false), timeout_ (0)
189 timerfd_ = timerfd_create(CLOCK_MONOTONIC, 0);
191 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
192 FdManager::instance().set( timerfd_, FdManager::EV_READ, this);
195 prefix_ senf::scheduler::detail::TimerFDTimerSource::~TimerFDTimerSource()
197 FdManager::instance().remove(timerfd_);
202 senf::scheduler::detail::TimerFDTimerSource::timeout(ClockService::clock_type timeout)
204 if (!timeoutEnabled_ || timeout_ != timeout) {
208 timeoutEnabled_ = true;
213 prefix_ void senf::scheduler::detail::TimerFDTimerSource::notimeout()
215 if (timeoutEnabled_) {
216 timeoutEnabled_ = false;
221 prefix_ void senf::scheduler::detail::TimerFDTimerSource::enable()
224 prefix_ void senf::scheduler::detail::TimerFDTimerSource::disable()
235 TimerFdCheck::TimerFdCheck()
238 int fd (timerfd_create(CLOCK_MONOTONIC, 0));
241 SENF_THROW_SYSTEM_EXCEPTION("timerfd_create()");
250 prefix_ bool senf::scheduler::detail::TimerFDTimerSource::haveTimerFD()
252 static TimerFdCheck check;
253 return check.timerFdOk;
256 prefix_ void senf::scheduler::detail::TimerFDTimerSource::signal(int events)
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)) );
263 prefix_ void senf::scheduler::detail::TimerFDTimerSource::reschedule()
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));
272 if (timerfd_settime(timerfd_, TFD_TIMER_ABSTIME, &timer, 0)<0)
273 SENF_THROW_SYSTEM_EXCEPTION("timerfd_settime()");
277 //-/////////////////////////////////////////////////////////////////////////////////////////////////
279 //#include "TimerSource.mpp"
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"