Struct atomic_ref::AtomicRef
[−]
[src]
pub struct AtomicRef<'a, T: 'a> { // some fields omitted }
A mutable Option<&'a, T> type which can be safely shared between threads.
Methods
impl<'a, T> AtomicRef<'a, T>
[src]
fn new(p: Option<&'a T>) -> AtomicRef<'a, T>
Creates a new AtomicRef
.
Examples
use atomic_ref::AtomicRef; static VALUE: i32 = 10; let atomic_ref = AtomicRef::new(Some(&VALUE));
fn load(&self, ordering: Ordering) -> Option<&'a T>
Loads the value stored in the AtomicRef
.
load
takes an Ordering
argument which describes the memory ordering of this operation.
Panics
Panics if order
is Release
or AcqRel
.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; let some_ref = AtomicRef::new(Some(&VALUE)); assert_eq!(some_ref.load(Ordering::Relaxed), Some(&10));
fn store(&self, ptr: Option<&'a T>, order: Ordering)
Stores a value into the AtomicRef
.
store
takes an Ordering
argument which describes the memory ordering of this operation.
Panics
Panics if order
is Acquire
or AcqRel
.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; let some_ptr = AtomicRef::new(None); some_ptr.store(Some(&VALUE), Ordering::Relaxed);
fn swap(&self, p: Option<&'a T>, order: Ordering) -> Option<&'a T>
Stores a value into the AtomicRef
, returning the old value.
swap
takes an Ordering
argument which describes the memory ordering of this operation.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; static OTHER_VALUE: i32 = 20; let some_ptr = AtomicRef::new(Some(&VALUE)); let value = some_ptr.swap(Some(&OTHER_VALUE), Ordering::Relaxed);
fn compare_and_swap(&self, current: Option<&'a T>, new: Option<&'a T>, order: Ordering) -> Option<&'a T>
Stores a value into the AtomicRef
if the current value is the "same" as
the current
value.
The return value is always the previous value. If it the "same" as
current
, then the value was updated.
This method considers two Option<&'a T>
s to be the "same" if they are
both Some
and have the same pointer value, or if they are both None
.
This method does not use Eq
or PartialEq
for comparison.
compare_and_swap
also takes an Ordering
argument which describes the
memory ordering of this operation.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; static OTHER_VALUE: i32 = 20; let some_ptr = AtomicRef::new(Some(&VALUE)); let value = some_ptr.compare_and_swap(Some(&OTHER_VALUE), None, Ordering::Relaxed);
fn compare_exchange(&self, current: Option<&'a T>, new: Option<&'a T>, success: Ordering, failure: Ordering) -> Result<Option<&'a T>, Option<&'a T>>
Stores a value into the AtomicRef
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 the "same" as new
.
This method considers two Option<&'a T>
s to be the "same" if they are
both Some
and have the same pointer value, or if they are both None
.
This method does not use Eq
or PartialEq
for comparison.
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.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; static OTHER_VALUE: i32 = 20; let some_ptr = AtomicRef::new(Some(&VALUE)); let value = some_ptr.compare_exchange(Some(&OTHER_VALUE), None, Ordering::SeqCst, Ordering::Relaxed);
fn compare_exchange_weak(&self, current: Option<&'a T>, new: Option<&'a T>, success: Ordering, failure: Ordering) -> Result<Option<&'a T>, Option<&'a T>>
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.
Examples
use std::sync::atomic::Ordering; use atomic_ref::AtomicRef; static VALUE: i32 = 10; static OTHER_VALUE: i32 = 20; let some_ptr = AtomicRef::new(Some(&VALUE)); let mut old = some_ptr.load(Ordering::Relaxed); loop { match some_ptr.compare_exchange_weak(old, Some(&VALUE), Ordering::SeqCst, Ordering::Relaxed) { Ok(_) => break, Err(x) => old = x, } }