X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Scheduler%2FScheduler.cc;h=62a5dfecf55bdb2ac3f8a2ef315786dee5ff4c47;hb=331547d2a137f796eb5fcb390502aece3e01bb16;hp=5e8a860835719e214a45f0723cb5291b34f7f3e8;hpb=1835b928b179302ecb716d697fbf3fa24b415ba4;p=senf.git diff --git a/Scheduler/Scheduler.cc b/Scheduler/Scheduler.cc index 5e8a860..62a5dfe 100644 --- a/Scheduler/Scheduler.cc +++ b/Scheduler/Scheduler.cc @@ -71,26 +71,32 @@ #include #include #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); @@ -114,7 +120,7 @@ prefix_ void satcom::lib::Scheduler::do_add(int fd, InternalCallback const & cb, 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()) @@ -130,7 +136,7 @@ prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask) memset(&ev,0,sizeof(ev)); ev.events = i->second.epollMask(); ev.data.fd = fd; - + int action (EPOLL_CTL_MOD); if (ev.events==0) { action = EPOLL_CTL_DEL; @@ -141,7 +147,8 @@ prefix_ void satcom::lib::Scheduler::do_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); @@ -153,16 +160,27 @@ prefix_ int satcom::lib::Scheduler::EventSpec::epollMask() 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); @@ -183,13 +201,22 @@ prefix_ void satcom::lib::Scheduler::process() } 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) { + 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); + } } + } } @@ -199,5 +226,5 @@ prefix_ void satcom::lib::Scheduler::process() // Local Variables: // mode: c++ -// c-file-style: "satcom" +// c-file-style: "senf" // End: