Struct scc::ebr::AtomicArc

source ·
pub struct AtomicArc<T> { /* private fields */ }
Expand description

AtomicArc owns the underlying instance, and allows users to perform atomic operations on the pointer to it.

Implementations§

source§

impl<T: 'static> AtomicArc<T>

source

pub fn new(t: T) -> Self

Creates a new AtomicArc from an instance of T.

The type of the instance must be determined at compile-time, must not contain non-static references, and must not be a non-static reference since the instance can, theoretically, live as long as the process. For instance, struct Disallowed<'l, T>(&'l T) is not allowed, because an instance of the type cannot outlive 'l whereas the garbage collector does not guarantee that the instance is dropped within 'l.

Examples
use scc::ebr::AtomicArc;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(10);
source§

impl<T> AtomicArc<T>

source

pub const fn from(arc: Arc<T>) -> Self

Creates a new AtomicArc from an Arc of T.

Examples
use scc::ebr::{Arc, AtomicArc};

let arc: Arc<usize> = Arc::new(10);
let atomic_arc: AtomicArc<usize> = AtomicArc::from(arc);
source

pub const fn null() -> Self

Creates a null AtomicArc.

Examples
use scc::ebr::AtomicArc;

let atomic_arc: AtomicArc<usize> = AtomicArc::null();
source

pub fn is_null(&self, order: Ordering) -> bool

Returns true if the AtomicArc is null.

Examples
use scc::ebr::{AtomicArc, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::null();
atomic_arc.update_tag_if(Tag::Both, |p| p.tag() == Tag::None, Relaxed, Relaxed);
assert!(atomic_arc.is_null(Relaxed));
source

pub fn load<'b>(&self, order: Ordering, _barrier: &'b Barrier) -> Ptr<'b, T>

Loads a pointer value from the AtomicArc.

Examples
use scc::ebr::{AtomicArc, Barrier};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(11);
let barrier = Barrier::new();
let ptr = atomic_arc.load(Relaxed, &barrier);
assert_eq!(*ptr.as_ref().unwrap(), 11);
source

pub fn swap( &self, new: (Option<Arc<T>>, Tag), order: Ordering ) -> (Option<Arc<T>>, Tag)

Stores the given value into the AtomicArc and returns the original value.

Examples
use scc::ebr::{Arc, AtomicArc, Barrier, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(14);
let barrier = Barrier::new();
let (old, tag) = atomic_arc.swap((Some(Arc::new(15)), Tag::Second), Relaxed);
assert_eq!(tag, Tag::None);
assert_eq!(*old.unwrap(), 14);
let (old, tag) = atomic_arc.swap((None, Tag::First), Relaxed);
assert_eq!(tag, Tag::Second);
assert_eq!(*old.unwrap(), 15);
let (old, tag) = atomic_arc.swap((None, Tag::None), Relaxed);
assert_eq!(tag, Tag::First);
assert!(old.is_none());
source

pub fn tag(&self, order: Ordering) -> Tag

Returns its Tag.

Examples
use scc::ebr::{AtomicArc, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::null();
assert_eq!(atomic_arc.tag(Relaxed), Tag::None);
source

pub fn update_tag_if<F: FnMut(Ptr<'_, T>) -> bool>( &self, tag: Tag, condition: F, set_order: Ordering, fetch_order: Ordering ) -> bool

Sets a new Tag if the given condition is met.

Returns true if the new Tag has been successfully set.

Examples
use scc::ebr::{AtomicArc, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::null();
assert!(atomic_arc.update_tag_if(Tag::Both, |p| p.tag() == Tag::None, Relaxed, Relaxed));
assert_eq!(atomic_arc.tag(Relaxed), Tag::Both);
source

pub fn compare_exchange<'b>( &self, current: Ptr<'b, T>, new: (Option<Arc<T>>, Tag), success: Ordering, failure: Ordering, _barrier: &'b Barrier ) -> Result<(Option<Arc<T>>, Ptr<'b, T>), (Option<Arc<T>>, Ptr<'b, T>)>

Stores new into the AtomicArc if the current value is the same as current.

Returns the previously held value and the updated Ptr.

Errors

Returns Err with the supplied Arc and the current Ptr.

Examples
use scc::ebr::{Arc, AtomicArc, Barrier, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(17);
let barrier = Barrier::new();

let mut ptr = atomic_arc.load(Relaxed, &barrier);
assert_eq!(*ptr.as_ref().unwrap(), 17);

atomic_arc.update_tag_if(Tag::Both, |_| true, Relaxed, Relaxed);
assert!(atomic_arc.compare_exchange(
    ptr, (Some(Arc::new(18)), Tag::First), Relaxed, Relaxed, &barrier).is_err());

ptr.set_tag(Tag::Both);
let old: Arc<usize> = atomic_arc.compare_exchange(
    ptr, (Some(Arc::new(18)), Tag::First), Relaxed, Relaxed, &barrier).unwrap().0.unwrap();
assert_eq!(*old, 17);
drop(old);

assert!(atomic_arc.compare_exchange(
    ptr, (Some(Arc::new(19)), Tag::None), Relaxed, Relaxed, &barrier).is_err());
assert_eq!(*ptr.as_ref().unwrap(), 17);
source

pub fn compare_exchange_weak<'b>( &self, current: Ptr<'b, T>, new: (Option<Arc<T>>, Tag), success: Ordering, failure: Ordering, _barrier: &'b Barrier ) -> Result<(Option<Arc<T>>, Ptr<'b, T>), (Option<Arc<T>>, Ptr<'b, T>)>

Stores new into the AtomicArc if the current value is the same as current.

This method is allowed to spuriously fail even when the comparison succeeds.

Returns the previously held value and the updated Ptr.

Errors

Returns Err with the supplied Arc and the current Ptr.

Examples
use scc::ebr::{Arc, AtomicArc, Barrier, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(17);
let barrier = Barrier::new();

let mut ptr = atomic_arc.load(Relaxed, &barrier);
assert_eq!(*ptr.as_ref().unwrap(), 17);

while let Err((_, actual)) = atomic_arc.compare_exchange_weak(
    ptr,
    (Some(Arc::new(18)), Tag::First),
    Relaxed,
    Relaxed,
    &barrier) {
    ptr = actual;
}

let mut ptr = atomic_arc.load(Relaxed, &barrier);
assert_eq!(*ptr.as_ref().unwrap(), 18);
source

pub fn clone(&self, order: Ordering, _barrier: &Barrier) -> AtomicArc<T>

Clones self including tags.

If self is not supposed to be an AtomicArc::null, this will never return an AtomicArc::null.

Examples
use scc::ebr::{Arc, AtomicArc, Barrier};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(59);
let barrier = Barrier::new();
let atomic_arc_clone = atomic_arc.clone(Relaxed, &barrier);
let ptr = atomic_arc_clone.load(Relaxed, &barrier);
assert_eq!(*ptr.as_ref().unwrap(), 59);
source

pub fn get_arc(&self, order: Ordering, _barrier: &Barrier) -> Option<Arc<T>>

Tries to create an Arc out of self.

If self is not supposed to be an AtomicArc::null, this will never return None.

Examples
use scc::ebr::{Arc, AtomicArc, Barrier};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(47);
let barrier = Barrier::new();
let arc: Arc<usize> = atomic_arc.get_arc(Relaxed, &barrier).unwrap();
assert_eq!(*arc, 47);
source

pub fn try_into_arc(self, order: Ordering) -> Option<Arc<T>>

Tries to convert self into an Arc.

Examples
use scc::ebr::{Arc, AtomicArc};
use std::sync::atomic::Ordering::Relaxed;

let atomic_arc: AtomicArc<usize> = AtomicArc::new(55);
let arc: Arc<usize> = atomic_arc.try_into_arc(Relaxed).unwrap();
assert_eq!(*arc, 55);

Trait Implementations§

source§

impl<T> Clone for AtomicArc<T>

source§

fn clone(&self) -> AtomicArc<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for AtomicArc<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Default for AtomicArc<T>

source§

fn default() -> Self

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

impl<T> Drop for AtomicArc<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: Send> Send for AtomicArc<T>

source§

impl<T: Sync> Sync for AtomicArc<T>

source§

impl<T: UnwindSafe> UnwindSafe for AtomicArc<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for AtomicArc<T>

§

impl<T> Unpin for AtomicArc<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.