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 "TimerSource.mpp"
34 ///////////////////////////////cc.p////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::scheduler::detail::TimerSource
39 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::scheduler::detail::POSIXTimerSource
45 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
46 : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
48 if (pipe(timerPipe_) < 0)
49 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
50 senf::scheduler::detail::FdManager::instance().set(
51 timerPipe_[0], detail::FdManager::EV_READ, this);
53 sigemptyset(&sigSet_);
54 sigaddset(&sigSet_, SIGALRM);
55 sigprocmask(SIG_BLOCK, &sigSet_, 0);
58 act.sa_sigaction = &sigHandler;
59 act.sa_mask = sigSet_;
60 act.sa_flags = SA_SIGINFO | SA_RESTART;
61 if (sigaction(SIGALRM, &act, 0) < 0)
62 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
65 ::memset(&ev, 0, sizeof(ev));
66 ev.sigev_notify = SIGEV_SIGNAL;
67 ev.sigev_signo = SIGALRM;
68 ev.sigev_value.sival_ptr = this;
69 if (timer_create(CLOCK_MONOTONIC, &ev, &timerId_) < 0)
70 SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
73 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
75 timer_delete(timerId_);
76 ::signal(SIGALRM, SIG_IGN);
77 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
78 senf::scheduler::detail::FdManager::instance().remove(timerPipe_[0]);
84 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
86 if (! timeoutEnabled_ || timeout_ != timeout) {
90 timeoutEnabled_ = true;
95 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
97 if (timeoutEnabled_) {
98 timeoutEnabled_ = false;
103 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
105 if (! signalEnabled_) {
106 signalEnabled_ = true;
107 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
111 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
113 if (signalEnabled_) {
114 signalEnabled_ = false;
115 sigprocmask(SIG_BLOCK, &sigSet_, 0);
119 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
120 ::siginfo_t * siginfo,
123 if (siginfo->si_value.sival_ptr == 0)
125 static char data = '\xD0';
126 write(static_cast<POSIXTimerSource*>(siginfo->si_value.sival_ptr)->timerPipe_[1],
127 &data, sizeof(data));
130 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
133 read(timerPipe_[0], &data, sizeof(data));
134 timeoutEnabled_ = false;
137 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
139 struct itimerspec timer;
140 memset(&timer, 0, sizeof(timer));
141 if (timeoutEnabled_) {
142 timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
143 timer.it_value.tv_nsec = ClockService::in_nanoseconds(
144 timeout_ - ClockService::seconds(timer.it_value.tv_sec));
146 if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
147 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
150 ///////////////////////////////////////////////////////////////////////////
151 // senf::scheduler::detail::PollTimerSource
153 prefix_ void senf::scheduler::detail::PollTimerSource::timeout(ClockService::clock_type timeout)
155 ClockService::clock_type now (ClockService::now());
156 int delay (ClockService::in_milliseconds(timeout-now)+1);
157 FileDispatcher::instance().timeout(delay<0?0:delay);
160 prefix_ void senf::scheduler::detail::PollTimerSource::notimeout()
162 FileDispatcher::instance().timeout(-1);
165 prefix_ void senf::scheduler::detail::PollTimerSource::enable()
168 prefix_ void senf::scheduler::detail::PollTimerSource::disable()
171 ///////////////////////////////cc.e////////////////////////////////////////
173 //#include "TimerSource.mpp"
179 // comment-column: 40
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"