X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Scheduler%2FScheduler.cc;h=5285fa7da8b2e77fd443843830e7b23d8619b80a;hb=51eecff12414ff9a0e99b398c1befb5aa438fd71;hp=21885f24a8fd26bf6add1f5c1cbb0f10cf4d91f4;hpb=c52cd7d87dbb525c1267aad27391b8b7365dbb57;p=senf.git diff --git a/Scheduler/Scheduler.cc b/Scheduler/Scheduler.cc index 21885f2..5285fa7 100644 --- a/Scheduler/Scheduler.cc +++ b/Scheduler/Scheduler.cc @@ -1,6 +1,6 @@ // $Id$ // -// Copyright (C) 2006 +// Copyright (C) 2006 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) // Kompetenzzentrum fuer Satelitenkommunikation (SatCom) // Stefan Bund @@ -20,7 +20,20 @@ // 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 @@ -37,7 +50,7 @@ // // call epoll // // block all relevant signals again // } -// +// // // now handle the event // // The signal handler is then simply defined as @@ -52,17 +65,12 @@ // 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" @@ -77,20 +85,15 @@ static const int EPollInitialSize = 16; #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance() -{ - static Scheduler instance; - return instance; -} - -prefix_ satcom::lib::Scheduler::Scheduler() - : epollFd_(epoll_create(EPollInitialSize)) +prefix_ senf::Scheduler::Scheduler() + : timerIdCounter_(0), epollFd_ (epoll_create(EPollInitialSize)), terminate_(false), + eventTime_(0) { if (epollFd_<0) throw SystemException(errno); } -prefix_ void satcom::lib::Scheduler::add(int fd, Callback 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); @@ -102,29 +105,25 @@ prefix_ void satcom::lib::Scheduler::add(int fd, Callback const & cb, EventId ev if (eventMask & EV_READ) i->second.cb_read = cb; if (eventMask & EV_PRIO) i->second.cb_prio = cb; if (eventMask & EV_WRITE) i->second.cb_write = cb; - if (eventMask & EV_HUP) i->second.cb_hup = cb; - if (eventMask & EV_ERR) i->second.cb_err = cb; epoll_event ev; 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::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; if (eventMask & EV_PRIO) i->second.cb_prio = 0; if (eventMask & EV_WRITE) i->second.cb_write = 0; - if (eventMask & EV_HUP) i->second.cb_hup = 0; - if (eventMask & EV_ERR) i->second.cb_err = 0; epoll_event ev; memset(&ev,0,sizeof(ev)); @@ -141,54 +140,87 @@ prefix_ void satcom::lib::Scheduler::remove(int fd, EventId eventMask) throw SystemException(errno); } -prefix_ int satcom::lib::Scheduler::EventSpec::epollMask() + +prefix_ int senf::Scheduler::EventSpec::epollMask() const { int mask (0); if (cb_read) mask |= EPOLLIN; if (cb_prio) mask |= EPOLLPRI; if (cb_write) mask |= EPOLLOUT; - if (cb_hup) mask |= EPOLLHUP; - if (cb_err) mask |= EPOLLERR; return mask; } -prefix_ void satcom::lib::Scheduler::process() +prefix_ void senf::Scheduler::process() { terminate_ = false; + eventTime_ = ClockService::now(); while (! terminate_) { + while ( ! timerQueue_.empty() && timerQueue_.top()->second.timeout <= eventTime_ ) { + TimerMap::iterator i (timerQueue_.top()); + if (! i->second.canceled) + i->second.cb(); + timerMap_.erase(i); + timerQueue_.pop(); + } + + if (terminate_) + return; + + int timeout (MinTimeout); + if (! timerQueue_.empty()) { + ClockService::clock_type delta ( + (timerQueue_.top()->second.timeout - eventTime_)/1000000UL); + if (deltasecond); + EventSpec spec (i->second); + + unsigned extraFlags (0); + if (ev.events & EPOLLHUP) extraFlags |= EV_HUP; + if (ev.events & EPOLLERR) extraFlags |= EV_ERR; if (ev.events & EPOLLIN) { - BOOST_ASSERT(spec.cb_read); - spec.cb_read(ev.data.fd, EV_READ); + BOOST_ASSERT(spec.cb_read); + spec.cb_read(EventId(EV_READ | extraFlags)); } else if (ev.events & EPOLLPRI) { BOOST_ASSERT(spec.cb_prio); - spec.cb_prio (ev.data.fd, EV_PRIO); + spec.cb_prio(EventId(EV_PRIO | extraFlags)); } else if (ev.events & EPOLLOUT) { BOOST_ASSERT(spec.cb_write); - spec.cb_write(ev.data.fd, EV_WRITE); - } - - else if (ev.events & EPOLLHUP) { - BOOST_ASSERT(spec.cb_hup); - spec.cb_hup(ev.data.fd, EV_HUP); + spec.cb_write(EventId(EV_WRITE | extraFlags)); } - else if (ev.events & EPOLLERR) { - BOOST_ASSERT(spec.cb_err); - spec.cb_err(ev.data.fd, EV_ERR); + else { + // This branch is only taken, if HUP or ERR is signaled but none of IN/OUT/PRI. + // In this case we will signal all registered callbacks. The callbacks must be + // prepared to be called multiple times if they are registered to more than + // one event. + if (spec.cb_write) + spec.cb_write(EventId(extraFlags)); + if (spec.cb_prio) + spec.cb_prio(EventId(extraFlags)); + if (spec.cb_read) + spec.cb_read(EventId(extraFlags)); } } } @@ -199,5 +231,10 @@ prefix_ void satcom::lib::Scheduler::process() // Local Variables: // mode: c++ -// c-file-style: "satcom" +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 // End: