Expand description
A managed pointer type which can be safely shared between threads.
This type has the same in-memory representation as a std::sync::atomic::AtomicPtr
, but
provides additional functionality and stricter guarantees:
haphazard::AtomicPtr
can safely load&T
directly, and ensure that the referencedT
is not deallocated until the&T
is dropped, even in the presence of concurrent writers.- All loads and stores on this type use
Acquire
andRelease
semantics.
Note: This type is only available on platforms that support atomic loads and stores of pointers. Its size depends on the target pointer’s size.
Basic usage
To construct one, use AtomicPtr::from
:
let ptr: AtomicPtr<usize> = AtomicPtr::from(Box::new(42));
// Remember to retire the item stored in the AtomicPtr when it's dropped
// (assuming of course that the pointer is not also shared elsewhere):
unsafe { ptr.retire(); }
Note the explicit use of AtomicPtr<usize>
, which is needed to get the default values for the
generic arguments F
and P
, the domain family and pointer types of the stored values.
Families are discussed in the documentation for Domain
. The pointer type P
, which must
implement raw::Pointer
, is the type originaly used to produce the stored pointer. This is
used to ensure that when writers drop a value, it is dropped using the appropriate Drop
implementation.
Warning: When this type is dropped, it does not automatically retire the object it is
currently pointing to. In order to retire (and eventually reclaim) that object, use
AtomicPtr::retire
or AtomicPtr::retire_in
.
Implementations§
source§impl<T, F, P> AtomicPtr<T, F, P>
impl<T, F, P> AtomicPtr<T, F, P>
sourcepub unsafe fn new(p: *mut T) -> Self
pub unsafe fn new(p: *mut T) -> Self
Directly construct an AtomicPtr
from a raw pointer.
Safety
p
must reference a validT
, or be null.p
must have been allocated using the pointer typeP
.*p
must only be dropped throughDomain::retire_ptr
.
sourcepub unsafe fn as_std(&self) -> &AtomicPtr<T>
pub unsafe fn as_std(&self) -> &AtomicPtr<T>
Directly access the “real” underlying AtomicPtr
.
Safety
If the stored pointer is modified, the new value must conform to the same safety
requirements as the argument to AtomicPtr::new
.
sourcepub unsafe fn as_std_mut(&mut self) -> &mut AtomicPtr<T>
pub unsafe fn as_std_mut(&mut self) -> &mut AtomicPtr<T>
Directly access the “real” underlying AtomicPtr
mutably.
Safety
If the stored pointer is modified, the new value must conform to the same safety
requirements as the argument to AtomicPtr::new
.
source§impl<T, F, P> AtomicPtr<T, F, P>where
F: Singleton,
impl<T, F, P> AtomicPtr<T, F, P>where
F: Singleton,
sourcepub fn safe_load<'hp, 'd>(
&self,
hp: &'hp mut HazardPointer<'d, F>
) -> Option<&'hp T>where
T: Sync + 'hp,
F: 'static,
pub fn safe_load<'hp, 'd>(
&self,
hp: &'hp mut HazardPointer<'d, F>
) -> Option<&'hp T>where
T: Sync + 'hp,
F: 'static,
Loads the value from the stored pointer and guards it using the given hazard pointer.
The guard ensures that the loaded T
will remain valid for as long as you hold a reference
to it.
This method is only available for domains with singleton families, because
implementations of the unsafe Singleton
trait guarantee that there is only ever one
instance of a Domain
with the family in question, which in turn implies that loads and
stores using such a family must be using the same (single) instance of that domain.
source§impl<T, F, P> AtomicPtr<T, F, P>
impl<T, F, P> AtomicPtr<T, F, P>
sourcepub unsafe fn load<'hp, 'd>(
&self,
hp: &'hp mut HazardPointer<'d, F>
) -> Option<&'hp T>where
T: Sync + 'hp,
F: 'static,
pub unsafe fn load<'hp, 'd>(
&self,
hp: &'hp mut HazardPointer<'d, F>
) -> Option<&'hp T>where
T: Sync + 'hp,
F: 'static,
Loads the value from the stored pointer and guards it using the given hazard pointer.
The guard ensures that the loaded T
will remain valid for as long as you hold a reference
to it.
Safety
All objects stored in this AtomicPtr
are retired through the same Domain
as the one
that produced hp
.
This requirement is partially enforced by the domain family (F
), but it’s on you to
ensure that you don’t “cross the streams” between multiple Domain<F>
, if those can arise
in your application.
sourcepub unsafe fn get_mut(&mut self) -> &mut *mut T
pub unsafe fn get_mut(&mut self) -> &mut *mut T
Returns a mutable reference to the underlying pointer.
Safety
If the stored pointer is modified, the new value must conform to the same safety
requirements as the argument to AtomicPtr::new
.
sourcepub fn into_inner(self) -> *mut T
pub fn into_inner(self) -> *mut T
Consumes the atomic and returns the contained value.
This is safe because passing self
by value guarantees that no other threads are
concurrently accessing the atomic data, and no loads can happen in the future.
source§impl<T, P> AtomicPtr<T, Global, P>where
P: Pointer<T>,
impl<T, P> AtomicPtr<T, Global, P>where
P: Pointer<T>,
sourcepub unsafe fn retire(self) -> usizewhere
T: Send,
pub unsafe fn retire(self) -> usizewhere
T: Send,
Retire the currently-referenced object, and reclaim it once it is safe to do so.
T
must be Send
since it may be reclaimed by a different thread.
Safety
- The currently-referenced object will never again be returned by any
AtomicPtr::load
. - The currently-referenced object has not already been retired.
source§impl<T, F, P> AtomicPtr<T, F, P>where
P: Pointer<T>,
impl<T, F, P> AtomicPtr<T, F, P>where
P: Pointer<T>,
sourcepub unsafe fn retire_in(self, domain: &Domain<F>) -> usizewhere
T: Send,
pub unsafe fn retire_in(self, domain: &Domain<F>) -> usizewhere
T: Send,
Retire the currently-referenced object, and reclaim it once it is safe to do so, through
the given domain
.
T
must be Send
since it may be reclaimed by a different thread.
Safety
- The currently-referenced object will never again be returned by any
AtomicPtr::load
. - The currently-referenced object has not already been retired.
- All calls to
load
that can have seen the currently-referenced object were using hazard pointers fromdomain
.
Note that requirement #3 is partially enforced by the domain family (F
), but it’s on
you to ensure that you don’t “cross the streams” between multiple Domain<F>
, if those can
arise in your application.
sourcepub fn store(&self, p: P)
pub fn store(&self, p: P)
Store an object into the pointer.
Note, crucially, that this will not automatically retire the pointer that’s currently stored, which is why it is safe.
sourcepub fn swap(&self, p: P) -> Option<Replaced<T, F, P>>
pub fn swap(&self, p: P) -> Option<Replaced<T, F, P>>
Overwrite the currently stored pointer with the given one, and return the previous pointer.
sourcepub fn compare_exchange(
&self,
current: *mut T,
new: P
) -> Result<Option<Replaced<T, F, P>>, P>
pub fn compare_exchange(
&self,
current: *mut T,
new: P
) -> Result<Option<Replaced<T, F, P>>, P>
Stores an object into the pointer if the current pointer is current
.
The return value is a result indicating whether the new value was written and containing
the previous value. On success this value is guaranteed to be equal to current
.
sourcepub fn compare_exchange_weak(
&self,
current: *mut T,
new: P
) -> Result<Option<Replaced<T, F, P>>, P>
pub fn compare_exchange_weak(
&self,
current: *mut T,
new: P
) -> Result<Option<Replaced<T, F, P>>, P>
Stores an object into the pointer if the current pointer is current
.
Unlike AtomicPtr::compare_exchange
, this function is allowed to spuriously fail even
when the comparison succeeds, which can result in more efficient code on some platforms.
The return value is a result indicating whether the new value was written and containing
the previous value. On success this value is guaranteed to be equal to current
.
source§impl<T, F, P> AtomicPtr<T, F, P>
impl<T, F, P> AtomicPtr<T, F, P>
sourcepub unsafe fn store_ptr(&self, ptr: *mut T)
pub unsafe fn store_ptr(&self, ptr: *mut T)
Overwrite the currently stored pointer with the given one.
Note, crucially, that this will not automatically retire the pointer that’s currently stored.
Safety
ptr
must conform to the same safety requirements as the argument to AtomicPtr::new
.
sourcepub unsafe fn swap_ptr(&self, ptr: *mut T) -> Option<Replaced<T, F, P>>
pub unsafe fn swap_ptr(&self, ptr: *mut T) -> Option<Replaced<T, F, P>>
Overwrite the currently stored pointer with the given one, and return the previous pointer.
Safety
ptr
must conform to the same safety requirements as the argument to AtomicPtr::new
.
sourcepub unsafe fn compare_exchange_ptr(
&self,
current: *mut T,
new: *mut T
) -> Result<Option<Replaced<T, F, P>>, *mut T>
pub unsafe fn compare_exchange_ptr(
&self,
current: *mut T,
new: *mut T
) -> Result<Option<Replaced<T, F, P>>, *mut T>
Stores new
if the current pointer is current
.
The return value is a result indicating whether the new pointer was written and containing
the previous pointer. On success this value is guaranteed to be equal to current
.
Safety
ptr
must conform to the same safety requirements as the argument to AtomicPtr::new
.
sourcepub unsafe fn compare_exchange_weak_ptr(
&self,
current: *mut T,
new: *mut T
) -> Result<Option<Replaced<T, F, P>>, *mut T>
pub unsafe fn compare_exchange_weak_ptr(
&self,
current: *mut T,
new: *mut T
) -> Result<Option<Replaced<T, F, P>>, *mut T>
Stores new
if the current pointer is current
.
Unlike AtomicPtr::compare_exchange_ptr
, this function is allowed to spuriously fail even
when the comparison succeeds, which can result in more efficient code on some platforms.
The return value is a result indicating whether the new pointer was written and containing
the previous pointer. On success this value is guaranteed to be equal to current
.
Safety
ptr
must conform to the same safety requirements as the argument to AtomicPtr::new
.