Struct haphazard::HazardPointerArray
source · pub struct HazardPointerArray<'domain, F, const N: usize> { /* private fields */ }
Expand description
An efficient way to obtain and hold N
HazardPointer
s.
Construct one either with
let _: HazardPointerArray<haphazard::Global, 4> = HazardPointerArray::default();
or using HazardPointer::many
/HazardPointer::many_in_domain
:
let _ = HazardPointer::many::<4>();
let _ = HazardPointer::many_in_domain::<4>(haphazard::Domain::global());
To use the individual hazard pointers, use HazardPointerArray::as_refs
, or protect multiple
AtomicPtr
s using HazardPointerArray::protect_all
.
Implementations§
source§impl<'domain, F, const N: usize> HazardPointerArray<'domain, F, N>
impl<'domain, F, const N: usize> HazardPointerArray<'domain, F, N>
sourcepub fn as_refs<'array>(
&'array mut self
) -> [&'array mut HazardPointer<'domain, F>; N]
pub fn as_refs<'array>(
&'array mut self
) -> [&'array mut HazardPointer<'domain, F>; N]
Reference the N
allocated HazardPointer
s.
If you’re curious why you can’t just slice directly into HazardPointerArray
, it’s because
doing so would mutable borrow the entire array, which would make the other pointers
unusable. The borrow checker knows that individual elements in a [_; N]
are distinct, and
can be borrowed individually, but does not know that that is the case for SomeType[i]
.
sourcepub unsafe fn protect_all<'l, T>(
&'l mut self,
sources: [&AtomicPtr<T>; N]
) -> [Option<&'l T>; N]where
T: Sync,
F: 'static,
pub unsafe fn protect_all<'l, T>(
&'l mut self,
sources: [&AtomicPtr<T>; N]
) -> [Option<&'l T>; N]where
T: Sync,
F: 'static,
Protect the value loaded from each AtomicPtr
, and dereference each one to &T
.
The order of the returned references matches the order of sources
.
This operation will load each AtomicPtr
multiple times:
- load to get the currently stored pointer,
ptr
- store
ptr
into the hazard pointer to protect it from reclamation - load again to check that the pointer didn’t change between 1 and 2.
if it did, set the loaded value to
ptr
and goto 2.
Produces None
at a given index if the loaded pointer for that AtomicPtr
was null.
Safety
- The values loaded each
AtomicPtr
are all valid&T
, or null. - The loaded
&T
will only be deallocated through calls toretire
functions on the sameDomain
as this holder is associated with.
sourcepub fn reset_protection(&mut self)
pub fn reset_protection(&mut self)
Release the protection awarded by the contained hazard pointers, if any.
If the hazard pointer array was protecting any objects, those objects may now be reclaimed when retired (assuming they aren’t protected by any other hazard pointers).