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