Simple timeout implementation
[senf.git] / Scheduler / Scheduler.cc
index 21885f2..4f39c4b 100644 (file)
@@ -71,6 +71,7 @@
 #include <errno.h>
 #include <sys/epoll.h>
 #include "Utils/Exception.hh"
+#include "Utils/MicroTime.hh"
 
 static const int EPollInitialSize = 16;
 
@@ -83,6 +84,11 @@ prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance()
     return instance;
 }
 
+prefix_ void satcom::lib::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
+{
+    timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
+}
+
 prefix_ satcom::lib::Scheduler::Scheduler()
     : epollFd_(epoll_create(EPollInitialSize))
 {
@@ -90,7 +96,7 @@ prefix_ satcom::lib::Scheduler::Scheduler()
         throw SystemException(errno);
 }
 
-prefix_ void satcom::lib::Scheduler::add(int fd, Callback const & cb, EventId eventMask)
+prefix_ void satcom::lib::Scheduler::do_add(int fd, SimpleCallback const & cb, EventId eventMask)
 {
     FdTable::iterator i (fdTable_.find(fd));
     int action (EPOLL_CTL_MOD);
@@ -114,7 +120,7 @@ prefix_ void satcom::lib::Scheduler::add(int fd, Callback const & cb, EventId ev
         throw SystemException(errno);
 }
 
-prefix_ void satcom::lib::Scheduler::remove(int fd, EventId eventMask)
+prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
 {
     FdTable::iterator i (fdTable_.find(fd));
     if (i == fdTable_.end()) 
@@ -141,6 +147,7 @@ prefix_ void satcom::lib::Scheduler::remove(int fd, EventId eventMask)
         throw SystemException(errno);
 }
 
+
 prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
     const
 {
@@ -158,11 +165,21 @@ prefix_ void satcom::lib::Scheduler::process()
     terminate_ = false;
     while (! terminate_) {
         struct epoll_event ev;
-        int events = epoll_wait(epollFd_, &ev, 1, 1000);
+       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);
+           
+        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);
@@ -171,24 +188,24 @@ prefix_ void satcom::lib::Scheduler::process()
 
         if (ev.events & EPOLLIN) {
             BOOST_ASSERT(spec.cb_read); 
-            spec.cb_read(ev.data.fd, EV_READ);
+            spec.cb_read(EV_READ);
         }
         else if (ev.events & EPOLLPRI) {
             BOOST_ASSERT(spec.cb_prio);
-            spec.cb_prio (ev.data.fd, EV_PRIO);
+            spec.cb_prio(EV_PRIO);
         }
         else if (ev.events & EPOLLOUT) {
             BOOST_ASSERT(spec.cb_write);
-            spec.cb_write(ev.data.fd, EV_WRITE);
+            spec.cb_write(EV_WRITE);
         }
 
         else if (ev.events & EPOLLHUP) {
             BOOST_ASSERT(spec.cb_hup);
-            spec.cb_hup(ev.data.fd, EV_HUP);
+            spec.cb_hup(EV_HUP);
         }
         else if (ev.events & EPOLLERR) {
             BOOST_ASSERT(spec.cb_err);
-            spec.cb_err(ev.data.fd, EV_ERR);
+            spec.cb_err(EV_ERR);
         }
     }
 }