pub struct AtomicRaw<T> { /* private fields */ }Expand description
AtomicRaw does not own the underlying instance, allowing the user to control the lifetime of
instances of type T.
AtomicRaw does not own the pointed-to instance, and the user needs to convert the pointer
into an Owned through Owned::from_raw in order to
reclaim the memory.
Implementations§
Source§impl<T> AtomicRaw<T>
impl<T> AtomicRaw<T>
Sourcepub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> RawPtr<'g, T>
pub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> RawPtr<'g, T>
Loads a pointer value from the AtomicRaw.
§Examples
use std::sync::atomic::Ordering::Relaxed;
use sdd::{AtomicRaw, Guard, Owned};
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
let guard = Guard::new();
let owned = Owned::new(11);
let ptr = owned.into_raw();
atomic_raw.store(ptr, Relaxed);
assert_eq!(ptr, atomic_raw.load(Relaxed, &guard));
drop(unsafe { Owned::from_raw(ptr) });Sourcepub fn store(&self, ptr: RawPtr<'_, T>, order: Ordering)
pub fn store(&self, ptr: RawPtr<'_, T>, order: Ordering)
Stores a pointer value into the AtomicRaw.
§Examples
use std::sync::atomic::Ordering::Relaxed;
use sdd::{AtomicRaw, Owned};
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
let owned = Owned::new(11);
let ptr = owned.into_raw();
atomic_raw.store(ptr, Relaxed);
drop(unsafe { Owned::from_raw(ptr) });Sourcepub fn swap<'g>(
&self,
new: RawPtr<'g, T>,
order: Ordering,
_guard: &'g Guard,
) -> RawPtr<'g, T>
pub fn swap<'g>( &self, new: RawPtr<'g, T>, order: Ordering, _guard: &'g Guard, ) -> RawPtr<'g, T>
Stores the given value into the AtomicRaw and returns the original value.
§Examples
use std::sync::atomic::Ordering::Relaxed;
use sdd::{AtomicRaw, Guard, Owned};
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
let guard = Guard::new();
let owned = Owned::new(11);
let ptr = owned.into_raw();
atomic_raw.store(ptr, Relaxed);
let new_owned = Owned::new(17);
let new_ptr = new_owned.into_raw();
let swapped = atomic_raw.swap(new_ptr, Relaxed, &guard);
assert_eq!(unsafe { *Owned::from_raw(swapped).unwrap() }, 11);
drop(unsafe { Owned::from_raw(new_ptr) });Sourcepub fn fetch_update<'g, F: FnMut(RawPtr<'g, T>) -> Option<RawPtr<'g, T>>>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
_guard: &'g Guard,
) -> Result<RawPtr<'g, T>, RawPtr<'g, T>>
pub fn fetch_update<'g, F: FnMut(RawPtr<'g, T>) -> Option<RawPtr<'g, T>>>( &self, set_order: Ordering, fetch_order: Ordering, f: F, _guard: &'g Guard, ) -> Result<RawPtr<'g, T>, RawPtr<'g, T>>
Fetches the RawPtr, and applies a closure to it that returns an optional
new RawPtr.
The closure is applied to the current value. If it returns Some(new_ptr), the atomic
pointer is updated to new_ptr, and it returns Ok(prev_ptr) containing the previous
pointer held before the update. If the closure returns None, the update is not performed
and the current value is returned as an Err(prev_ptr)
§Errors
Returns the previous RawPtr if the closure returned None.
§Examples
use std::sync::atomic::Ordering::Relaxed;
use sdd::{AtomicRaw, Guard, Owned, RawPtr};
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
let guard = Guard::new();
let owned = Owned::new(11);
let ptr = owned.into_raw();
atomic_raw.store(ptr, Relaxed);
assert_eq!(atomic_raw.fetch_update(Relaxed, Relaxed, |_| None, &guard), Err(ptr));
assert_eq!(
atomic_raw.fetch_update(Relaxed, Relaxed, |_| Some(RawPtr::null()), &guard),
Ok(ptr)
);
drop(unsafe { Owned::from_raw(ptr) });Sourcepub fn compare_exchange<'g>(
&self,
current: RawPtr<'g, T>,
new: RawPtr<'g, T>,
success: Ordering,
failure: Ordering,
_guard: &'g Guard,
) -> Result<RawPtr<'g, T>, RawPtr<'g, T>>
pub fn compare_exchange<'g>( &self, current: RawPtr<'g, T>, new: RawPtr<'g, T>, success: Ordering, failure: Ordering, _guard: &'g Guard, ) -> Result<RawPtr<'g, T>, RawPtr<'g, T>>
Stores new into the AtomicRaw if the current RawPtr is the same as current.
Returns the previously held RawPtr.
§Errors
Returns the previous RawPtr if the supplied RawPtr did not equal to current.
§Examples
use sdd::{AtomicRaw, Guard, Owned, RawPtr};
use std::sync::atomic::Ordering::Relaxed;
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
let guard = Guard::new();
let owned = Owned::new(11);
let ptr = owned.into_raw();
assert_eq!(
unsafe { atomic_raw.compare_exchange(ptr, RawPtr::null(), Relaxed, Relaxed, &guard) },
Err(RawPtr::null())
);
assert_eq!(
unsafe { atomic_raw.compare_exchange(RawPtr::null(), ptr, Relaxed, Relaxed, &guard) },
Ok(RawPtr::null())
);
drop(unsafe { Owned::from_raw(ptr) });