Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {Atomic, Shared}::try_into_owned #701

Merged
merged 3 commits into from Jul 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 76 additions & 0 deletions crossbeam-epoch/src/atomic.rs
Expand Up @@ -809,6 +809,52 @@ impl<T: ?Sized + Pointable> Atomic<T> {
Owned::from_usize(self.data.into_inner())
}
}

/// Takes ownership of the pointee if it is non-null.
///
/// This consumes the atomic and converts it into [`Owned`]. As [`Atomic`] doesn't have a
/// destructor and doesn't drop the pointee while [`Owned`] does, this is suitable for
/// destructors of data structures.
///
/// # Safety
///
/// This method may be called only if the pointer is valid and nobody else is holding a
/// reference to the same object, or the pointer is null..
taiki-e marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```rust
/// # use std::mem;
/// # use crossbeam_epoch::Atomic;
/// struct DataStructure {
/// ptr: Atomic<usize>,
/// }
///
/// impl Drop for DataStructure {
/// fn drop(&mut self) {
/// // By now the DataStructure lives only in our thread and we are sure we don't hold
/// // any Shared or & to it ourselves, but it may be null, so we have to be careful.
/// let old = mem::replace(&mut self.ptr, Atomic::null());
/// unsafe {
/// if let Option::Some(x) = old.try_into_owned() {
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
/// drop(x)
/// }
/// }
/// }
/// }
/// ```
pub unsafe fn try_into_owned(self) -> Option<Owned<T>> {
// FIXME: See self.into_owned()
#[cfg(crossbeam_loom)]
let data = self.data.unsync_load();
#[cfg(not(crossbeam_loom))]
let data = self.data.into_inner();
if decompose_tag::<T>(data).0 == 0 {
Option::None
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
} else {
Option::Some(Owned::from_usize(data))
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl<T: ?Sized + Pointable> fmt::Debug for Atomic<T> {
Expand Down Expand Up @@ -1419,6 +1465,36 @@ impl<'g, T: ?Sized + Pointable> Shared<'g, T> {
Owned::from_usize(self.data)
}

/// 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 Option::Some(x) = p.try_into_owned() {
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
/// drop(x);
/// }
/// }
/// ```
pub unsafe fn try_into_owned(self) -> Option<Owned<T>> {
if self.is_null() {
Option::None
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
} else {
Option::Some(Owned::from_usize(self.data))
PatrickNorton marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Returns the tag stored within the pointer.
///
/// # Examples
Expand Down