event/Loop: move TimerRecord::due to class TimeoutMonitor

Prepare to eliminate the TimerRecord struct.
This commit is contained in:
Max Kellermann 2017-08-29 13:34:08 +02:00
parent d4266d0063
commit 71ed3ff992
3 changed files with 28 additions and 14 deletions

View File

@ -27,6 +27,18 @@
#include <algorithm> #include <algorithm>
inline std::chrono::steady_clock::time_point
EventLoop::TimerRecord::GetDue() const noexcept
{
return timer.due;
}
inline bool
EventLoop::TimerRecord::operator<(const TimerRecord &other) const noexcept
{
return timer.due < other.timer.due;
}
EventLoop::EventLoop(ThreadId _thread) EventLoop::EventLoop(ThreadId _thread)
:SocketMonitor(*this), thread(_thread) :SocketMonitor(*this), thread(_thread)
{ {
@ -93,7 +105,8 @@ EventLoop::AddTimer(TimeoutMonitor &t, std::chrono::steady_clock::duration d)
{ {
assert(IsInside()); assert(IsInside());
timers.insert(TimerRecord(t, now + d)); t.due = now + d;
timers.insert(TimerRecord(t));
again = true; again = true;
} }
@ -150,7 +163,7 @@ EventLoop::Run()
break; break;
} }
timeout = i->due - now; timeout = i->GetDue() - now;
if (timeout > timeout.zero()) if (timeout > timeout.zero())
break; break;

View File

@ -51,21 +51,16 @@ class DeferredMonitor;
class EventLoop final : SocketMonitor class EventLoop final : SocketMonitor
{ {
struct TimerRecord { struct TimerRecord {
/**
* Projected monotonic_clock_ms() value when this
* timer is due.
*/
const std::chrono::steady_clock::time_point due;
TimeoutMonitor &timer; TimeoutMonitor &timer;
constexpr TimerRecord(TimeoutMonitor &_timer, explicit constexpr TimerRecord(TimeoutMonitor &_timer)
std::chrono::steady_clock::time_point _due) :timer(_timer) {}
:due(_due), timer(_timer) {}
bool operator<(const TimerRecord &other) const { gcc_pure
return due < other.due; std::chrono::steady_clock::time_point GetDue() const noexcept;
}
gcc_pure
bool operator<(const TimerRecord &other) const noexcept;
}; };
WakeFD wake_fd; WakeFD wake_fd;

View File

@ -39,6 +39,12 @@ class TimeoutMonitor {
EventLoop &loop; EventLoop &loop;
/**
* When is this timer due? This is only valid if #active is
* true.
*/
std::chrono::steady_clock::time_point due;
bool active; bool active;
public: public: