Struct AtomSetOnce

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

This is a restricted version of the Atom. It allows for only set_if_none to be called.

swap and take can be used only with a mutable reference. Meaning that AtomSetOnce is not usable as a

Implementations§

Source§

impl<P> AtomSetOnce<P>

Source

pub fn empty() -> AtomSetOnce<P>

Create an empty AtomSetOnce

Examples found in repository?
examples/fifo.rs (line 33)
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    }
36}
37
38fn main() {
39    let b = Arc::new(Barrier::new(THREADS + 1));
40
41    let head = Arc::new(Link {
42        next: AtomSetOnce::empty(),
43    });
44
45    for _ in 0..THREADS {
46        let b = b.clone();
47        let head = head.clone();
48        thread::spawn(move || {
49            let mut hptr = &*head;
50
51            for _ in 0..10_000 {
52                let mut my_awesome_node = Box::new(Link {
53                    next: AtomSetOnce::empty(),
54                });
55
56                loop {
57                    while let Some(h) = hptr.next.get(Ordering::Acquire) {
58                        hptr = h;
59                    }
60
61                    my_awesome_node =
62                        match hptr.next.set_if_none(my_awesome_node, Ordering::Release) {
63                            Some(v) => v,
64                            None => break,
65                        };
66                }
67            }
68            b.wait();
69        });
70    }
71
72    b.wait();
73
74    let mut hptr = &*head;
75    let mut count = 0;
76    while let Some(h) = hptr.next.get(Ordering::Acquire) {
77        hptr = h;
78        count += 1;
79    }
80    println!(
81        "Using {} threads we wrote {} links at the same time!",
82        THREADS, count
83    );
84}
Source

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

Create a new AtomSetOnce from Pointer P

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 OK(()) if the value was written, otherwise a Err(P) will be returned, where the value was the same value that you passed into this function

Examples found in repository?
examples/fifo.rs (line 62)
38fn main() {
39    let b = Arc::new(Barrier::new(THREADS + 1));
40
41    let head = Arc::new(Link {
42        next: AtomSetOnce::empty(),
43    });
44
45    for _ in 0..THREADS {
46        let b = b.clone();
47        let head = head.clone();
48        thread::spawn(move || {
49            let mut hptr = &*head;
50
51            for _ in 0..10_000 {
52                let mut my_awesome_node = Box::new(Link {
53                    next: AtomSetOnce::empty(),
54                });
55
56                loop {
57                    while let Some(h) = hptr.next.get(Ordering::Acquire) {
58                        hptr = h;
59                    }
60
61                    my_awesome_node =
62                        match hptr.next.set_if_none(my_awesome_node, Ordering::Release) {
63                            Some(v) => v,
64                            None => break,
65                        };
66                }
67            }
68            b.wait();
69        });
70    }
71
72    b.wait();
73
74    let mut hptr = &*head;
75    let mut count = 0;
76    while let Some(h) = hptr.next.get(Ordering::Acquire) {
77        hptr = h;
78        count += 1;
79    }
80    println!(
81        "Using {} threads we wrote {} links at the same time!",
82        THREADS, count
83    );
84}
Source

pub fn into_atom(self) -> Atom<P>

Convert an AtomSetOnce into an Atom

Source

pub fn atom(&mut self) -> &mut Atom<P>

Allow access to the atom if exclusive access is granted

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    }
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<T, P> AtomSetOnce<P>
where P: IntoRawPtr + FromRawPtr + Deref<Target = T>,

Source

pub fn get(&self, order: Ordering) -> Option<&T>

If the Atom is set, get the value

Examples found in repository?
examples/fifo.rs (line 57)
38fn main() {
39    let b = Arc::new(Barrier::new(THREADS + 1));
40
41    let head = Arc::new(Link {
42        next: AtomSetOnce::empty(),
43    });
44
45    for _ in 0..THREADS {
46        let b = b.clone();
47        let head = head.clone();
48        thread::spawn(move || {
49            let mut hptr = &*head;
50
51            for _ in 0..10_000 {
52                let mut my_awesome_node = Box::new(Link {
53                    next: AtomSetOnce::empty(),
54                });
55
56                loop {
57                    while let Some(h) = hptr.next.get(Ordering::Acquire) {
58                        hptr = h;
59                    }
60
61                    my_awesome_node =
62                        match hptr.next.set_if_none(my_awesome_node, Ordering::Release) {
63                            Some(v) => v,
64                            None => break,
65                        };
66                }
67            }
68            b.wait();
69        });
70    }
71
72    b.wait();
73
74    let mut hptr = &*head;
75    let mut count = 0;
76    while let Some(h) = hptr.next.get(Ordering::Acquire) {
77        hptr = h;
78        count += 1;
79    }
80    println!(
81        "Using {} threads we wrote {} links at the same time!",
82        THREADS, count
83    );
84}
Source§

impl<T> AtomSetOnce<Box<T>>

Source

pub fn get_mut(&mut self, order: Ordering) -> Option<&mut T>

If the Atom is set, get the value

Source§

impl<T> AtomSetOnce<T>

Source

pub fn dup(&self, order: Ordering) -> Option<T>

Duplicate the inner pointer if it is set

Trait Implementations§

Source§

impl<P> Debug for AtomSetOnce<P>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P> !Freeze for AtomSetOnce<P>

§

impl<P> !RefUnwindSafe for AtomSetOnce<P>

§

impl<P> Send for AtomSetOnce<P>
where P: Send,

§

impl<P> Sync for AtomSetOnce<P>
where P: Send,

§

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

§

impl<P> UnwindSafe for AtomSetOnce<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.