Skip to content

Commit

Permalink
Adding Manual Reclaim Stratedy (#1)
Browse files Browse the repository at this point in the history
* Adding manual reclaim strategy and exposing public reclaim method on domain.

* Applying rust format

* Fix typo in documentation of reclaim method
  • Loading branch information
Johnabell authored Nov 8, 2021
1 parent 9230b96 commit 9dc7f47
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atom_box"
version = "0.1.2"
version = "0.1.3"
edition = "2018"
authors = ["John Bell <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Atom Box

Atom Box provides an atomic box implementation where the box owns the contained value and its underlying allocation.
It is therefore, responsible for deallocating the memory when it is no longer being access by any threads.
It is therefore, responsible for deallocating the memory when it is no longer being accessed by any threads.

Under the covers Atom Box is 'a safe implementation of hazard pointers in rust.'

Expand Down
20 changes: 20 additions & 0 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ On nightly this will panic if the domain id is equal to the shared domain's id (
)
}

/// Reclaim all unprotected retired items.
///
///
/// # Example
///
/// ```
/// use atom_box::{AtomBox, domain::{Domain, ReclaimStrategy}};
///
/// const CUSTOM_DOMAIN_ID: usize = 42;
/// static CUSTOM_DOMAIN: Domain<CUSTOM_DOMAIN_ID> = Domain::new(ReclaimStrategy::Manual);
///
/// let atom_box = AtomBox::new_with_domain("Hello World", &CUSTOM_DOMAIN);
/// atom_box.swap("Goodbye World");
///
/// CUSTOM_DOMAIN.reclaim();
/// ```
pub fn reclaim(&self) -> usize {
self.bulk_reclaim()
}

fn bulk_reclaim(&self) -> usize {
let retired_list = self
.retired
Expand Down
6 changes: 6 additions & 0 deletions src/domain/reclaim_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const DEFAULT_HAZARD_POINTER_MULTIPLIER: isize = 2;
/// A `default` const constructor function is defined for this enum. It cannot implement `Default`
/// since we would like the `default` constructor to be a const function.
#[derive(Debug)]
#[non_exhaustive]
pub enum ReclaimStrategy {
/// Every time an item is retired the domain will try to reclaim any items which are not
/// currently being protected by a hazard pointer.
Expand All @@ -19,6 +20,10 @@ pub enum ReclaimStrategy {
/// Items will be reclaimed both periodically, and when the number of retired items exceeds
/// certain thresholds.
TimedCapped(TimedCappedSettings),

/// Memory reclaimation will only happen when the `reclaim` method on [`crate::domain::Domain`]
/// is called.
Manual,
}

impl ReclaimStrategy {
Expand All @@ -28,6 +33,7 @@ impl ReclaimStrategy {
Self::TimedCapped(settings) => {
settings.should_reclaim(hazard_pointer_count, retired_count)
}
Self::Manual => false,
}
}

Expand Down

0 comments on commit 9dc7f47

Please sign in to comment.