Skip to main content

Guard

Struct Guard 

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

Grants the current thread access to the managed heap.

Obtain one with pin() or Handle::pin. While the guard is alive you can allocate managed objects, load atomic pointers, and dereference the resulting Local references. A Guard is not a lock: any number of threads may hold their own guards at once. It only marks the thread as actively using the heap so the collector does not reclaim anything the thread might still reach, much like the read side of an RCU critical section.

§Keep guards short-lived

A long-lived guard prevents the collector from reclaiming garbage, so drop it as soon as you stop loading new pointers. To keep a reference past the guard, promote it to a Local<Handle, T> with Local::protect (hazard-pointer protected) or to a Shared<T> with Local::as_shared (root-count protected). The Local<Handle, T> form is generally cheaper to create.

§Examples

let guard = pin();                                // enter a critical section
let cell = AtomicShared::new(Node { value: 1 }, &guard);
let node = cell.load(Ordering::Acquire, &guard);  // load a pointer
assert_eq!(node.value, 1);                         // dereference it
drop(guard);                                       // let the collector proceed

Implementations§

Source§

impl Guard

Source

pub fn repin(&mut self)

Lets the collector make progress during a long-running loop.

Briefly unpins and re-pins the thread, refreshing the local epoch so the collector is not blocked indefinitely. Takes &mut self to statically ensure no Local<Guard, T> references are held across the call. To keep those references alive across this call, consider promoting them (see the struct-level docs).

Only effective when this is the sole active guard for the current thread.

§Examples
let mut guard = pin();
for _ in 0..3 {
    // ... a chunk of work using `&guard` ...
    guard.repin(); // let the collector make progress between chunks
}
Source

pub fn help_collect(&self)

Voluntarily assists the collector with pending work.

Called automatically during intensive allocation, but you can invoke it explicitly in long-running operations to help the collector make progress, for example sweeping dead objects or tracing live ones.

§Examples
let guard = pin();
// In a long loop that rarely allocates, lend the collector a hand.
guard.help_collect();

Trait Implementations§

Source§

impl Drop for Guard

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Protector for Guard

Source§

type Shield = ()

Source§

fn protect<T: TraceObj>(&self, _: ManPtr<T>) -> Self::Shield

Auto Trait Implementations§

§

impl !Freeze for Guard

§

impl !RefUnwindSafe for Guard

§

impl !Send for Guard

§

impl !Sync for Guard

§

impl !UnwindSafe for Guard

§

impl Unpin for Guard

§

impl UnsafeUnpin for Guard

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.