// $Id$
//
-// Copyright (C) 2006
+// Copyright (C) 2006
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
// Stefan Bund <stefan.bund@fokus.fraunhofer.de>
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-// TODO: Implement signal handling
+/** \file
+ \brief Scheduler non-inline non-template implementation
+
+ \idea Implement signal handling (See source for more discussion
+ about this)
+
+ \idea Multithreading support: To support multithreading, the
+ static member Scheduler::instance() must return a thread-local
+ value (that is Scheduler::instance() must allocate one Scheduler
+ instance per thread). Another possibility would be to distribute
+ the async load unto several threads (one scheduler for multiple
+ threads)
+ */
+
// Here a basic concept of how to add signal support to the scheduler:
//
// Every signal to be reported by the scheduler will be asigned a
// // call epoll
// // block all relevant signals again
// }
-//
+//
// // now handle the event
//
// The signal handler is then simply defined as
// You should use sigaction to register the signal handlers and define
// a sa_mask so all Scheduler-registered signals are automatically
// *blocked* whenever one of the signals is called (including the
-// called signal!). This ensures, that no two signals can be delivered
-// on top of each other. And of course any signal registered with the
-// scheduler must be blocked as soon as it is registered with the
-// scheduler.
-
-// TODO: Multithreading support
-// To support multithreading, the static member Scheduler::instance()
-// must return a thread-local value (that is Scheduler::instance()
-// must allocate one Scheduler instance per thread)
-
-// Definition of non-inline non-template functions
+// called signal!) (This also means, we will have to re-register all
+// signals if we change the registration of some signal since the
+// sa_mask changes). This ensures, that no two signals can be
+// delivered on top of each other. And of course any signal registered
+// with the scheduler must be blocked as soon as it is registered with
+// the scheduler.
#include "Scheduler.hh"
//#include "Scheduler.ih"
#include <errno.h>
#include <sys/epoll.h>
#include "Utils/Exception.hh"
+#include "Utils/MicroTime.hh"
static const int EPollInitialSize = 16;
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
-prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance()
+prefix_ senf::Scheduler::Scheduler & senf::Scheduler::instance()
{
static Scheduler instance;
return instance;
}
-prefix_ satcom::lib::Scheduler::Scheduler()
+prefix_ void senf::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
+{
+ timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
+}
+
+prefix_ senf::Scheduler::Scheduler()
: epollFd_(epoll_create(EPollInitialSize))
{
if (epollFd_<0)
throw SystemException(errno);
}
-prefix_ void satcom::lib::Scheduler::do_add(int fd, InternalCallback const & cb, EventId eventMask)
+prefix_ void senf::Scheduler::do_add(int fd, SimpleCallback const & cb, int eventMask)
{
FdTable::iterator i (fdTable_.find(fd));
int action (EPOLL_CTL_MOD);
memset(&ev,0,sizeof(ev));
ev.events = i->second.epollMask();
ev.data.fd = fd;
-
+
if (epoll_ctl(epollFd_, action, fd, &ev)<0)
throw SystemException(errno);
}
-prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
+prefix_ void senf::Scheduler::do_remove(int fd, int eventMask)
{
FdTable::iterator i (fdTable_.find(fd));
- if (i == fdTable_.end())
+ if (i == fdTable_.end())
return;
if (eventMask & EV_READ) i->second.cb_read = 0;
throw SystemException(errno);
}
-prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
+
+prefix_ int senf::Scheduler::EventSpec::epollMask()
const
{
int mask (0);
return mask;
}
-prefix_ void satcom::lib::Scheduler::process()
+prefix_ void senf::Scheduler::process()
{
terminate_ = false;
while (! terminate_) {
+
+ MicroTime timeNow = now();
+ while ( ! timerQueue_.empty() && timerQueue_.top().timeout <= timeNow ) {
+ timerQueue_.top().cb();
+ timerQueue_.pop();
+ }
+ if (terminate_)
+ return;
+ int timeout = timerQueue_.empty() ? -1 : int((timerQueue_.top().timeout - timeNow)/1000);
+
struct epoll_event ev;
- int events = epoll_wait(epollFd_, &ev, 1, 1000);
+ int events = epoll_wait(epollFd_, &ev, 1, timeout);
if (events<0)
// Hmm ... man epoll says, it will NOT return with EINTR ??
throw SystemException(errno);
if (events==0)
+ // Timeout .. it will be run when reachiung the top of the loop
continue;
-
+
FdTable::iterator i = fdTable_.find(ev.data.fd);
BOOST_ASSERT (i != fdTable_.end() );
- EventSpec const & spec (i->second);
+ EventSpec const & spec (i->second);
if (ev.events & EPOLLIN) {
- BOOST_ASSERT(spec.cb_read);
+ BOOST_ASSERT(spec.cb_read);
spec.cb_read(EV_READ);
}
else if (ev.events & EPOLLPRI) {
}
else if (ev.events & EPOLLHUP) {
- BOOST_ASSERT(spec.cb_hup);
- spec.cb_hup(EV_HUP);
+ if (spec.cb_hup)
+ spec.cb_hup(EV_HUP);
+ else if (ev.events & EPOLLERR) {
+ /** \fixme This is stupid, if cb_write and cb_read are
+ the same. The same below. We really have to
+ exactly define sane semantics of what to do on
+ EPOLLHUP and EPOLLERR. */
+ if (spec.cb_write) spec.cb_write(EV_HUP);
+ if (spec.cb_read) spec.cb_read(EV_HUP);
+ }
}
- else if (ev.events & EPOLLERR) {
- BOOST_ASSERT(spec.cb_err);
- spec.cb_err(EV_ERR);
+ else if (ev.events & EPOLLERR && ! ev.events & EPOLLHUP) {
+ if (spec.cb_err)
+ spec.cb_err(EV_ERR);
+ else {
+ if (spec.cb_write) spec.cb_write(EV_ERR);
+ if (spec.cb_read) spec.cb_read(EV_ERR);
+ }
}
+
}
}
\f
// Local Variables:
// mode: c++
-// c-file-style: "satcom"
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
// End: