Skip to content

Commit

Permalink
Fix an irregularity in BackgroundCleaner::ScheduleCleaning() with
Browse files Browse the repository at this point in the history
`absl::InfiniteFuture()`: replace the existing scheduled cleaning (to do
nothing) instead of always doing nothing.

PiperOrigin-RevId: 609042280
  • Loading branch information
QrczakMK committed Feb 21, 2024
1 parent eb8a80b commit 2dd2c84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 14 additions & 3 deletions riegeli/base/background_cleaning.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void BackgroundCleaner::Unregister(Token token) {
void BackgroundCleaner::CancelCleaning(Token token) {
absl::MutexLock lock(&mutex_);
CancelCleaningInternal(token);
// Move `token.iter()` anywhere before `next_`.
if (next_ == token.iter()) {
++next_;
} else {
Expand All @@ -64,11 +65,21 @@ inline void BackgroundCleaner::CancelCleaningInternal(Token token) {
&args));
}

void BackgroundCleaner::ScheduleCleaningSlow(Token token, absl::Time deadline) {
void BackgroundCleaner::ScheduleCleaning(Token token, absl::Time deadline) {
absl::MutexLock lock(&mutex_);

// Update `entries_` by moving `token.iter()` into the right place, and update
// Update `entries_` by moving `token.iter()` to the right place, and update
// `next_`.

if (deadline == absl::InfiniteFuture()) {
// Move `token.iter()` anywhere before `next_`.
if (next_ == token.iter()) {
++next_;
} else {
entries_.splice(entries_.begin(), entries_, token.iter());
}
return;
}

Entries::iterator iter = entries_.end();
for (;;) {
if (iter == next_) {
Expand Down
12 changes: 2 additions & 10 deletions riegeli/base/background_cleaning.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class BackgroundCleaner {

// Schedules cleaning the cleanee corresponding to `token` at `deadline`.
//
// Does nothing if `deadline == absl::InfiniteFuture()`. If `deadline` is in
// the past, cleaning will be scheduled.
// If `deadline` is `absl::InfiniteFuture()`, cleaning will never happen.
// If `deadline` is in the past, cleaning will be scheduled immediately.
//
// If `ScheduleCleaning()` is called again for the same cleanee with a pending
// cleaning, its deadline is replaced.
Expand All @@ -131,8 +131,6 @@ class BackgroundCleaner {
void CancelCleaningInternal(Token token)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);

void ScheduleCleaningSlow(Token token, absl::Time deadline);

void BackgroundThread();

absl::Mutex mutex_;
Expand All @@ -157,12 +155,6 @@ class BackgroundCleaner {

// Implementation details follow.

inline void BackgroundCleaner::ScheduleCleaning(Token token,
absl::Time deadline) {
if (deadline == absl::InfiniteFuture()) return;
ScheduleCleaningSlow(token, deadline);
}

inline absl::Time BackgroundCleaner::TimeNow() { return absl::Now(); }

} // namespace riegeli
Expand Down

0 comments on commit 2dd2c84

Please sign in to comment.