AtomicPtr

Struct AtomicPtr 

Source
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>

Source

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);
Source

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);
Source

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);
Source

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();
Source

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);
Source

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);
Source

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);
Source

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);
Source

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());
}

Trait Implementations§

Source§

impl<T: Default> Default for AtomicPtr<T>

Source§

fn default() -> AtomicPtr<T>

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

impl<T> From<*mut T> for AtomicPtr<T>

Source§

fn from(p: *mut T) -> Self

Converts a *mut T into an AtomicPtr<T>.

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicPtr<T>

§

impl<T> RefUnwindSafe for AtomicPtr<T>

§

impl<T> Send for AtomicPtr<T>

§

impl<T> Sync for AtomicPtr<T>

§

impl<T> Unpin for AtomicPtr<T>

§

impl<T> UnwindSafe for AtomicPtr<T>
where T: RefUnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.