Skip to main content

AtomicRaw

Struct AtomicRaw 

Source
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>

Source

pub const fn new(ptr: RawPtr<'_, T>) -> Self

Creates a new AtomicRaw from a RawPtr.

§Examples
use sdd::{AtomicRaw, Owned};

let owned = Owned::new(11);
let ptr = owned.into_raw();
let atomic_raw: AtomicRaw<usize> = AtomicRaw::new(ptr);
drop(unsafe { Owned::from_raw(ptr) });
Source

pub const fn null() -> Self

Creates a null AtomicRaw.

§Examples
use sdd::AtomicRaw;

let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
Source

pub fn is_null(&self, order: Ordering) -> bool

Returns true if the AtomicRaw is null.

§Examples
use std::sync::atomic::Ordering::Relaxed;

use sdd::{AtomicRaw, Tag};

let atomic_raw: AtomicRaw<usize> = AtomicRaw::null();
assert!(atomic_raw.is_null(Relaxed));
Source

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) });
Source

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) });
Source

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) });
Source

pub fn tag(&self, order: Ordering) -> Tag

Returns its Tag.

§Examples
use sdd::{AtomicRaw, Tag};
use std::sync::atomic::Ordering::Relaxed;

let atomic_owned: AtomicRaw<usize> = AtomicRaw::null();
assert_eq!(atomic_owned.tag(Relaxed), Tag::None);
Source

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) });
Source

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) });

Trait Implementations§

Source§

impl<T: Debug> Debug for AtomicRaw<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for AtomicRaw<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Send> Send for AtomicRaw<T>

Source§

impl<T: Send + Sync> Sync for AtomicRaw<T>

Source§

impl<T: UnwindSafe> UnwindSafe for AtomicRaw<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicRaw<T>

§

impl<T> RefUnwindSafe for AtomicRaw<T>

§

impl<T> Unpin for AtomicRaw<T>

§

impl<T> UnsafeUnpin for AtomicRaw<T>

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.