Struct atomic_ref::AtomicRef [] [src]

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

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

Methods

impl<'a, T> AtomicRef<'a, T>
[src]

Creates a new AtomicRef.

Examples

use atomic_ref::AtomicRef;

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

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

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

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

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

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

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

Trait Implementations

impl<'a, T: Debug> Debug for AtomicRef<'a, T>
[src]

Formats the value using the given formatter.

impl<'a, T> Default for AtomicRef<'a, T>
[src]

Returns the "default value" for a type. Read more