Skip to content

Commit

Permalink
Ensure crossbeam-epoch to run GC when dropping a cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuya6502 committed Jan 20, 2024
1 parent 8488c49 commit 761899c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
21 changes: 12 additions & 9 deletions src/future/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ impl<K, V, S> Drop for BaseCache<K, V, S> {
// The housekeeper needs to be dropped before the inner is dropped.
std::mem::drop(self.housekeeper.take());

// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}
if Arc::strong_count(&self.inner) <= 1 {
dbg!(Arc::strong_count(&self.inner));
// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}

// NOTE: inner, read_op_ch, and write_op_ch will be dropped after returning
// from this `drop` method. They use crossbeam-epoch internally, but we do
// not have to call `flush` for them because their `drop` methods do not
// create `deferred_fn`s, and drop their values in place.
// NOTE: The `inner` will be dropped after returning from this `drop`
// method. It uses crossbeam-epoch internally, but we do not have to call
// `flush` for it because its `drop` methods do not create
// `deferred_fn`s, and drop its values in place.
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/sync_base/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ impl<K, V, S> Drop for BaseCache<K, V, S> {
// The housekeeper needs to be dropped before the inner is dropped.
std::mem::drop(self.housekeeper.take());

// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}
if Arc::strong_count(&self.inner) <= 1 {
// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}

// NOTE: inner, read_op_ch, and write_op_ch will be dropped after returning
// from this `drop` method. They use crossbeam-epoch internally, but we do
// not have to call `flush` for them because their `drop` methods do not
// create `deferred_fn`s, and drop their values in place.
// NOTE: The `inner` will be dropped after returning from this `drop`
// method. It uses crossbeam-epoch internally, but we do not have to call
// `flush` for it because its `drop` methods do not create
// `deferred_fn`s, and drop its values in place.
}
}
}

Expand Down

0 comments on commit 761899c

Please sign in to comment.