event/Loop: use ClockCache

This commit is contained in:
Max Kellermann
2020-12-01 20:22:52 +01:00
parent 16074c565f
commit 790e540c19
2 changed files with 20 additions and 8 deletions

View File

@@ -159,7 +159,7 @@ EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept
{ {
assert(IsInside()); assert(IsInside());
t.due = now + d; t.due = SteadyNow() + d;
timers.insert(t); timers.insert(t);
again = true; again = true;
} }
@@ -167,6 +167,8 @@ EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept
inline Event::Duration inline Event::Duration
EventLoop::HandleTimers() noexcept EventLoop::HandleTimers() noexcept
{ {
const auto now = SteadyNow();
Event::Duration timeout; Event::Duration timeout;
while (!quit) { while (!quit) {
@@ -301,8 +303,9 @@ EventLoop::Run() noexcept
}; };
#endif #endif
steady_clock_cache.flush();
do { do {
now = std::chrono::steady_clock::now();
again = false; again = false;
/* invoke timers */ /* invoke timers */
@@ -340,7 +343,7 @@ EventLoop::Run() noexcept
Wait(timeout); Wait(timeout);
now = std::chrono::steady_clock::now(); steady_clock_cache.flush();
#ifdef HAVE_THREADED_EVENT_LOOP #ifdef HAVE_THREADED_EVENT_LOOP
{ {

View File

@@ -24,6 +24,7 @@
#include "Backend.hxx" #include "Backend.hxx"
#include "SocketEvent.hxx" #include "SocketEvent.hxx"
#include "event/Features.h" #include "event/Features.h"
#include "time/ClockCache.hxx"
#include "util/Compiler.h" #include "util/Compiler.h"
#include "util/IntrusiveList.hxx" #include "util/IntrusiveList.hxx"
@@ -114,8 +115,6 @@ class EventLoop final
std::unique_ptr<Uring::Manager> uring; std::unique_ptr<Uring::Manager> uring;
#endif #endif
Event::Clock::time_point now = Event::Clock::now();
#ifdef HAVE_THREADED_EVENT_LOOP #ifdef HAVE_THREADED_EVENT_LOOP
/** /**
* A reference to the thread that is currently inside Run(). * A reference to the thread that is currently inside Run().
@@ -155,6 +154,8 @@ class EventLoop final
EventPollBackend poll_backend; EventPollBackend poll_backend;
ClockCache<std::chrono::steady_clock> steady_clock_cache;
public: public:
/** /**
* Throws on error. * Throws on error.
@@ -172,15 +173,23 @@ public:
EventLoop(const EventLoop &other) = delete; EventLoop(const EventLoop &other) = delete;
EventLoop &operator=(const EventLoop &other) = delete; EventLoop &operator=(const EventLoop &other) = delete;
const auto &GetSteadyClockCache() const noexcept {
return steady_clock_cache;
}
/** /**
* A caching wrapper for Event::Clock::now(). * Caching wrapper for std::chrono::steady_clock::now(). The
* real clock is queried at most once per event loop
* iteration, because it is assumed that the event loop runs
* for a negligible duration.
*/ */
auto GetTime() const { gcc_pure
const auto &SteadyNow() const noexcept {
#ifdef HAVE_THREADED_EVENT_LOOP #ifdef HAVE_THREADED_EVENT_LOOP
assert(IsInside()); assert(IsInside());
#endif #endif
return now; return steady_clock_cache.now();
} }
#ifdef HAVE_URING #ifdef HAVE_URING