Properly std::chrono'ize HHWheelTimer
authorChristopher Dykes <cdykes@fb.com>
Sat, 7 Jan 2017 20:51:23 +0000 (12:51 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 7 Jan 2017 21:03:09 +0000 (13:03 -0800)
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

folly/io/async/HHWheelTimer.cpp
folly/io/async/HHWheelTimer.h
folly/io/async/test/HHWheelTimerSlowTests.cpp
folly/io/async/test/HHWheelTimerTest.cpp

index eda571399ec7547ac24613a1c0ade757f15d8c16..c464c3fbad2e45206b160b883d9a736ecfe426ff 100644 (file)
@@ -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
index 1701bfabfc87fca04186329ea31d34fdf80bb05c..844ff906bbbfa4107755832efa91603908ab046e 100644 (file)
@@ -70,10 +70,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
       : public boost::intrusive::list_base_hook<
             boost::intrusive::link_mode<boost::intrusive::auto_unlink>> {
    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::milliseconds>(
-        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<std::chrono::milliseconds>(
+          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::milliseconds>(
-        std::chrono::steady_clock::now().time_since_epoch());
+  std::chrono::steady_clock::time_point getCurTime() {
+    return std::chrono::steady_clock::now();
   }
 };
 
index 3d55ef38eab0326dc8d94f09c0e22bdc488da95f..d1451c49a88b635696ab1221927ad8fbef5aaeb0 100644 (file)
@@ -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::milliseconds>(
-               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);
   }
 };
 
index efe9b5a152028fa3854584a475d3af76b9e739cf..4c86db029aaae538fc3c516d4ed88f1cf9e3a5dd 100644 (file)
@@ -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::milliseconds>(
-        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 {