Skip to content

Commit

Permalink
Allow cascading into the current tick
Browse files Browse the repository at this point in the history
Summary:
Currently, when cascading, we are unable to use bucket 0. This means that timeouts that are already expired would need to wait another tick before being fired.

A simple example is when we're at tick 0 and scheduling for tick 256, such timeout would go into cascading logic and should fall into bucket 0 of next wheel epoch. However, the existing logic would cascade it to bucket 1.

This diff fixes that, by reordering draining of current bucket and cascading timeouts.

Reviewed By: yfeldblum

Differential Revision: D13541506

fbshipit-source-id: 1284fca18612ae91f96538192bfad75e27cd816c
  • Loading branch information
spalamarchuk authored and facebook-github-bot committed Jan 3, 2019
1 parent c53bda7 commit 67cb8ee
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions folly/io/async/HHWheelTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ void HHWheelTimer::timeoutExpired() noexcept {
while (lastTick_ < nextTick) {
int idx = lastTick_ & 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);
}
}

auto bi = makeBitIterator(bitmap_.begin());
*(bi + idx) = false;

Expand All @@ -220,14 +228,6 @@ void HHWheelTimer::timeoutExpired() noexcept {
cbs->pop_front();
timeoutsToRunNow_.push_back(*cb);
}

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);
}
}
}

while (!timeoutsToRunNow_.empty()) {
Expand Down

0 comments on commit 67cb8ee

Please sign in to comment.