4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 Scheduler non-inline non-template implementation
26 \idea Implement signal handling (See source for more discussion
29 \idea Multithreading support: To support multithreading, the
30 static member Scheduler::instance() must return a thread-local
31 value (that is Scheduler::instance() must allocate one Scheduler
32 instance per thread). Another possibility would be to distribute
33 the async load unto several threads (one scheduler for multiple
37 // Here a basic concept of how to add signal support to the scheduler:
39 // Every signal to be reported by the scheduler will be asigned a
40 // generic signal handler by the scheduler. This signal handler will
41 // use longjmp (juck) to report this signal back to the scheduler
44 // To make this safe, the main-loop will look something like:
46 // int signal = setjmp(jmpBuffer_);
48 // // unblock all signals which are registered with the
51 // // block all relevant signals again
54 // // now handle the event
56 // The signal handler is then simply defined as
58 // static void Scheduler::sigHandler(int signal)
60 // // make sure to restore the signal handler here if
62 // longjmp(Scheduler::instance().jmpBuffer_,signal);
65 // You should use sigaction to register the signal handlers and define
66 // a sa_mask so all Scheduler-registered signals are automatically
67 // *blocked* whenever one of the signals is called (including the
68 // called signal!) (This also means, we will have to re-register all
69 // signals if we change the registration of some signal since the
70 // sa_mask changes). This ensures, that no two signals can be
71 // delivered on top of each other. And of course any signal registered
72 // with the scheduler must be blocked as soon as it is registered with
75 #include "Scheduler.hh"
76 //#include "Scheduler.ih"
80 #include <sys/epoll.h>
81 #include "Utils/Exception.hh"
83 static const int EPollInitialSize = 16;
86 ///////////////////////////////cc.p////////////////////////////////////////
88 prefix_ senf::Scheduler::Scheduler()
89 : timerIdCounter_(0), epollFd_ (epoll_create(EPollInitialSize)), terminate_(false),
93 throw SystemException(errno);
96 prefix_ void senf::Scheduler::do_add(int fd, SimpleCallback const & cb, int eventMask)
98 FdTable::iterator i (fdTable_.find(fd));
99 int action (EPOLL_CTL_MOD);
100 if (i == fdTable_.end()) {
101 action = EPOLL_CTL_ADD;
102 i = fdTable_.insert(std::make_pair(fd, EventSpec())).first;
105 if (eventMask & EV_READ) i->second.cb_read = cb;
106 if (eventMask & EV_PRIO) i->second.cb_prio = cb;
107 if (eventMask & EV_WRITE) i->second.cb_write = cb;
110 memset(&ev,0,sizeof(ev));
111 ev.events = i->second.epollMask();
114 if (epoll_ctl(epollFd_, action, fd, &ev)<0)
115 throw SystemException(errno);
118 prefix_ void senf::Scheduler::do_remove(int fd, int eventMask)
120 FdTable::iterator i (fdTable_.find(fd));
121 if (i == fdTable_.end())
124 if (eventMask & EV_READ) i->second.cb_read = 0;
125 if (eventMask & EV_PRIO) i->second.cb_prio = 0;
126 if (eventMask & EV_WRITE) i->second.cb_write = 0;
129 memset(&ev,0,sizeof(ev));
130 ev.events = i->second.epollMask();
133 int action (EPOLL_CTL_MOD);
135 action = EPOLL_CTL_DEL;
139 if (epoll_ctl(epollFd_, action, fd, &ev)<0)
140 throw SystemException(errno);
144 prefix_ int senf::Scheduler::EventSpec::epollMask()
148 if (cb_read) mask |= EPOLLIN;
149 if (cb_prio) mask |= EPOLLPRI;
150 if (cb_write) mask |= EPOLLOUT;
154 prefix_ void senf::Scheduler::process()
157 eventTime_ = ClockService::now();
158 while (! terminate_) {
159 while ( ! timerQueue_.empty() && timerQueue_.top()->second.timeout <= eventTime_ ) {
160 TimerMap::iterator i (timerQueue_.top());
161 if (! i->second.canceled)
170 int timeout (MinTimeout);
171 if (! timerQueue_.empty()) {
172 ClockService::clock_type delta (
173 (timerQueue_.top()->second.timeout - eventTime_)/1000000UL);
174 if (delta<MinTimeout)
175 timeout = int(delta);
178 struct epoll_event ev;
179 int events = epoll_wait(epollFd_, &ev, 1, timeout);
181 // 'man epoll' says, epoll will not return with EINTR.
182 throw SystemException(errno);
184 /// \fixme Fix unneeded timer delays
185 // Hmm ... I remember, I purposely moved the timeout-handlers to the loop top ... but why?
186 // This delays possible time-critical handlers even further ...
188 eventTime_ = ClockService::now();
190 // Timeout .. the handler will be run when going back to the loop top
193 FdTable::iterator i = fdTable_.find(ev.data.fd);
194 BOOST_ASSERT (i != fdTable_.end() );
195 EventSpec spec (i->second);
197 unsigned extraFlags (0);
198 if (ev.events & EPOLLHUP) extraFlags |= EV_HUP;
199 if (ev.events & EPOLLERR) extraFlags |= EV_ERR;
201 if (ev.events & EPOLLIN) {
202 BOOST_ASSERT(spec.cb_read);
203 spec.cb_read(EventId(EV_READ | extraFlags));
205 else if (ev.events & EPOLLPRI) {
206 BOOST_ASSERT(spec.cb_prio);
207 spec.cb_prio(EventId(EV_PRIO | extraFlags));
209 else if (ev.events & EPOLLOUT) {
210 BOOST_ASSERT(spec.cb_write);
211 spec.cb_write(EventId(EV_WRITE | extraFlags));
214 // This branch is only taken, if HUP or ERR is signaled but none of IN/OUT/PRI.
215 // In this case we will signal all registered callbacks. The callbacks must be
216 // prepared to be called multiple times if they are registered to more than
219 spec.cb_write(EventId(extraFlags));
221 spec.cb_prio(EventId(extraFlags));
223 spec.cb_read(EventId(extraFlags));
228 ///////////////////////////////cc.e////////////////////////////////////////
235 // c-file-style: "senf"
236 // indent-tabs-mode: nil
237 // ispell-local-dictionary: "american"
238 // compile-command: "scons -u test"
239 // comment-column: 40