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