Commit 65ad7e9d authored by Stepan Palamarchuk's avatar Stepan Palamarchuk Committed by Facebook Github Bot

Fold lastTick_ into expireTick_

Summary: There's no need to distinguish between them (it only adds complexity by having two modes: schedules vs running).

Reviewed By: djwatson

Differential Revision: D13541505

fbshipit-source-id: 7cb2c3fb9ba4e3b191adee37ad1d28f471378f85
parent 6ea3ec05
...@@ -83,7 +83,6 @@ HHWheelTimer::HHWheelTimer( ...@@ -83,7 +83,6 @@ HHWheelTimer::HHWheelTimer(
: AsyncTimeout(timeoutMananger, internal), : AsyncTimeout(timeoutMananger, internal),
interval_(intervalMS), interval_(intervalMS),
defaultTimeout_(defaultTimeoutMS), defaultTimeout_(defaultTimeoutMS),
lastTick_(1),
expireTick_(1), expireTick_(1),
count_(0), count_(0),
startTime_(getCurTime()), startTime_(getCurTime()),
...@@ -150,15 +149,13 @@ void HHWheelTimer::scheduleTimeout( ...@@ -150,15 +149,13 @@ void HHWheelTimer::scheduleTimeout(
// There are three possible scenarios: // There are three possible scenarios:
// - we are currently inside of HHWheelTimer::timeoutExpired. In this case, // - we are currently inside of HHWheelTimer::timeoutExpired. In this case,
// we need to use its lastTick_ as a base for computations // we need to use its last tick as a base for computations
// - HHWheelTimer tick timeout is already scheduled. In this case, // - HHWheelTimer tick timeout is already scheduled. In this case,
// we need to use its scheduled tick as a base. // we need to use its scheduled tick as a base.
// - none of the above are true. In this case, it's safe to use the nextTick // - none of the above are true. In this case, it's safe to use the nextTick
// as a base. // as a base.
int64_t baseTick = nextTick; int64_t baseTick = nextTick;
if (processingCallbacksGuard_) { if (processingCallbacksGuard_ || isScheduled()) {
baseTick = std::min(lastTick_, nextTick);
} else if (isScheduled()) {
baseTick = std::min(expireTick_, nextTick); baseTick = std::min(expireTick_, nextTick);
} }
scheduleTimeoutImpl(callback, timeout, baseTick, nextTick); scheduleTimeoutImpl(callback, timeout, baseTick, nextTick);
...@@ -184,7 +181,7 @@ bool HHWheelTimer::cascadeTimers(int bucket, int tick) { ...@@ -184,7 +181,7 @@ bool HHWheelTimer::cascadeTimers(int bucket, int tick) {
auto* cb = &cbs.front(); auto* cb = &cbs.front();
cbs.pop_front(); cbs.pop_front();
scheduleTimeoutImpl( scheduleTimeoutImpl(
cb, cb->getTimeRemaining(getCurTime()), lastTick_, calcNextTick()); cb, cb->getTimeRemaining(getCurTime()), expireTick_, calcNextTick());
} }
// If tick is zero, timeoutExpired will cascade the next bucket. // If tick is zero, timeoutExpired will cascade the next bucket.
...@@ -212,22 +209,21 @@ void HHWheelTimer::timeoutExpired() noexcept { ...@@ -212,22 +209,21 @@ void HHWheelTimer::timeoutExpired() noexcept {
// timeoutExpired() can only be invoked directly from the event base loop. // timeoutExpired() can only be invoked directly from the event base loop.
// It should never be invoked recursively. // It should never be invoked recursively.
// //
lastTick_ = expireTick_; while (expireTick_ < nextTick) {
while (lastTick_ < nextTick) { int idx = expireTick_ & WHEEL_MASK;
int idx = lastTick_ & WHEEL_MASK;
if (idx == 0) { if (idx == 0) {
// Cascade timers // Cascade timers
if (cascadeTimers(1, (lastTick_ >> WHEEL_BITS) & WHEEL_MASK) && if (cascadeTimers(1, (expireTick_ >> WHEEL_BITS) & WHEEL_MASK) &&
cascadeTimers(2, (lastTick_ >> (2 * WHEEL_BITS)) & WHEEL_MASK)) { cascadeTimers(2, (expireTick_ >> (2 * WHEEL_BITS)) & WHEEL_MASK)) {
cascadeTimers(3, (lastTick_ >> (3 * WHEEL_BITS)) & WHEEL_MASK); cascadeTimers(3, (expireTick_ >> (3 * WHEEL_BITS)) & WHEEL_MASK);
} }
} }
auto bi = makeBitIterator(bitmap_.begin()); auto bi = makeBitIterator(bitmap_.begin());
*(bi + idx) = false; *(bi + idx) = false;
lastTick_++; expireTick_++;
CallbackList* cbs = &buckets_[0][idx]; CallbackList* cbs = &buckets_[0][idx];
while (!cbs->empty()) { while (!cbs->empty()) {
auto* cb = &cbs->front(); auto* cb = &cbs->front();
...@@ -251,7 +247,7 @@ void HHWheelTimer::timeoutExpired() noexcept { ...@@ -251,7 +247,7 @@ void HHWheelTimer::timeoutExpired() noexcept {
return; return;
} }
} }
scheduleNextTimeout(lastTick_); scheduleNextTimeout(expireTick_);
} }
size_t HHWheelTimer::cancelAll() { size_t HHWheelTimer::cancelAll() {
......
...@@ -293,7 +293,6 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -293,7 +293,6 @@ class HHWheelTimer : private folly::AsyncTimeout,
} }
bool cascadeTimers(int bucket, int tick); bool cascadeTimers(int bucket, int tick);
int64_t lastTick_;
int64_t expireTick_; int64_t expireTick_;
std::size_t count_; std::size_t count_;
std::chrono::steady_clock::time_point startTime_; std::chrono::steady_clock::time_point startTime_;
......
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