pub struct Shared<'g, T>{
data: usize,
_marker: PhantomData<(&'g (), *const T)>,
}
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.
Fields§
§data: usize
§_marker: PhantomData<(&'g (), *const T)>
Implementations§
pub fn as_raw(&self) -> *const T
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);
pub fn null() -> Shared<'g, T>
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());
pub fn is_null(&self) -> bool
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());
pub unsafe fn deref(&self) -> &'g T
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:
- A thread creates a new object:
a.store(Owned::new(10), Relaxed)
- 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);
}
pub unsafe fn deref_mut(&mut self) -> &'g mut T
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]);
}
pub unsafe fn as_ref(&self) -> Option<&'g T>
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:
- A thread creates a new object:
a.store(Owned::new(10), Relaxed)
- 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));
}
pub unsafe fn into_owned(self) -> Owned<T>
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());
}
pub unsafe fn try_into_owned(self) -> Option<Owned<T>>
pub unsafe fn try_into_owned(self) -> Option<Owned<T>>
Takes ownership of the pointee if it is not null.
§Safety
This method may be called only if the pointer is valid and nobody else is holding a reference to the same object, or if the pointer is null.
§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);
if let Some(x) = p.try_into_owned() {
drop(x);
}
}
pub fn tag(&self) -> usize
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);
pub fn with_tag(&self, tag: usize) -> Shared<'g, T>
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§
§fn partial_cmp(&self, other: &Shared<'g, T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Shared<'g, T>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§fn into_usize(self) -> usize
fn into_usize(self) -> usize
§unsafe fn from_usize(data: usize) -> Shared<'_, T>
unsafe fn from_usize(data: usize) -> Shared<'_, T>
data
. 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> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request