Struct atomic_ref::AtomicRef[][src]

#[repr(C)]
pub struct AtomicRef<'a, T: 'a> { /* fields omitted */ }
Expand description

A mutable Option<&'a T> type which can be safely shared between threads.

Implementations

Creates a new AtomicRef.

Examples

use atomic_ref::AtomicRef;

static VALUE: i32 = 10;
let atomic_ref = AtomicRef::new(Some(&VALUE));

Returns a mutable reference to the underlying Option<&'a T>.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

Examples

use std::sync::atomic::Ordering;
use atomic_ref::AtomicRef;

let value: i32 = 10;
let value2: i32 = 20;

let mut some_ref = AtomicRef::new(Some(&value));
assert_eq!(*some_ref.get_mut(), Some(&value));
*some_ref.get_mut() = Some(&value2);
assert_eq!(some_ref.load(Ordering::SeqCst), Some(&value2));

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 atomic_ref::AtomicRef;

let some_ref = AtomicRef::new(Some(&5));
assert_eq!(some_ref.into_inner(), Some(&5));

Loads the value stored in the AtomicRef.

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

Calls with ordering weaker than Acquire will be performed with Acquire ordering.

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::Acquire), Some(&10));

Stores a value into the AtomicRef.

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

Calls with ordering weaker than Release will be performed with Release ordering.

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::SeqCst);

Stores a value into the AtomicRef, returning the old value.

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

Calls with ordering weaker than AcqRel will be performed with AcqRel 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.swap(Some(&OTHER_VALUE), Ordering::SeqCst);

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.

Calls with ordering weaker than AcqRel will be performed with AcqRel 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_and_swap(Some(&OTHER_VALUE), None, Ordering::SeqCst);

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.

Calls with a success ordering weaker than AcqRel or failure ordering weaker than Acquire will be performed with those orderings.

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::Acquire);

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.

Calls with a success ordering weaker than AcqRel or failure ordering weaker than Acquire will be performed with those orderings.

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::Acquire);
loop {
    match some_ptr.compare_exchange_weak(old, Some(&VALUE),
                                         Ordering::SeqCst, Ordering::Acquire) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}

Trait Implementations

Formats the value using the given formatter. Read more

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

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.