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.
Since AtomicRaw does not own the pointed-to instance, the user needs to convert the pointer
into an Owned through Owned::from_raw in order to
reclaim the memory.
Additionally, memory ordering is not automatically enforced: for example,
swap allows relaxed operations, while AtomicOwned
and AtomicShared implicitly upgrade Relaxed to
Release.
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>
Atomically stores the given value into the AtomicRaw, returning the previous 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 update_tag_if<F: FnMut(RawPtr<'_, T>) -> bool>(
&self,
tag: Tag,
condition: F,
set_order: Ordering,
fetch_order: Ordering,
) -> bool
pub fn update_tag_if<F: FnMut(RawPtr<'_, 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 std::sync::atomic::Ordering::Relaxed;
use sdd::{AtomicRaw, Tag};
let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
assert!(atomic_raw.update_tag_if(Tag::Both, |p| p.tag() == Tag::None, Relaxed, Relaxed));
assert_eq!(atomic_raw.tag(Relaxed), Tag::Both);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, wrapped in Err.
§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>>
Atomically stores new into the AtomicRaw if the current RawPtr matches current.
Returns the previously held RawPtr.
§Errors
Returns the previous RawPtr if the supplied RawPtr did not match current, wrapped in Err.
§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) });