git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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
75 static const int EPollInitialSize = 16;
76
77 #define prefix_
78 ///////////////////////////////cc.p////////////////////////////////////////
79
80 prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance()
81 {
82     static Scheduler instance;
83     return instance;
84 }
85
86 prefix_ satcom::lib::Scheduler::Scheduler()
87     : epollFd_(epoll_create(EPollInitialSize))
88 {
89     if (epollFd_<0)
90         throw SystemException(errno);
91 }
92
93 prefix_ void satcom::lib::Scheduler::add(int fd, Callback const & cb, EventId eventMask)
94 {
95     FdTable::iterator i (fdTable_.find(fd));
96     int action (EPOLL_CTL_MOD);
97     if (i == fdTable_.end()) {
98         action = EPOLL_CTL_ADD;
99         i = fdTable_.insert(std::make_pair(fd, EventSpec())).first;
100     }
101
102     if (eventMask & EV_READ)  i->second.cb_read = cb;
103     if (eventMask & EV_PRIO)  i->second.cb_prio = cb;
104     if (eventMask & EV_WRITE) i->second.cb_write = cb;
105     if (eventMask & EV_HUP)   i->second.cb_hup = cb;
106     if (eventMask & EV_ERR)   i->second.cb_err = cb;
107
108     epoll_event ev;
109     memset(&ev,0,sizeof(ev));
110     ev.events = i->second.epollMask();
111     ev.data.fd = fd;
112     
113     if (epoll_ctl(epollFd_, action, fd, &ev)<0)
114         throw SystemException(errno);
115 }
116
117 prefix_ void satcom::lib::Scheduler::remove(int fd, EventId eventMask)
118 {
119     FdTable::iterator i (fdTable_.find(fd));
120     if (i == fdTable_.end()) 
121         return;
122
123     if (eventMask & EV_READ)  i->second.cb_read = 0;
124     if (eventMask & EV_PRIO)  i->second.cb_prio = 0;
125     if (eventMask & EV_WRITE) i->second.cb_write = 0;
126     if (eventMask & EV_HUP)   i->second.cb_hup = 0;
127     if (eventMask & EV_ERR)   i->second.cb_err = 0;
128
129     epoll_event ev;
130     memset(&ev,0,sizeof(ev));
131     ev.events = i->second.epollMask();
132     ev.data.fd = fd;
133
134     int action (EPOLL_CTL_MOD);
135     if (ev.events==0) {
136         action = EPOLL_CTL_DEL;
137         fdTable_.erase(i);
138     }
139
140     if (epoll_ctl(epollFd_, action, fd, &ev)<0)
141         throw SystemException(errno);
142 }
143
144 prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
145     const
146 {
147     int mask (0);
148     if (cb_read)  mask |= EPOLLIN;
149     if (cb_prio)  mask |= EPOLLPRI;
150     if (cb_write) mask |= EPOLLOUT;
151     if (cb_hup)   mask |= EPOLLHUP;
152     if (cb_err)   mask |= EPOLLERR;
153     return mask;
154 }
155
156 prefix_ void satcom::lib::Scheduler::process()
157 {
158     terminate_ = false;
159     while (! terminate_) {
160         struct epoll_event ev;
161         int events = epoll_wait(epollFd_, &ev, 1, 1000);
162         if (events<0)
163             // Hmm ... man epoll says, it will NOT return with EINTR ??
164             throw SystemException(errno);
165         if (events==0)
166             continue;
167         
168         FdTable::iterator i = fdTable_.find(ev.data.fd);
169         BOOST_ASSERT (i != fdTable_.end() );
170         EventSpec const & spec (i->second); 
171
172         if (ev.events & EPOLLIN) {
173             BOOST_ASSERT(spec.cb_read); 
174             spec.cb_read(ev.data.fd, EV_READ);
175         }
176         else if (ev.events & EPOLLPRI) {
177             BOOST_ASSERT(spec.cb_prio);
178             spec.cb_prio (ev.data.fd, EV_PRIO);
179         }
180         else if (ev.events & EPOLLOUT) {
181             BOOST_ASSERT(spec.cb_write);
182             spec.cb_write(ev.data.fd, EV_WRITE);
183         }
184
185         else if (ev.events & EPOLLHUP) {
186             BOOST_ASSERT(spec.cb_hup);
187             spec.cb_hup(ev.data.fd, EV_HUP);
188         }
189         else if (ev.events & EPOLLERR) {
190             BOOST_ASSERT(spec.cb_err);
191             spec.cb_err(ev.data.fd, EV_ERR);
192         }
193     }
194 }
195
196 ///////////////////////////////cc.e////////////////////////////////////////
197 #undef prefix_
198
199 \f
200 // Local Variables:
201 // mode: c++
202 // c-file-style: "satcom"
203 // End: