Remove -ansi -pedantic from the g++ command for better optimization
[senf.git] / Scheduler / Scheduler.cc
index 21885f2..75ed7f8 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, int 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, int eventMask)
 {
     FdTable::iterator i (fdTable_.find(fd));
     if (i == fdTable_.end()) 
@@ -130,7 +136,7 @@ prefix_ void satcom::lib::Scheduler::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,6 +147,7 @@ prefix_ void satcom::lib::Scheduler::remove(int fd, EventId eventMask)
         throw SystemException(errno);
 }
 
+
 prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
     const
 {
@@ -157,12 +164,23 @@ prefix_ void satcom::lib::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);
@@ -171,25 +189,34 @@ 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);
+           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.data.fd, 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);
+           }
         }
+
     }
 }