Skip to content

Commit

Permalink
Fix lastWakeup_ accounting in ThrottledLifoSem
Browse files Browse the repository at this point in the history
Summary: `lastWakeup_` should be updated with the timestamp *after* the sleep, otherwise the next sleep will be 0. This means that currently only every other thread in a chain of wake-ups waits for the wakeup interval.

Reviewed By: philippv

Differential Revision: D38684505

fbshipit-source-id: 02f6e033083b58a8d878c73b087395938bd78327
  • Loading branch information
ot authored and facebook-github-bot committed Aug 14, 2022
1 parent 1cb51d4 commit 46c03de
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions folly/synchronization/ThrottledLifoSem.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,18 @@ class ThrottledLifoSem {
std::is_same<Clock, std::chrono::steady_clock>::value
? now
: std::chrono::steady_clock::now();
const auto nextWakeup =
std::exchange(lastWakeup_, steadyNow) + options_.wakeUpInterval;
const auto nextWakeup = lastWakeup_ + options_.wakeUpInterval;
const auto sleep = std::min(
nextWakeup - steadyNow,
std::chrono::duration_cast<std::chrono::nanoseconds>(
deadline - now));
if (sleep.count() > 0) {
/* sleep override */ std::this_thread::sleep_for(sleep);
// Update with the intended wake-up time instead of the current time,
// so if sleep_for oversleeps we'll correct in the next sleep.
lastWakeup_ = steadyNow + sleep;
} else {
lastWakeup_ = steadyNow;
}
}

Expand Down

0 comments on commit 46c03de

Please sign in to comment.