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