From 410932330c9a4e1ee92c617c2ce5d98af9ed0912 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 30 Jan 2014 12:41:34 +0000 Subject: [PATCH] EventManager now wakes up for next timer rather than running a 10Hz --- src/event_manager.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/event_manager.cc b/src/event_manager.cc index 3d0c9f31..4f944b5d 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -47,14 +47,22 @@ EventManager::~EventManager() void EventManager::handle_next_events() { - const int timeout_ms = 100; std::vector events; events.reserve(m_fd_watchers.size()); for (auto& watcher : m_fd_watchers) events.emplace_back(pollfd{ watcher->fd(), POLLIN | POLLPRI, 0 }); std::vector forced; std::swap(forced, m_forced_fd); - poll(events.data(), events.size(), timeout_ms); + + TimePoint next_timer = TimePoint::max(); + for (auto& timer : m_timers) + { + if (timer->next_date() <= next_timer) + next_timer = timer->next_date(); + } + auto timeout = std::chrono::duration_cast(next_timer - Clock::now()); + + poll(events.data(), events.size(), std::max(0, (int)timeout.count())); for (size_t i = 0; i < events.size(); ++i) { auto& event = events[i];