Struct Atom

Source
pub struct Atom<P>{ /* private fields */ }
Expand description

An Atom wraps an AtomicPtr, it allows for safe mutation of an atomic into common Rust Types.

Implementations§

Source§

impl<P> Atom<P>

Source

pub fn empty() -> Atom<P>

Create a empty Atom

Examples found in repository?
examples/simple.rs (line 9)
7fn main() {
8    // Create an empty atom
9    let shared_atom = Arc::new(Atom::empty());
10
11    // set the value 75
12    shared_atom.swap(Box::new(75), Ordering::AcqRel);
13
14    // Spawn a bunch of thread that will try and take the value
15    let threads: Vec<thread::JoinHandle<()>> = (0..8)
16        .map(|_| {
17            let shared_atom = shared_atom.clone();
18            thread::spawn(move || {
19                // Take the contents of the atom, only one will win the race
20                if let Some(v) = shared_atom.take(Ordering::Acquire) {
21                    println!("I got it: {:?} :D", v);
22                } else {
23                    println!("I did not get it :(");
24                }
25            })
26        })
27        .collect();
28
29    // join the threads
30    for t in threads {
31        t.join().unwrap();
32    }
33}
Source

pub fn new(value: P) -> Atom<P>

Create a new Atomic from Pointer P

Source

pub fn swap(&self, v: P, order: Ordering) -> Option<P>

Swap a new value into the Atom, This will try multiple times until it succeeds. The old value will be returned.

Examples found in repository?
examples/simple.rs (line 12)
7fn main() {
8    // Create an empty atom
9    let shared_atom = Arc::new(Atom::empty());
10
11    // set the value 75
12    shared_atom.swap(Box::new(75), Ordering::AcqRel);
13
14    // Spawn a bunch of thread that will try and take the value
15    let threads: Vec<thread::JoinHandle<()>> = (0..8)
16        .map(|_| {
17            let shared_atom = shared_atom.clone();
18            thread::spawn(move || {
19                // Take the contents of the atom, only one will win the race
20                if let Some(v) = shared_atom.take(Ordering::Acquire) {
21                    println!("I got it: {:?} :D", v);
22                } else {
23                    println!("I did not get it :(");
24                }
25            })
26        })
27        .collect();
28
29    // join the threads
30    for t in threads {
31        t.join().unwrap();
32    }
33}
Source

pub fn take(&self, order: Ordering) -> Option<P>

Take the value of the Atom replacing it with null pointer Returning the contents. If the contents was a null pointer the result will be None.

Examples found in repository?
examples/fifo.rs (line 32)
30    fn drop(&mut self) {
31        // This is done to avoid a recusive drop of the List
32        while let Some(mut h) = self.next.atom().take(Ordering::Acquire) {
33            self.next = mem::replace(&mut h.next, AtomSetOnce::empty());
34        }
35    }
More examples
Hide additional examples
examples/simple.rs (line 20)
7fn main() {
8    // Create an empty atom
9    let shared_atom = Arc::new(Atom::empty());
10
11    // set the value 75
12    shared_atom.swap(Box::new(75), Ordering::AcqRel);
13
14    // Spawn a bunch of thread that will try and take the value
15    let threads: Vec<thread::JoinHandle<()>> = (0..8)
16        .map(|_| {
17            let shared_atom = shared_atom.clone();
18            thread::spawn(move || {
19                // Take the contents of the atom, only one will win the race
20                if let Some(v) = shared_atom.take(Ordering::Acquire) {
21                    println!("I got it: {:?} :D", v);
22                } else {
23                    println!("I did not get it :(");
24                }
25            })
26        })
27        .collect();
28
29    // join the threads
30    for t in threads {
31        t.join().unwrap();
32    }
33}
Source

pub fn set_if_none(&self, v: P, order: Ordering) -> Option<P>

This will do a CAS setting the value only if it is NULL this will return None if the value was written, otherwise a Some(v) will be returned, where the value was the same value that you passed into this function

Source

pub fn replace_and_set_next( &self, value: P, load_order: Ordering, cas_order: Ordering, ) -> bool
where P: GetNextMut<NextPtr = Option<P>>,

Take the current content, write it into P then do a CAS to extent this Atom with the previous contents. This can be used to create a LIFO

Returns true if this set this migrated the Atom from null.

Source

pub fn is_none(&self, order: Ordering) -> bool

Check to see if an atom is None

This only means that the contents was None when it was measured

Source§

impl<P, T> Atom<P>
where P: IntoRawPtr + FromRawPtr + Deref<Target = T>,

Source

pub fn compare_and_swap( &self, current: Option<&P>, new: Option<P>, order: Ordering, ) -> Result<Option<P>, (Option<P>, *mut P)>

Stores new in the Atom if current has the same raw pointer representation as the currently stored value.

On success, the Atom’s previous value is returned. On failure, new is returned together with a raw pointer to the Atom’s current unchanged value, which is not safe to dereference, especially if the Atom is accessed from multiple threads.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation.

Source

pub fn compare_exchange( &self, current: Option<&P>, new: Option<P>, success: Ordering, failure: Ordering, ) -> Result<Option<P>, (Option<P>, *mut P)>

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.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering if the operation succeeds while the second describes the required ordering when the operation fails. The failure ordering can’t be Release or AcqRel and must be equivalent or weaker than the success ordering.

Source

pub fn compare_exchange_weak( &self, current: Option<&P>, new: Option<P>, success: Ordering, failure: Ordering, ) -> Result<Option<P>, (Option<P>, *mut P)>

Stores a value into the pointer if the current value is the same as the current value.

Unlike 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.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering if the operation succeeds while the second describes the required ordering when the operation fails. The failure ordering can’t be Release or AcqRel and must be equivalent or weaker than the success ordering.

Trait Implementations§

Source§

impl<P> Debug for Atom<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<P> Drop for Atom<P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<P> Send for Atom<P>
where P: IntoRawPtr + FromRawPtr + Send,

Source§

impl<P> Sync for Atom<P>
where P: IntoRawPtr + FromRawPtr + Send,

Auto Trait Implementations§

§

impl<P> !Freeze for Atom<P>

§

impl<P> !RefUnwindSafe for Atom<P>

§

impl<P> Unpin for Atom<P>
where P: Unpin,

§

impl<P> UnwindSafe for Atom<P>
where P: UnwindSafe,

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, 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.