pub struct AtomSetOnce<P>where
P: IntoRawPtr + FromRawPtr,{ /* 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>where
P: IntoRawPtr + FromRawPtr,
impl<P> AtomSetOnce<P>where
P: IntoRawPtr + FromRawPtr,
Sourcepub fn empty() -> AtomSetOnce<P>
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}
Sourcepub fn new(value: P) -> AtomSetOnce<P>
pub fn new(value: P) -> AtomSetOnce<P>
Create a new AtomSetOnce
from Pointer P
Sourcepub fn set_if_none(&self, v: P, order: Ordering) -> Option<P>
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§impl<T, P> AtomSetOnce<P>
impl<T, P> AtomSetOnce<P>
Sourcepub fn get(&self, order: Ordering) -> Option<&T>
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>>
impl<T> AtomSetOnce<Box<T>>
Source§impl<T> AtomSetOnce<T>
impl<T> AtomSetOnce<T>
Trait Implementations§
Source§impl<P> Debug for AtomSetOnce<P>
impl<P> Debug for AtomSetOnce<P>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more