X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Scheduler%2FScheduler.cc;h=adbb6a6b5f0fe5d3101084f4ee4f081973a23acb;hb=82ad2ed94c12c3e53097fef92978de8c28239fab;hp=e4bc1a09fbc42d59e55994089e0094e67e8c28dc;hpb=8421c3a8da7485cb8781045494ecaab3ed84f403;p=senf.git diff --git a/Scheduler/Scheduler.cc b/Scheduler/Scheduler.cc index e4bc1a0..adbb6a6 100644 --- a/Scheduler/Scheduler.cc +++ b/Scheduler/Scheduler.cc @@ -78,27 +78,16 @@ // Custom includes #include #include -#include "Utils/Exception.hh" -#include "Utils/MicroTime.hh" +#include "../Utils/Exception.hh" static const int EPollInitialSize = 16; #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -prefix_ senf::Scheduler::Scheduler & senf::Scheduler::instance() -{ - static Scheduler instance; - return instance; -} - -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)) + : timerIdCounter_(0), epollFd_ (epoll_create(EPollInitialSize)), terminate_(false), + eventTime_(0) { if (epollFd_<0) throw SystemException(errno); @@ -165,30 +154,62 @@ prefix_ int senf::Scheduler::EventSpec::epollMask() prefix_ void senf::Scheduler::process() { terminate_ = false; + eventTime_ = ClockService::now(); while (! terminate_) { - MicroTime timeNow = now(); - while ( ! timerQueue_.empty() && timerQueue_.top().timeout <= timeNow ) { - timerQueue_.top().cb(); + // Since a callback may have disabled further timers, we need to check for canceled timeouts + // again. + + while (! timerQueue_.empty()) { + TimerMap::iterator i (timerQueue_.top()); + if (! i->second.canceled) + break; + timerMap_.erase(i); timerQueue_.pop(); } - if (terminate_) - return; - int timeout = timerQueue_.empty() ? -1 : int((timerQueue_.top().timeout - timeNow)/1000); + int timeout (-1); + if (timerQueue_.empty()) { + if (fdTable_.empty()) + break; + } + else { + ClockService::clock_type delta ( + (timerQueue_.top()->second.timeout - eventTime_)/1000000UL); + timeout = delta < 0 ? 0 : delta; + } + + ///\todo Handle more than one epoll_event per call struct epoll_event ev; int events = epoll_wait(epollFd_, &ev, 1, timeout); if (events<0) - // Hmm ... man epoll says, it will NOT return with EINTR. I hope, this is true :-) - throw SystemException(errno); - if (events==0) - // Timeout .. the handler will be run when going back to the loop top + if (errno != EINTR) + throw SystemException(errno); + + eventTime_ = ClockService::now(); + + // We always run event handlers. This is important, even if a file-descriptor is signaled + // since some descriptors (e.g. real files) will *always* be ready and we still may want to + // handle timers. + // Time handlers are run before file events to not delay them unnecessarily. + + while (! timerQueue_.empty()) { + TimerMap::iterator i (timerQueue_.top()); + if (i->second.canceled) + ; + else if (i->second.timeout <= eventTime_) + i->second.cb(); + else + break; + timerQueue_.pop(); + timerMap_.erase(i); + } + + if (events <= 0) continue; FdTable::iterator i = fdTable_.find(ev.data.fd); BOOST_ASSERT (i != fdTable_.end() ); - // \todo Make this more efficient. Instead of copying the event-spec it should be - // revalidated by monitoring add/remove calls EventSpec spec (i->second); unsigned extraFlags (0); @@ -222,6 +243,15 @@ prefix_ void senf::Scheduler::process() } } +/////////////////////////////////////////////////////////////////////////// +// senf::SchedulerLogTimeSource + +prefix_ boost::posix_time::ptime senf::SchedulerLogTimeSource::operator()() + const +{ + return ClockService::abstime(Scheduler::instance().eventTime()); +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ @@ -232,4 +262,6 @@ prefix_ void senf::Scheduler::process() // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 // End: