Refactor (again) event handling, use proper infinite timeout

This commit is contained in:
Maxime Coste 2014-12-04 20:01:34 +00:00
parent 176358f6b7
commit 423030c9bd

View File

@ -77,19 +77,20 @@ void EventManager::handle_next_events(EventMode mode)
} }
} }
TimePoint next_timer = TimePoint::max(); timeval tv{};
for (auto& timer : m_timers) if (not m_timers.empty())
{ {
if (timer->next_date() <= next_timer) auto next = std::min_element(
next_timer = timer->next_date(); m_timers.begin(), m_timers.end(), [](Timer* lhs, Timer* rhs) {
return lhs->next_date() < rhs->next_date();
});
using namespace std::chrono; using us = std::chrono::microseconds;
auto usecs = std::max(us(0), duration_cast<us>((*next)->next_date() - Clock::now()));
auto secs = duration_cast<seconds>(usecs);
tv = timeval{ (time_t)secs.count(), (suseconds_t)(usecs - secs).count() };
} }
using namespace std::chrono; int res = select(max_fd + 1, &rfds, nullptr, nullptr,
auto timeout = duration_cast<microseconds>(next_timer - Clock::now()).count(); m_timers.empty() ? nullptr : &tv);
constexpr auto us = 1000000;
// max timeout of 2 secs
timeval tv{ (time_t)(timeout > us ? 1 : 0), (suseconds_t)(timeout % us) };
int res = select(max_fd + 1, &rfds, nullptr, nullptr, &tv);
// copy forced fds *after* poll, so that signal handlers can write to // copy forced fds *after* poll, so that signal handlers can write to
// m_forced_fd, interupt poll, and directly be serviced. // m_forced_fd, interupt poll, and directly be serviced.