Skip to main content

Atomic

Struct Atomic 

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

A pointer to a heap-allocated value with atomic operations.

This type provides atomic load, store, and compare-exchange operations on pointers to T. Memory reclamation is handled automatically through the guard-based protocol.

§Examples

use kovan::{Atomic, pin};
use std::sync::atomic::Ordering;

let atomic = Atomic::new(Box::into_raw(Box::new(42)));
let guard = pin();
let ptr = atomic.load(Ordering::Acquire, &guard);

Implementations§

Source§

impl<T> Atomic<T>

Source

pub fn new(ptr: *mut T) -> Self

Creates a new atomic pointer.

§Examples
use kovan::Atomic;

let atomic: Atomic<i32> = Atomic::new(std::ptr::null_mut());
Source

pub fn null() -> Self

Creates a null atomic pointer.

Source

pub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> Shared<'g, T>

Loads a pointer from the atomic.

This operation has zero overhead - it’s just a single atomic load.

§Examples
use kovan::{Atomic, pin};
use std::sync::atomic::Ordering;

let atomic = Atomic::new(Box::into_raw(Box::new(42)));
let guard = pin();
let ptr = atomic.load(Ordering::Acquire, &guard);
Source

pub fn store(&self, ptr: Shared<'_, T>, order: Ordering)

Stores a pointer into the atomic.

§Examples
use kovan::{Atomic, Shared};
use std::sync::atomic::Ordering;

let atomic = Atomic::new(std::ptr::null_mut());
let ptr = Box::into_raw(Box::new(42));
unsafe {
    atomic.store(Shared::from_raw(ptr), Ordering::Release);
}
Source

pub fn compare_exchange<'g>( &self, current: Shared<'_, T>, new: Shared<'_, T>, success: Ordering, failure: Ordering, _guard: &'g Guard, ) -> Result<Shared<'g, T>, Shared<'g, T>>

Compares and exchanges the pointer.

§Examples
use kovan::{Atomic, Shared, pin};
use std::sync::atomic::Ordering;

let atomic = Atomic::new(Box::into_raw(Box::new(42)));
let guard = pin();
let current = atomic.load(Ordering::Acquire, &guard);
let new = Box::into_raw(Box::new(43));
unsafe {
    atomic.compare_exchange(
        current,
        Shared::from_raw(new),
        Ordering::AcqRel,
        Ordering::Acquire,
        &guard
    );
}
Source

pub fn compare_exchange_weak<'g>( &self, current: Shared<'_, T>, new: Shared<'_, T>, success: Ordering, failure: Ordering, _guard: &'g Guard, ) -> Result<Shared<'g, T>, Shared<'g, T>>

Compares and exchanges the pointer (weak version).

This version may spuriously fail even when the comparison succeeds.

Source

pub fn swap<'g>( &self, new: Shared<'_, T>, order: Ordering, _guard: &'g Guard, ) -> Shared<'g, T>

Swaps the pointer with a new value.

Trait Implementations§

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