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