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"
31 //#include "TimerSource.mpp"
33 ///////////////////////////////cc.p////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::scheduler::detail::TimerSource
38 prefix_ senf::scheduler::detail::TimerSource::~TimerSource()
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::scheduler::detail::POSIXTimerSource
44 prefix_ senf::scheduler::detail::POSIXTimerSource::POSIXTimerSource()
45 : timeoutEnabled_ (false), timeout_ (0), signalEnabled_ (false)
47 if (pipe(timerPipe_) < 0)
48 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
49 senf::scheduler::detail::FdManager::instance().set(
50 timerPipe_[0], detail::FdManager::EV_READ, this);
52 sigemptyset(&sigSet_);
53 sigaddset(&sigSet_, SIGALRM);
54 sigprocmask(SIG_BLOCK, &sigSet_, 0);
57 act.sa_sigaction = &sigHandler;
58 act.sa_mask = sigSet_;
59 act.sa_flags = SA_SIGINFO | SA_RESTART;
60 if (sigaction(SIGALRM, &act, 0) < 0)
61 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
64 ::memset(&ev, 0, sizeof(ev));
65 ev.sigev_notify = SIGEV_SIGNAL;
66 ev.sigev_signo = SIGALRM;
67 ev.sigev_value.sival_ptr = this;
68 if (timer_create(CLOCK_MONOTONIC, &ev, &timerId_) < 0)
69 SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
72 prefix_ senf::scheduler::detail::POSIXTimerSource::~POSIXTimerSource()
74 timer_delete(timerId_);
75 ::signal(SIGALRM, SIG_IGN);
76 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
77 senf::scheduler::detail::FdManager::instance().remove(timerPipe_[0]);
83 senf::scheduler::detail::POSIXTimerSource::timeout(ClockService::clock_type timeout)
85 if (! timeoutEnabled_ || timeout_ != timeout) {
89 timeoutEnabled_ = true;
94 prefix_ void senf::scheduler::detail::POSIXTimerSource::notimeout()
96 if (timeoutEnabled_) {
97 timeoutEnabled_ = false;
102 prefix_ void senf::scheduler::detail::POSIXTimerSource::enable()
104 if (! signalEnabled_) {
105 signalEnabled_ = true;
106 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
110 prefix_ void senf::scheduler::detail::POSIXTimerSource::disable()
112 if (signalEnabled_) {
113 signalEnabled_ = false;
114 sigprocmask(SIG_BLOCK, &sigSet_, 0);
118 prefix_ void senf::scheduler::detail::POSIXTimerSource::sigHandler(int,
119 ::siginfo_t * siginfo,
122 if (siginfo->si_value.sival_ptr == 0)
124 static char data = '\xD0';
125 write(static_cast<POSIXTimerSource*>(siginfo->si_value.sival_ptr)->timerPipe_[1],
126 &data, sizeof(data));
129 prefix_ void senf::scheduler::detail::POSIXTimerSource::signal(int events)
132 read(timerPipe_[0], &data, sizeof(data));
133 timeoutEnabled_ = false;
136 prefix_ void senf::scheduler::detail::POSIXTimerSource::reschedule()
138 struct itimerspec timer;
139 memset(&timer, 0, sizeof(timer));
140 if (timeoutEnabled_) {
141 timer.it_value.tv_sec = ClockService::in_seconds(timeout_);
142 timer.it_value.tv_nsec = ClockService::in_nanoseconds(
143 timeout_ - ClockService::seconds(timer.it_value.tv_sec));
145 if (timer_settime(timerId_, TIMER_ABSTIME, &timer, 0)<0)
146 SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
149 ///////////////////////////////cc.e////////////////////////////////////////
151 //#include "TimerSource.mpp"
157 // comment-column: 40
158 // c-file-style: "senf"
159 // indent-tabs-mode: nil
160 // ispell-local-dictionary: "american"
161 // compile-command: "scons -u test"