crossbeam::epoch

Struct Shared

Source
pub struct Shared<'g, T>
where T: 'g + Pointable + ?Sized,
{ /* private fields */ }
Expand description

A pointer to an object protected by the epoch GC.

The pointer is valid for use only during the lifetime 'g.

The pointer must be properly aligned. Since it is aligned, a tag can be stored into the unused least significant bits of the address.

Implementations§

Source§

impl<'g, T> Shared<'g, T>

Source

pub fn as_raw(&self) -> *const T

Converts the pointer to a raw pointer (without the tag).

§Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let o = Owned::new(1234);
let raw = &*o as *const _;
let a = Atomic::from(o);

let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
assert_eq!(p.as_raw(), raw);
Source§

impl<'g, T> Shared<'g, T>
where T: Pointable + ?Sized,

Source

pub fn null() -> Shared<'g, T>

Returns a new null pointer.

§Examples
use crossbeam_epoch::Shared;

let p = Shared::<i32>::null();
assert!(p.is_null());
Source

pub fn is_null(&self) -> bool

Returns true if the pointer is null.

§Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::null();
let guard = &epoch::pin();
assert!(a.load(SeqCst, guard).is_null());
a.store(Owned::new(1234), SeqCst);
assert!(!a.load(SeqCst, guard).is_null());
Source

pub unsafe fn deref(&self) -> &'g T

Dereferences the pointer.

Returns a reference to the pointee that is valid during the lifetime 'g.

§Safety

Dereferencing a pointer is unsafe because it could be pointing to invalid memory.

Another concern is the possibility of data races due to lack of proper synchronization. For example, consider the following scenario:

  1. A thread creates a new object: a.store(Owned::new(10), Relaxed)
  2. Another thread reads it: *a.load(Relaxed, guard).as_ref().unwrap()

The problem is that relaxed orderings don’t synchronize initialization of the object with the read from the second thread. This is a data race. A possible solution would be to use Release and Acquire orderings.

§Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.deref(), &1234);
}
Source

pub unsafe fn deref_mut(&mut self) -> &'g mut T

Dereferences the pointer.

Returns a mutable reference to the pointee that is valid during the lifetime 'g.

§Safety
  • There is no guarantee that there are no more threads attempting to read/write from/to the actual object at the same time.

    The user must know that there are no concurrent accesses towards the object itself.

  • Other than the above, all safety concerns of deref() applies here.

§Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(vec![1, 2, 3, 4]);
let guard = &epoch::pin();

let mut p = a.load(SeqCst, guard);
unsafe {
    assert!(!p.is_null());
    let b = p.deref_mut();
    assert_eq!(b, &vec![1, 2, 3, 4]);
    b.push(5);
    assert_eq!(b, &vec![1, 2, 3, 4, 5]);
}

let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.deref(), &vec![1, 2, 3, 4, 5]);
}
Source

pub unsafe fn as_ref(&self) -> Option<&'g T>

Converts the pointer to a reference.

Returns None if the pointer is null, or else a reference to the object wrapped in Some.

§Safety

Dereferencing a pointer is unsafe because it could be pointing to invalid memory.

Another concern is the possibility of data races due to lack of proper synchronization. For example, consider the following scenario:

  1. A thread creates a new object: a.store(Owned::new(10), Relaxed)
  2. Another thread reads it: *a.load(Relaxed, guard).as_ref().unwrap()

The problem is that relaxed orderings don’t synchronize initialization of the object with the read from the second thread. This is a data race. A possible solution would be to use Release and Acquire orderings.

§Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
unsafe {
    assert_eq!(p.as_ref(), Some(&1234));
}
Source

pub unsafe fn into_owned(self) -> Owned<T>

Takes ownership of the pointee.

§Panics

Panics if this pointer is null, but only in debug mode.

§Safety

This method may be called only if the pointer is valid and nobody else is holding a reference to the same object.

§Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(1234);
unsafe {
    let guard = &epoch::unprotected();
    let p = a.load(SeqCst, guard);
    drop(p.into_owned());
}
Source

pub fn tag(&self) -> usize

Returns the tag stored within the pointer.

§Examples
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::<u64>::from(Owned::new(0u64).with_tag(2));
let guard = &epoch::pin();
let p = a.load(SeqCst, guard);
assert_eq!(p.tag(), 2);
Source

pub fn with_tag(&self, tag: usize) -> Shared<'g, T>

Returns the same pointer, but tagged with tag. tag is truncated to be fit into the unused bits of the pointer to T.

§Examples
use crossbeam_epoch::{self as epoch, Atomic};
use std::sync::atomic::Ordering::SeqCst;

let a = Atomic::new(0u64);
let guard = &epoch::pin();
let p1 = a.load(SeqCst, guard);
let p2 = p1.with_tag(2);

assert_eq!(p1.tag(), 0);
assert_eq!(p2.tag(), 2);
assert_eq!(p1.as_raw(), p2.as_raw());

Trait Implementations§

Source§

impl<T> Clone for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

fn clone(&self) -> Shared<'_, T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

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

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

impl<T> Default for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

fn default() -> Shared<'_, T>

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

impl<T> From<*const T> for Shared<'_, T>

Source§

fn from(raw: *const T) -> Shared<'_, T>

Returns a new pointer pointing to raw.

§Panics

Panics if raw is not properly aligned.

§Examples
use crossbeam_epoch::Shared;

let p = Shared::from(Box::into_raw(Box::new(1234)) as *const _);
assert!(!p.is_null());
Source§

impl<'g, T> From<Shared<'g, T>> for Atomic<T>
where T: Pointable + ?Sized,

Source§

fn from(ptr: Shared<'g, T>) -> Atomic<T>

Returns a new atomic pointer pointing to ptr.

§Examples
use crossbeam_epoch::{Atomic, Shared};

let a = Atomic::<i32>::from(Shared::<i32>::null());
Source§

impl<T> Ord for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

fn cmp(&self, other: &Shared<'_, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'g, T> PartialEq for Shared<'g, T>
where T: Pointable + ?Sized,

Source§

fn eq(&self, other: &Shared<'g, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'g, T> PartialOrd for Shared<'g, T>
where T: Pointable + ?Sized,

Source§

fn partial_cmp(&self, other: &Shared<'g, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Pointer<T> for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

fn into_usize(self) -> usize

Returns the machine representation of the pointer.
Source§

unsafe fn from_usize(data: usize) -> Shared<'_, T>

Returns a new pointer pointing to the tagged pointer data. Read more
Source§

impl<T> Pointer for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

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

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

impl<T> Copy for Shared<'_, T>
where T: Pointable + ?Sized,

Source§

impl<T> Eq for Shared<'_, T>
where T: Pointable + ?Sized,

Auto Trait Implementations§

§

impl<'g, T> Freeze for Shared<'g, T>
where T: ?Sized,

§

impl<'g, T> RefUnwindSafe for Shared<'g, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'g, T> !Send for Shared<'g, T>

§

impl<'g, T> !Sync for Shared<'g, T>

§

impl<'g, T> Unpin for Shared<'g, T>
where T: ?Sized,

§

impl<'g, T> UnwindSafe for Shared<'g, T>
where T: RefUnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.