Commit 3cdd3857 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

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
parent 77249d4d
...@@ -56,7 +56,7 @@ HHWheelTimer::Callback::~Callback() { ...@@ -56,7 +56,7 @@ HHWheelTimer::Callback::~Callback() {
void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel, void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel,
std::chrono::milliseconds timeout) { std::chrono::milliseconds timeout) {
assert(wheel_ == nullptr); assert(wheel_ == nullptr);
assert(expiration_ == milliseconds(0)); assert(expiration_ == decltype(expiration_){});
wheel_ = wheel; wheel_ = wheel;
...@@ -75,7 +75,7 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() { ...@@ -75,7 +75,7 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() {
} }
wheel_ = nullptr; wheel_ = nullptr;
expiration_ = milliseconds(0); expiration_ = {};
} }
HHWheelTimer::HHWheelTimer( HHWheelTimer::HHWheelTimer(
...@@ -232,7 +232,7 @@ void HHWheelTimer::timeoutExpired() noexcept { ...@@ -232,7 +232,7 @@ void HHWheelTimer::timeoutExpired() noexcept {
timeouts.pop_front(); timeouts.pop_front();
count_--; count_--;
cb->wheel_ = nullptr; cb->wheel_ = nullptr;
cb->expiration_ = milliseconds(0); cb->expiration_ = {};
RequestContextScopeGuard rctx(cb->context_); RequestContextScopeGuard rctx(cb->context_);
cb->timeoutExpired(); cb->timeoutExpired();
if (isDestroyed) { if (isDestroyed) {
...@@ -306,8 +306,7 @@ void HHWheelTimer::scheduleNextTimeout() { ...@@ -306,8 +306,7 @@ void HHWheelTimer::scheduleNextTimeout() {
} }
int64_t HHWheelTimer::calcNextTick() { int64_t HHWheelTimer::calcNextTick() {
auto intervals = auto intervals = (getCurTime() - startTime_) / interval_;
(getCurTime().count() - startTime_.count()) / interval_.count();
// Slow eventbases will have skew between the actual time and the // Slow eventbases will have skew between the actual time and the
// callback time. To avoid racing the next scheduleNextTimeout() // callback time. To avoid racing the next scheduleNextTimeout()
// call, always schedule new timeouts against the actual // call, always schedule new timeouts against the actual
......
...@@ -70,10 +70,7 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -70,10 +70,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
: public boost::intrusive::list_base_hook< : public boost::intrusive::list_base_hook<
boost::intrusive::link_mode<boost::intrusive::auto_unlink>> { boost::intrusive::link_mode<boost::intrusive::auto_unlink>> {
public: public:
Callback() Callback() = default;
: wheel_(nullptr)
, expiration_(0) {}
virtual ~Callback(); virtual ~Callback();
/** /**
...@@ -113,27 +110,27 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -113,27 +110,27 @@ class HHWheelTimer : private folly::AsyncTimeout,
* Don't override this unless you're doing a test. This is mainly here so * 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. * that we can override it to simulate lag in steady_clock.
*/ */
virtual std::chrono::milliseconds getCurTime() { virtual std::chrono::steady_clock::time_point getCurTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch());
} }
private: private:
// Get the time remaining until this timeout expires // Get the time remaining until this timeout expires
std::chrono::milliseconds getTimeRemaining( std::chrono::milliseconds getTimeRemaining(
std::chrono::milliseconds now) const { std::chrono::steady_clock::time_point now) const {
if (now >= expiration_) { if (now >= expiration_) {
return std::chrono::milliseconds(0); return std::chrono::milliseconds(0);
} }
return expiration_ - now; return std::chrono::duration_cast<std::chrono::milliseconds>(
expiration_ - now);
} }
void setScheduled(HHWheelTimer* wheel, void setScheduled(HHWheelTimer* wheel,
std::chrono::milliseconds); std::chrono::milliseconds);
void cancelTimeoutImpl(); void cancelTimeoutImpl();
HHWheelTimer* wheel_; HHWheelTimer* wheel_{nullptr};
std::chrono::milliseconds expiration_; std::chrono::steady_clock::time_point expiration_{};
int bucket_{-1}; int bucket_{-1};
typedef boost::intrusive::list< typedef boost::intrusive::list<
...@@ -288,7 +285,7 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -288,7 +285,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
int64_t lastTick_; int64_t lastTick_;
int64_t expireTick_; int64_t expireTick_;
uint64_t count_; uint64_t count_;
std::chrono::milliseconds startTime_; std::chrono::steady_clock::time_point startTime_;
int64_t calcNextTick(); int64_t calcNextTick();
...@@ -297,9 +294,8 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -297,9 +294,8 @@ class HHWheelTimer : private folly::AsyncTimeout,
bool* processingCallbacksGuard_; bool* processingCallbacksGuard_;
CallbackList timeouts; // Timeouts queued to run CallbackList timeouts; // Timeouts queued to run
std::chrono::milliseconds getCurTime() { std::chrono::steady_clock::time_point getCurTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::steady_clock::now();
std::chrono::steady_clock::now().time_since_epoch());
} }
}; };
......
...@@ -56,10 +56,8 @@ class TestTimeout : public HHWheelTimer::Callback { ...@@ -56,10 +56,8 @@ class TestTimeout : public HHWheelTimer::Callback {
class TestTimeoutDelayed : public TestTimeout { class TestTimeoutDelayed : public TestTimeout {
protected: protected:
std::chrono::milliseconds getCurTime() override { std::chrono::steady_clock::time_point getCurTime() override {
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::steady_clock::now() - milliseconds(5);
std::chrono::steady_clock::now().time_since_epoch()) -
milliseconds(5);
} }
}; };
......
...@@ -56,10 +56,8 @@ class TestTimeout : public HHWheelTimer::Callback { ...@@ -56,10 +56,8 @@ class TestTimeout : public HHWheelTimer::Callback {
class TestTimeoutDelayed : public TestTimeout { class TestTimeoutDelayed : public TestTimeout {
protected: protected:
std::chrono::milliseconds getCurTime() override { std::chrono::steady_clock::time_point getCurTime() override {
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::steady_clock::now() - milliseconds(5);
std::chrono::steady_clock::now().time_since_epoch()) -
milliseconds(5);
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment