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>
impl<T> Atomic<T>
Sourcepub fn new(ptr: *mut T) -> Self
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());Sourcepub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> Shared<'g, T>
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);Sourcepub fn store(&self, ptr: Shared<'_, T>, order: Ordering)
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);
}Sourcepub 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>>
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
);
}Sourcepub 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>>
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.
Trait Implementations§
impl<T: Send + Sync> Send for Atomic<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more