pub struct AtomicPtr<T>(AtomicPtr<T>);Expand description
A raw pointer type which can be safely shared between threads.
This type has the same size and bit validity as a *mut T.
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.
Tuple Fields§
§0: AtomicPtr<T>Implementations§
Source§impl<T> AtomicPtr<T>
impl<T> AtomicPtr<T>
Sourcepub const fn new(p: *mut T) -> AtomicPtr<T>
pub const fn new(p: *mut T) -> AtomicPtr<T>
Creates a new AtomicPtr.
§Examples
use keos::atomic::AtomicPtr;
let ptr = &mut 5;
let atomic_ptr = AtomicPtr::new(ptr);Sourcepub fn get_mut(&mut self) -> &mut *mut T
pub fn get_mut(&mut self) -> &mut *mut T
Returns a mutable reference to the underlying pointer.
This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.
§Examples
use keos::atomic::AtomicPtr;
let mut data = 10;
let mut atomic_ptr = AtomicPtr::new(&mut data);
let mut other_data = 5;
*atomic_ptr.get_mut() = &mut other_data;
assert_eq!(unsafe { *atomic_ptr.load() }, 5);Sourcepub const fn into_inner(self) -> *mut T
pub const 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.
§Examples
use keos::atomic::AtomicPtr;
let mut data = 5;
let atomic_ptr = AtomicPtr::new(&mut data);
assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);Sourcepub fn load(&self) -> *mut T
pub fn load(&self) -> *mut T
Loads a value from the pointer.
§Examples
use keos::atomic::AtomicPtr;
let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);
let value = some_ptr.load();Sourcepub fn store(&self, ptr: *mut T)
pub fn store(&self, ptr: *mut T)
Stores a value into the pointer.
§Examples
use keos::atomic::AtomicPtr;
let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);
let other_ptr = &mut 10;
some_ptr.store(other_ptr);Sourcepub fn swap(&self, ptr: *mut T) -> *mut T
pub fn swap(&self, ptr: *mut T) -> *mut T
Stores a value into the pointer, returning the previous value.
§Examples
use keos::atomic::AtomicPtr;
let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);
let other_ptr = &mut 10;
let value = some_ptr.swap(other_ptr);Sourcepub fn compare_exchange(
&self,
current: *mut T,
new: *mut T,
) -> Result<*mut T, *mut T>
pub fn compare_exchange( &self, current: *mut T, new: *mut T, ) -> Result<*mut T, *mut T>
Stores a value into the pointer if the current value is the same as the
current value.
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.
§Examples
use keos::atomic::AtomicPtr;
let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);
let other_ptr = &mut 10;
let value = some_ptr.compare_exchange(ptr, other_ptr);Sourcepub fn fetch_update<F>(&self, f: F) -> Result<*mut T, *mut T>where
F: FnMut(*mut T) -> Option<*mut T>,
pub fn fetch_update<F>(&self, f: F) -> Result<*mut T, *mut T>where
F: FnMut(*mut T) -> Option<*mut T>,
Fetches the value, and applies a function to it that returns an optional
new value. Returns a Result of Ok(previous_value) if the function
returned Some(_), else Err(previous_value).
Note: This may call the function multiple times if the value has been
changed from other threads in the meantime, as long as the function
returns Some(_), but the function will have been applied only once to
the stored value.
§Examples
use keos::atomic::AtomicPtr;
let ptr: *mut _ = &mut 5;
let some_ptr = AtomicPtr::new(ptr);
let new: *mut _ = &mut 10;
assert_eq!(some_ptr.fetch_update(, |_| None), Err(ptr));
let result = some_ptr.fetch_update(, |x| {
if x == ptr {
Some(new)
} else {
None
}
});
assert_eq!(result, Ok(ptr));
assert_eq!(some_ptr.load(), new);Sourcepub const fn as_ptr(&self) -> *mut *mut T
pub const fn as_ptr(&self) -> *mut *mut T
Returns a mutable pointer to the underlying pointer.
Doing non-atomic reads and writes on the resulting pointer can be a data
race. This method is mostly useful for FFI, where the function
signature may use *mut *mut T instead of &AtomicPtr<T>.
Returning an *mut pointer from a shared reference to this atomic is
safe because the atomic types work with interior mutability. All
modifications of an atomic change the value through a shared
reference, and can do so safely as long as they use atomic operations.
Any use of the returned raw pointer requires an unsafe block and
still has to uphold the same restriction: operations on it must be
atomic.
§Examples
use keos::atomic::AtomicPtr;
extern "C" {
fn my_atomic_op(arg: *mut *mut u32);
}
let mut value = 17;
let atomic = AtomicPtr::new(&mut value);
// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
my_atomic_op(atomic.as_ptr());
}