[go: up one dir, main page]

Struct Atomic

Source
pub struct Atomic<T> { /* private fields */ }
Expand description

An atomic pointer that can be safely shared between threads.

The pointer must be properly aligned. Since it is aligned, a tag can be stored into the unused least significant bits of the address.

Any method that loads the pointer must be passed a reference to a Scope.

Implementations§

Source§

impl<T> Atomic<T>

Source

pub fn null() -> Self

Returns a new null atomic pointer.

§Examples
use coco::epoch::Atomic;

let a = Atomic::<i32>::null();
Source

pub fn new(value: T) -> Self

Allocates value on the heap and returns a new atomic pointer pointing to it.

§Examples
use coco::epoch::Atomic;

let a = Atomic::new(1234);
Source

pub fn from_owned(owned: Owned<T>) -> Self

Returns a new atomic pointer pointing to owned.

§Examples
use coco::epoch::{Atomic, Owned};

let a = Atomic::from_owned(Owned::new(1234));
Source

pub fn from_ptr(ptr: Ptr<'_, T>) -> Self

Returns a new atomic pointer pointing to ptr.

§Examples
use coco::epoch::{Atomic, Ptr};

let a = Atomic::from_ptr(Ptr::<i32>::null());
Source

pub fn load<'scope>(&self, ord: Ordering, _: &'scope Scope) -> Ptr<'scope, T>

Loads a Ptr from the atomic pointer.

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

§Examples
use coco::epoch::{self, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
epoch::pin(|scope| {
    let p = a.load(SeqCst, scope);
});
Source

pub fn store(&self, new: Ptr<'_, T>, ord: Ordering)

Stores a Ptr into the atomic pointer.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
a.store(Ptr::null(), SeqCst);
Source

pub fn store_owned(&self, new: Owned<T>, ord: Ordering)

Stores an Owned into the atomic pointer.

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

§Examples
use coco::epoch::{self, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::null();
a.store_owned(Owned::new(1234), SeqCst);
Source

pub fn swap<'scope>( &self, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>

Stores a Ptr into the atomic pointer, returning the previous Ptr.

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

§Examples
use coco::epoch::{self, Atomic, Owned, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
epoch::pin(|scope| {
    let p = a.swap(Ptr::null(), SeqCst, scope);
});
Source

pub fn compare_and_swap<'scope>( &self, current: Ptr<'_, T>, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Result<(), Ptr<'scope, T>>

Stores new into the atomic pointer if the current value is the same as current.

The return value is a result indicating whether the new pointer was written. On failure the actual current value is returned.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);

epoch::pin(|scope| {
    let mut curr = a.load(SeqCst, scope);
    let res = a.compare_and_swap(curr, Ptr::null(), SeqCst, scope);
});
Source

pub fn compare_and_swap_weak<'scope>( &self, current: Ptr<'_, T>, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Result<(), Ptr<'scope, T>>

Stores new into the atomic pointer if the current value is the same as current.

Unlike compare_and_swap, this method is allowed to spuriously fail even when comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new pointer was written. On failure the actual current value is returned.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);

epoch::pin(|scope| {
    let mut curr = a.load(SeqCst, scope);
    loop {
        match a.compare_and_swap_weak(curr, Ptr::null(), SeqCst, scope) {
            Ok(()) => break,
            Err(c) => curr = c,
        }
    }
});
Source

pub fn compare_and_swap_owned<'scope>( &self, current: Ptr<'_, T>, new: Owned<T>, ord: Ordering, _: &'scope Scope, ) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>

Stores new into the atomic pointer if the current value is the same as current.

The return value is a result indicating whether the new pointer was written. On success the pointer that was written is returned. On failure new and the actual current value are returned.

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

§Examples
use coco::epoch::{self, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);

epoch::pin(|scope| {
    let mut curr = a.load(SeqCst, scope);
    let res = a.compare_and_swap_owned(curr, Owned::new(5678), SeqCst, scope);
});
Source

pub fn compare_and_swap_weak_owned<'scope>( &self, current: Ptr<'_, T>, new: Owned<T>, ord: Ordering, _: &'scope Scope, ) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>

Stores new into the atomic pointer if the current value is the same as current.

Unlike compare_and_swap_owned, this method is allowed to spuriously fail even when comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new pointer was written. On success the pointer that was written is returned. On failure new and the actual current value are returned.

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

§Examples
use coco::epoch::{self, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);

epoch::pin(|scope| {
    let mut new = Owned::new(5678);
    let mut ptr = a.load(SeqCst, scope);
    loop {
        match a.compare_and_swap_weak_owned(ptr, new, SeqCst, scope) {
            Ok(p) => {
                ptr = p;
                break;
            }
            Err((p, n)) => {
                ptr = p;
                new = n;
            }
        }
    }
});
Source

pub fn fetch_and<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>

Bitwise “and” with the current tag.

Performs a bitwise “and” operation on the current tag and the argument val, and sets the new tag to the result. Returns the previous pointer.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(3));
epoch::pin(|scope| {
    assert_eq!(a.fetch_and(2, SeqCst, scope).tag(), 3);
    assert_eq!(a.load(SeqCst, scope).tag(), 2);
});
Source

pub fn fetch_or<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>

Bitwise “or” with the current tag.

Performs a bitwise “or” operation on the current tag and the argument val, and sets the new tag to the result. Returns the previous pointer.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(1));
epoch::pin(|scope| {
    assert_eq!(a.fetch_or(2, SeqCst, scope).tag(), 1);
    assert_eq!(a.load(SeqCst, scope).tag(), 3);
});
Source

pub fn fetch_xor<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>

Bitwise “xor” with the current tag.

Performs a bitwise “xor” operation on the current tag and the argument val, and sets the new tag to the result. Returns the previous pointer.

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

§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(1));
epoch::pin(|scope| {
    assert_eq!(a.fetch_xor(3, SeqCst, scope).tag(), 1);
    assert_eq!(a.load(SeqCst, scope).tag(), 2);
});

Trait Implementations§

Source§

impl<T: Debug> Debug for Atomic<T>

Source§

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

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

impl<T> Default for Atomic<T>

Source§

fn default() -> Self

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

impl<T: Send + Sync> Send for Atomic<T>

Source§

impl<T: Send + Sync> Sync for Atomic<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Atomic<T>

§

impl<T> RefUnwindSafe for Atomic<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Atomic<T>

§

impl<T> UnwindSafe for Atomic<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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

Source§

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.