Struct Deferred

Source
pub struct Deferred { /* private fields */ }
Expand description

A batch of pointers to be reclaimed in the future.

Sometimes it is necessary to defer the retirement of a batch of pointers. For example, a set of pointers may be reachable from multiple locations in a data structure and can only be retired after a specific object is reclaimed. In such cases, the Deferred type can serve as a cheap place to defer the retirement of pointers, without allocating extra memory.

Deferred is a concurrent list, meaning that pointers can be added from multiple threads concurrently. It is not meant to be used to amortize the cost of retirement, which is done through thread-local batches controlled with Collector::batch_size, as access from a single-thread can be more expensive than is required. Deferred batches are useful when you need to control when a batch of objects is retired directly, a relatively rare use case.

§Examples

let collector = Collector::new().batch_size(10);

// allocate a set of pointers
let items = (0..10)
    .map(|i| AtomicPtr::new(collector.link_boxed(i)))
    .collect::<Arc<[_]>>();

// create a batch of objects to retire
let mut batch = Deferred::new();

for item in items.iter() {
    // make the item unreachable with an atomic swap
    let old = item.swap(std::ptr::null_mut(), Ordering::AcqRel);
    // don't retire just yet, add the object to the batch
    unsafe { batch.defer(old) };
}

// sometime later... retire all the items in the batch
unsafe { batch.retire_all(&collector, reclaim::boxed::<Linked<usize>>) }

Implementations§

Source§

impl Deferred

Source

pub const fn new() -> Deferred

Create a new batch of deferred objects.

Source

pub unsafe fn defer<T: AsLink>(&self, ptr: *mut T)

Add an object to the batch.

§Safety

After this method is called, it is undefined behavior to add this pointer to the batch again, or any other batch. The pointer must also be valid for access as a Link, per the AsLink trait.

Source

pub unsafe fn retire_all( &mut self, collector: &Collector, reclaim: unsafe fn(*mut Link), )

Retires a batch of values, running reclaim when no threads hold a reference to any objects in the batch.

Note that this method is disconnected from any guards on the current thread, so the pointers may be reclaimed immediately.

§Safety

The safety requirements of Collector::retire apply to each object in the batch.

Source

pub fn for_each(&mut self, f: impl FnMut(*mut Node))

Run a function for each object in the batch.

This function does not consume the batch and can be called multiple times, before retirement.

Trait Implementations§

Source§

impl Default for Deferred

Source§

fn default() -> Deferred

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.