From: Christopher Dykes Date: Sat, 7 Jan 2017 20:51:23 +0000 (-0800) Subject: Properly std::chrono'ize HHWheelTimer X-Git-Tag: v2017.03.06.00~120 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=3cdd3857fbfacd9312a23214f54a47f156726927 Properly std::chrono'ize HHWheelTimer Summary: It was using `std::chrono::milliseconds` to represent a point in time, so use a time point instead. Reviewed By: yfeldblum Differential Revision: D4378116 fbshipit-source-id: f0b10bb7894dda44d78b31672d2b6735f3e1cbf4 --- diff --git a/folly/io/async/HHWheelTimer.cpp b/folly/io/async/HHWheelTimer.cpp index eda57139..c464c3fb 100644 --- a/folly/io/async/HHWheelTimer.cpp +++ b/folly/io/async/HHWheelTimer.cpp @@ -56,7 +56,7 @@ HHWheelTimer::Callback::~Callback() { void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel, std::chrono::milliseconds timeout) { assert(wheel_ == nullptr); - assert(expiration_ == milliseconds(0)); + assert(expiration_ == decltype(expiration_){}); wheel_ = wheel; @@ -75,7 +75,7 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() { } wheel_ = nullptr; - expiration_ = milliseconds(0); + expiration_ = {}; } HHWheelTimer::HHWheelTimer( @@ -232,7 +232,7 @@ void HHWheelTimer::timeoutExpired() noexcept { timeouts.pop_front(); count_--; cb->wheel_ = nullptr; - cb->expiration_ = milliseconds(0); + cb->expiration_ = {}; RequestContextScopeGuard rctx(cb->context_); cb->timeoutExpired(); if (isDestroyed) { @@ -306,8 +306,7 @@ void HHWheelTimer::scheduleNextTimeout() { } int64_t HHWheelTimer::calcNextTick() { - auto intervals = - (getCurTime().count() - startTime_.count()) / interval_.count(); + auto intervals = (getCurTime() - startTime_) / interval_; // Slow eventbases will have skew between the actual time and the // callback time. To avoid racing the next scheduleNextTimeout() // call, always schedule new timeouts against the actual diff --git a/folly/io/async/HHWheelTimer.h b/folly/io/async/HHWheelTimer.h index 1701bfab..844ff906 100644 --- a/folly/io/async/HHWheelTimer.h +++ b/folly/io/async/HHWheelTimer.h @@ -70,10 +70,7 @@ class HHWheelTimer : private folly::AsyncTimeout, : public boost::intrusive::list_base_hook< boost::intrusive::link_mode> { public: - Callback() - : wheel_(nullptr) - , expiration_(0) {} - + Callback() = default; virtual ~Callback(); /** @@ -113,27 +110,27 @@ class HHWheelTimer : private folly::AsyncTimeout, * Don't override this unless you're doing a test. This is mainly here so * that we can override it to simulate lag in steady_clock. */ - virtual std::chrono::milliseconds getCurTime() { - return std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()); + virtual std::chrono::steady_clock::time_point getCurTime() { + return std::chrono::steady_clock::now(); } private: // Get the time remaining until this timeout expires std::chrono::milliseconds getTimeRemaining( - std::chrono::milliseconds now) const { + std::chrono::steady_clock::time_point now) const { if (now >= expiration_) { return std::chrono::milliseconds(0); } - return expiration_ - now; + return std::chrono::duration_cast( + expiration_ - now); } void setScheduled(HHWheelTimer* wheel, std::chrono::milliseconds); void cancelTimeoutImpl(); - HHWheelTimer* wheel_; - std::chrono::milliseconds expiration_; + HHWheelTimer* wheel_{nullptr}; + std::chrono::steady_clock::time_point expiration_{}; int bucket_{-1}; typedef boost::intrusive::list< @@ -288,7 +285,7 @@ class HHWheelTimer : private folly::AsyncTimeout, int64_t lastTick_; int64_t expireTick_; uint64_t count_; - std::chrono::milliseconds startTime_; + std::chrono::steady_clock::time_point startTime_; int64_t calcNextTick(); @@ -297,9 +294,8 @@ class HHWheelTimer : private folly::AsyncTimeout, bool* processingCallbacksGuard_; CallbackList timeouts; // Timeouts queued to run - std::chrono::milliseconds getCurTime() { - return std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()); + std::chrono::steady_clock::time_point getCurTime() { + return std::chrono::steady_clock::now(); } }; diff --git a/folly/io/async/test/HHWheelTimerSlowTests.cpp b/folly/io/async/test/HHWheelTimerSlowTests.cpp index 3d55ef38..d1451c49 100644 --- a/folly/io/async/test/HHWheelTimerSlowTests.cpp +++ b/folly/io/async/test/HHWheelTimerSlowTests.cpp @@ -56,10 +56,8 @@ class TestTimeout : public HHWheelTimer::Callback { class TestTimeoutDelayed : public TestTimeout { protected: - std::chrono::milliseconds getCurTime() override { - return std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()) - - milliseconds(5); + std::chrono::steady_clock::time_point getCurTime() override { + return std::chrono::steady_clock::now() - milliseconds(5); } }; diff --git a/folly/io/async/test/HHWheelTimerTest.cpp b/folly/io/async/test/HHWheelTimerTest.cpp index efe9b5a1..4c86db02 100644 --- a/folly/io/async/test/HHWheelTimerTest.cpp +++ b/folly/io/async/test/HHWheelTimerTest.cpp @@ -56,11 +56,9 @@ class TestTimeout : public HHWheelTimer::Callback { class TestTimeoutDelayed : public TestTimeout { protected: - std::chrono::milliseconds getCurTime() override { - return std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()) - - milliseconds(5); - } + std::chrono::steady_clock::time_point getCurTime() override { + return std::chrono::steady_clock::now() - milliseconds(5); + } }; struct HHWheelTimerTest : public ::testing::Test {