Renamed namespaces satcom::lib and satcom::pkf to senf
[senf.git] / Scheduler / Scheduler.cc
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 // TODO: Implement signal handling
24 // Here a basic concept of how to add signal support to the scheduler:
25 //
26 // Every signal to be reported by the scheduler will be asigned a
27 // generic signal handler by the scheduler. This signal handler will
28 // use longjmp (juck) to report this signal back to the scheduler
29 // main-loop.
30 //
31 // To make this safe, the main-loop will look something like:
32 //
33 //     int signal = setjmp(jmpBuffer_);
34 //     if (signal == 0) {
35 //         // unblock all signals which are registered with the
36 //         // scheduler
37 //         // call epoll
38 //         // block all relevant signals again
39 //     }
40 //   
41 //     // now handle the event
42 //
43 // The signal handler is then simply defined as
44 //
45 //     static void Scheduler::sigHandler(int signal)
46 //     {
47 //         // make sure to restore the signal handler here if
48 //         // necessary
49 //         longjmp(Scheduler::instance().jmpBuffer_,signal);
50 //     }
51 //
52 // You should use sigaction to register the signal handlers and define
53 // a sa_mask so all Scheduler-registered signals are automatically
54 // *blocked* whenever one of the signals is called (including the
55 // called signal!). This ensures, that no two signals can be delivered
56 // on top of each other. And of course any signal registered with the
57 // scheduler must be blocked as soon as it is registered with the
58 // scheduler.
59
60 // TODO: Multithreading support
61 // To support multithreading, the static member Scheduler::instance()
62 // must return a thread-local value (that is Scheduler::instance()
63 // must allocate one Scheduler instance per thread)
64
65 // Definition of non-inline non-template functions
66
67 #include "Scheduler.hh"
68 //#include "Scheduler.ih"
69
70 // Custom includes
71 #include <errno.h>
72 #include <sys/epoll.h>
73 #include "Utils/Exception.hh"
74 #include "Utils/MicroTime.hh"
75
76 static const int EPollInitialSize = 16;
77
78 #define prefix_
79 ///////////////////////////////cc.p////////////////////////////////////////
80
81 prefix_ senf::Scheduler::Scheduler & senf::Scheduler::instance()
82 {
83     static Scheduler instance;
84     return instance;
85 }
86
87 prefix_ void senf::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
88 {
89     timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
90 }
91
92 prefix_ senf::Scheduler::Scheduler()
93     : epollFd_(epoll_create(EPollInitialSize))
94 {
95     if (epollFd_<0)
96         throw SystemException(errno);
97 }
98
99 prefix_ void senf::Scheduler::do_add(int fd, SimpleCallback const & cb, int eventMask)
100 {
101     FdTable::iterator i (fdTable_.find(fd));
102     int action (EPOLL_CTL_MOD);
103     if (i == fdTable_.end()) {
104         action = EPOLL_CTL_ADD;
105         i = fdTable_.insert(std::make_pair(fd, EventSpec())).first;
106     }
107
108     if (eventMask & EV_READ)  i->second.cb_read = cb;
109     if (eventMask & EV_PRIO)  i->second.cb_prio = cb;
110     if (eventMask & EV_WRITE) i->second.cb_write = cb;
111     if (eventMask & EV_HUP)   i->second.cb_hup = cb;
112     if (eventMask & EV_ERR)   i->second.cb_err = cb;
113
114     epoll_event ev;
115     memset(&ev,0,sizeof(ev));
116     ev.events = i->second.epollMask();
117     ev.data.fd = fd;
118     
119     if (epoll_ctl(epollFd_, action, fd, &ev)<0)
120         throw SystemException(errno);
121 }
122
123 prefix_ void senf::Scheduler::do_remove(int fd, int eventMask)
124 {
125     FdTable::iterator i (fdTable_.find(fd));
126     if (i == fdTable_.end()) 
127         return;
128
129     if (eventMask & EV_READ)  i->second.cb_read = 0;
130     if (eventMask & EV_PRIO)  i->second.cb_prio = 0;
131     if (eventMask & EV_WRITE) i->second.cb_write = 0;
132     if (eventMask & EV_HUP)   i->second.cb_hup = 0;
133     if (eventMask & EV_ERR)   i->second.cb_err = 0;
134
135     epoll_event ev;
136     memset(&ev,0,sizeof(ev));
137     ev.events = i->second.epollMask();
138     ev.data.fd = fd;
139     
140     int action (EPOLL_CTL_MOD);
141     if (ev.events==0) {
142         action = EPOLL_CTL_DEL;
143         fdTable_.erase(i);
144     }
145
146     if (epoll_ctl(epollFd_, action, fd, &ev)<0)
147         throw SystemException(errno);
148 }
149
150
151 prefix_ int senf::Scheduler::EventSpec::epollMask()
152     const
153 {
154     int mask (0);
155     if (cb_read)  mask |= EPOLLIN;
156     if (cb_prio)  mask |= EPOLLPRI;
157     if (cb_write) mask |= EPOLLOUT;
158     if (cb_hup)   mask |= EPOLLHUP;
159     if (cb_err)   mask |= EPOLLERR;
160     return mask;
161 }
162
163 prefix_ void senf::Scheduler::process()
164 {
165     terminate_ = false;
166     while (! terminate_) {
167
168         MicroTime timeNow = now();
169         while ( ! timerQueue_.empty() && timerQueue_.top().timeout <= timeNow ) {
170             timerQueue_.top().cb();
171             timerQueue_.pop();
172         }
173         if (terminate_) 
174             return;
175         int timeout = timerQueue_.empty() ? -1 : int((timerQueue_.top().timeout - timeNow)/1000);
176             
177         struct epoll_event ev;
178         int events = epoll_wait(epollFd_, &ev, 1, timeout);
179         if (events<0)
180             // Hmm ... man epoll says, it will NOT return with EINTR ??
181             throw SystemException(errno);
182         if (events==0)
183             // Timeout .. it will be run when reachiung the top of the loop
184             continue;
185         
186         FdTable::iterator i = fdTable_.find(ev.data.fd);
187         BOOST_ASSERT (i != fdTable_.end() );
188         EventSpec const & spec (i->second); 
189
190         if (ev.events & EPOLLIN) {
191             BOOST_ASSERT(spec.cb_read); 
192             spec.cb_read(EV_READ);
193         }
194         else if (ev.events & EPOLLPRI) {
195             BOOST_ASSERT(spec.cb_prio);
196             spec.cb_prio(EV_PRIO);
197         }
198         else if (ev.events & EPOLLOUT) {
199             BOOST_ASSERT(spec.cb_write);
200             spec.cb_write(EV_WRITE);
201         }
202
203         else if (ev.events & EPOLLHUP) {
204             if (spec.cb_hup)
205                 spec.cb_hup(EV_HUP);
206             else if (ev.events & EPOLLERR) {
207                 if (spec.cb_write) spec.cb_write(EV_HUP);
208                 if (spec.cb_read) spec.cb_read(EV_HUP);
209             }
210         }
211         else if (ev.events & EPOLLERR && ! ev.events & EPOLLHUP) {
212             if (spec.cb_err)
213                 spec.cb_err(EV_ERR);
214             else {
215                 if (spec.cb_write) spec.cb_write(EV_ERR);
216                 if (spec.cb_read) spec.cb_read(EV_ERR);
217             }
218         }
219
220     }
221 }
222
223 ///////////////////////////////cc.e////////////////////////////////////////
224 #undef prefix_
225
226 \f
227 // Local Variables:
228 // mode: c++
229 // c-file-style: "senf"
230 // End: