Struct sdd::AtomicShared
source · pub struct AtomicShared<T> { /* private fields */ }
Expand description
AtomicShared
owns the underlying instance, and allows users to perform atomic operations
on the pointer to it.
Implementations§
sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Creates a new AtomicShared
from an instance of T
.
The type of the instance must be determined at compile-time, must not contain non-static
references, and must not be a non-static reference since the instance can, theoretically,
live as long as the process. For instance, struct Disallowed<'l, T>(&'l T)
is not
allowed, because an instance of the type cannot outlive 'l
whereas the garbage collector
does not guarantee that the instance is dropped within 'l
.
§Examples
use sdd::AtomicShared;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(10);
sourcepub const fn from(shared: Shared<T>) -> Self
pub const fn from(shared: Shared<T>) -> Self
Creates a new AtomicShared
from a Shared
of T
.
§Examples
use sdd::{AtomicShared, Shared};
let shared: Shared<usize> = Shared::new(10);
let atomic_shared: AtomicShared<usize> = AtomicShared::from(shared);
sourcepub const fn null() -> Self
pub const fn null() -> Self
Creates a null AtomicShared
.
§Examples
use sdd::AtomicShared;
let atomic_shared: AtomicShared<usize> = AtomicShared::null();
sourcepub fn is_null(&self, order: Ordering) -> bool
pub fn is_null(&self, order: Ordering) -> bool
Returns true
if the AtomicShared
is null.
§Examples
use sdd::{AtomicShared, Tag};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::null();
atomic_shared.update_tag_if(Tag::Both, |p| p.tag() == Tag::None, Relaxed, Relaxed);
assert!(atomic_shared.is_null(Relaxed));
sourcepub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> Ptr<'g, T>
pub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> Ptr<'g, T>
Loads a pointer value from the AtomicShared
.
§Examples
use sdd::{AtomicShared, Guard};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(11);
let guard = Guard::new();
let ptr = atomic_shared.load(Relaxed, &guard);
assert_eq!(*ptr.as_ref().unwrap(), 11);
sourcepub fn swap(
&self,
new: (Option<Shared<T>>, Tag),
order: Ordering,
) -> (Option<Shared<T>>, Tag)
pub fn swap( &self, new: (Option<Shared<T>>, Tag), order: Ordering, ) -> (Option<Shared<T>>, Tag)
Stores the given value into the AtomicShared
and returns the original value.
§Examples
use sdd::{AtomicShared, Guard, Shared, Tag};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(14);
let guard = Guard::new();
let (old, tag) = atomic_shared.swap((Some(Shared::new(15)), Tag::Second), Relaxed);
assert_eq!(tag, Tag::None);
assert_eq!(*old.unwrap(), 14);
let (old, tag) = atomic_shared.swap((None, Tag::First), Relaxed);
assert_eq!(tag, Tag::Second);
assert_eq!(*old.unwrap(), 15);
let (old, tag) = atomic_shared.swap((None, Tag::None), Relaxed);
assert_eq!(tag, Tag::First);
assert!(old.is_none());
sourcepub fn update_tag_if<F: FnMut(Ptr<'_, T>) -> bool>(
&self,
tag: Tag,
condition: F,
set_order: Ordering,
fetch_order: Ordering,
) -> bool
pub fn update_tag_if<F: FnMut(Ptr<'_, 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 sdd::{AtomicShared, Tag};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::null();
assert!(atomic_shared.update_tag_if(Tag::Both, |p| p.tag() == Tag::None, Relaxed, Relaxed));
assert_eq!(atomic_shared.tag(Relaxed), Tag::Both);
sourcepub fn compare_exchange<'g>(
&self,
current: Ptr<'g, T>,
new: (Option<Shared<T>>, Tag),
success: Ordering,
failure: Ordering,
_guard: &'g Guard,
) -> Result<(Option<Shared<T>>, Ptr<'g, T>), (Option<Shared<T>>, Ptr<'g, T>)>
pub fn compare_exchange<'g>( &self, current: Ptr<'g, T>, new: (Option<Shared<T>>, Tag), success: Ordering, failure: Ordering, _guard: &'g Guard, ) -> Result<(Option<Shared<T>>, Ptr<'g, T>), (Option<Shared<T>>, Ptr<'g, T>)>
Stores new
into the AtomicShared
if the current value is the same as current
.
Returns the previously held value and the updated Ptr
.
§Errors
Returns Err
with the supplied Shared
and the current Ptr
.
§Examples
use sdd::{AtomicShared, Guard, Shared, Tag};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(17);
let guard = Guard::new();
let mut ptr = atomic_shared.load(Relaxed, &guard);
assert_eq!(*ptr.as_ref().unwrap(), 17);
atomic_shared.update_tag_if(Tag::Both, |_| true, Relaxed, Relaxed);
assert!(atomic_shared.compare_exchange(
ptr, (Some(Shared::new(18)), Tag::First), Relaxed, Relaxed, &guard).is_err());
ptr.set_tag(Tag::Both);
let old: Shared<usize> = atomic_shared.compare_exchange(
ptr,
(Some(Shared::new(18)), Tag::First),
Relaxed,
Relaxed,
&guard).unwrap().0.unwrap();
assert_eq!(*old, 17);
drop(old);
assert!(atomic_shared.compare_exchange(
ptr, (Some(Shared::new(19)), Tag::None), Relaxed, Relaxed, &guard).is_err());
assert_eq!(*ptr.as_ref().unwrap(), 17);
sourcepub fn compare_exchange_weak<'g>(
&self,
current: Ptr<'g, T>,
new: (Option<Shared<T>>, Tag),
success: Ordering,
failure: Ordering,
_guard: &'g Guard,
) -> Result<(Option<Shared<T>>, Ptr<'g, T>), (Option<Shared<T>>, Ptr<'g, T>)>
pub fn compare_exchange_weak<'g>( &self, current: Ptr<'g, T>, new: (Option<Shared<T>>, Tag), success: Ordering, failure: Ordering, _guard: &'g Guard, ) -> Result<(Option<Shared<T>>, Ptr<'g, T>), (Option<Shared<T>>, Ptr<'g, T>)>
Stores new
into the AtomicShared
if the current value is the same as current
.
This method is allowed to spuriously fail even when the comparison succeeds.
Returns the previously held value and the updated Ptr
.
§Errors
Returns Err
with the supplied Shared
and the current Ptr
.
§Examples
use sdd::{AtomicShared, Guard, Shared, Tag};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(17);
let guard = Guard::new();
let mut ptr = atomic_shared.load(Relaxed, &guard);
assert_eq!(*ptr.as_ref().unwrap(), 17);
while let Err((_, actual)) = atomic_shared.compare_exchange_weak(
ptr,
(Some(Shared::new(18)), Tag::First),
Relaxed,
Relaxed,
&guard) {
ptr = actual;
}
let mut ptr = atomic_shared.load(Relaxed, &guard);
assert_eq!(*ptr.as_ref().unwrap(), 18);
sourcepub fn clone(&self, order: Ordering, guard: &Guard) -> AtomicShared<T>
pub fn clone(&self, order: Ordering, guard: &Guard) -> AtomicShared<T>
Clones self
including tags.
If self
is not supposed to be an AtomicShared::null
, this will never return an
AtomicShared::null
.
§Examples
use sdd::{AtomicShared, Guard};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(59);
let guard = Guard::new();
let atomic_shared_clone = atomic_shared.clone(Relaxed, &guard);
let ptr = atomic_shared_clone.load(Relaxed, &guard);
assert_eq!(*ptr.as_ref().unwrap(), 59);
Tries to create a Shared
out of self
.
If self
is not supposed to be an AtomicShared::null
, this will never return None
.
§Examples
use sdd::{AtomicShared, Guard, Shared};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(47);
let guard = Guard::new();
let shared: Shared<usize> = atomic_shared.get_shared(Relaxed, &guard).unwrap();
assert_eq!(*shared, 47);
Converts self
into a Shared
.
Returns None
if self
did not hold a strong reference.
§Examples
use sdd::{AtomicShared, Shared};
use std::sync::atomic::Ordering::Relaxed;
let atomic_shared: AtomicShared<usize> = AtomicShared::new(55);
let shared: Shared<usize> = atomic_shared.into_shared(Relaxed).unwrap();
assert_eq!(*shared, 55);
Trait Implementations§
source§fn clone(&self) -> AtomicShared<T>
fn clone(&self) -> AtomicShared<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)