#include <errno.h>
#include <sys/epoll.h>
#include "Utils/Exception.hh"
+#include "Utils/MicroTime.hh"
static const int EPollInitialSize = 16;
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))
{
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);
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())
throw SystemException(errno);
}
+
prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
const
{
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);
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);
}
}
}