-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 Arc::into_inner
for safely discarding Arc
s without calling the destructor on the inner type.
#79665
Add Arc::into_inner
for safely discarding Arc
s without calling the destructor on the inner type.
#79665
Changes from all commits
6a2e8e7
657b534
a0740b8
fbedcd5
a7bdb90
5d81f76
00d50fe
ca7066d
948e402
7d43bcb
1e28132
81623f4
f57de56
cd444ca
ecb85f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -485,6 +485,20 @@ impl<T> Arc<T> { | |||||
/// | ||||||
/// This will succeed even if there are outstanding weak references. | ||||||
/// | ||||||
// FIXME: when `Arc::into_inner` is stabilized, add this paragraph: | ||||||
/* | ||||||
/// It is strongly recommended to use [`Arc::into_inner`] instead if you don't | ||||||
/// want to keep the `Arc` in the [`Err`] case. | ||||||
/// Immediately dropping the [`Err`] payload, like in the expression | ||||||
/// `Arc::try_unwrap(this).ok()`, can still cause the strong count to | ||||||
/// drop to zero and the inner value of the `Arc` to be dropped: | ||||||
/// For instance if two threads execute this expression in parallel, then | ||||||
/// there is a race condition. The threads could first both check whether they | ||||||
/// have the last clone of their `Arc` via `Arc::try_unwrap`, and then | ||||||
/// both drop their `Arc` in the call to [`ok`][`Result::ok`], | ||||||
/// taking the strong count from two down to zero. | ||||||
/// | ||||||
*/ | ||||||
/// # Examples | ||||||
/// | ||||||
/// ``` | ||||||
|
@@ -516,6 +530,130 @@ impl<T> Arc<T> { | |||||
Ok(elem) | ||||||
} | ||||||
} | ||||||
|
||||||
/// Returns the inner value, if the `Arc` has exactly one strong reference. | ||||||
/// | ||||||
/// Otherwise, [`None`] is returned and the `Arc` is dropped. | ||||||
/// | ||||||
/// This will succeed even if there are outstanding weak references. | ||||||
/// | ||||||
/// If `Arc::into_inner` is called on every clone of this `Arc`, | ||||||
/// it is guaranteed that exactly one of the calls returns the inner value. | ||||||
/// This means in particular that the inner value is not dropped. | ||||||
/// | ||||||
/// The similar expression `Arc::try_unwrap(this).ok()` does not | ||||||
/// offer such a guarantee. See the last example below and the documentation | ||||||
/// of [`Arc::try_unwrap`]. | ||||||
/// | ||||||
/// # Examples | ||||||
/// | ||||||
/// Minimal example demonstrating the guarantee that `Arc::into_inner` gives. | ||||||
/// ``` | ||||||
/// #![feature(arc_into_inner)] | ||||||
/// | ||||||
/// use std::sync::Arc; | ||||||
/// | ||||||
/// let x = Arc::new(3); | ||||||
/// let y = Arc::clone(&x); | ||||||
/// | ||||||
/// // Two threads calling `Arc::into_inner` on both clones of an `Arc`: | ||||||
/// let x_unwrap_thread = std::thread::spawn(|| Arc::into_inner(x)); | ||||||
/// let y_unwrap_thread = std::thread::spawn(|| Arc::into_inner(y)); | ||||||
/// | ||||||
/// let x_unwrapped_value = x_unwrap_thread.join().unwrap(); | ||||||
/// let y_unwrapped_value = y_unwrap_thread.join().unwrap(); | ||||||
/// | ||||||
/// // One of the threads is guaranteed to receive the inner value: | ||||||
/// assert!(matches!( | ||||||
/// (x_unwrapped_value, y_unwrapped_value), | ||||||
/// (None, Some(3)) | (Some(3), None) | ||||||
/// )); | ||||||
/// // The result could also be `(None, None)` if the threads called | ||||||
/// // `Arc::try_unwrap(x).ok()` and `Arc::try_unwrap(y).ok()` instead. | ||||||
/// ``` | ||||||
/// | ||||||
/// A more practical example demonstrating the need for `Arc::into_inner`: | ||||||
/// ``` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In case a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least on my machine this doc-test does not run particularly slowly. There seem to be lots of other doc tests that take much longer than this one. The 100_000 iterations are necessary to ensure stack overflows are even possible on enough platforms. |
||||||
/// #![feature(arc_into_inner)] | ||||||
/// | ||||||
/// use std::sync::Arc; | ||||||
/// | ||||||
/// // Definition of a simple singly linked list using `Arc`: | ||||||
/// #[derive(Clone)] | ||||||
/// struct LinkedList<T>(Option<Arc<Node<T>>>); | ||||||
/// struct Node<T>(T, Option<Arc<Node<T>>>); | ||||||
/// | ||||||
/// // Dropping a long `LinkedList<T>` relying on the destructor of `Arc` | ||||||
/// // can cause a stack overflow. To prevent this, we can provide a | ||||||
/// // manual `Drop` implementation that does the destruction in a loop: | ||||||
/// impl<T> Drop for LinkedList<T> { | ||||||
/// fn drop(&mut self) { | ||||||
/// let mut link = self.0.take(); | ||||||
/// while let Some(arc_node) = link.take() { | ||||||
/// if let Some(Node(_value, next)) = Arc::into_inner(arc_node) { | ||||||
/// link = next; | ||||||
/// } | ||||||
/// } | ||||||
/// } | ||||||
/// } | ||||||
/// | ||||||
/// // Implementation of `new` and `push` omitted | ||||||
/// impl<T> LinkedList<T> { | ||||||
/// /* ... */ | ||||||
/// # fn new() -> Self { | ||||||
/// # LinkedList(None) | ||||||
/// # } | ||||||
/// # fn push(&mut self, x: T) { | ||||||
/// # self.0 = Some(Arc::new(Node(x, self.0.take()))); | ||||||
/// # } | ||||||
/// } | ||||||
/// | ||||||
/// // The following code could still cause a stack overflow | ||||||
/// // despite the manual `Drop` impl if that `Drop` impl used | ||||||
/// // `Arc::try_unwrap(arc).ok()` instead of `Arc::into_inner(arc)`. | ||||||
/// | ||||||
/// // Create a long list and clone it | ||||||
/// let mut x = LinkedList::new(); | ||||||
/// for i in 0..100000 { | ||||||
/// x.push(i); // Adds i to the front of x | ||||||
/// } | ||||||
/// let y = x.clone(); | ||||||
/// | ||||||
/// // Drop the clones in parallel | ||||||
/// let t1 = std::thread::spawn(|| drop(x)); | ||||||
/// let t2 = std::thread::spawn(|| drop(y)); | ||||||
/// t1.join().unwrap(); | ||||||
/// t2.join().unwrap(); | ||||||
/// ``` | ||||||
|
||||||
// FIXME: when `Arc::into_inner` is stabilized, adjust the documentation of | ||||||
// `Arc::try_unwrap` according to the `FIXME` presented there. | ||||||
#[inline] | ||||||
#[unstable(feature = "arc_into_inner", issue = "none")] // FIXME: create and add issue | ||||||
pub fn into_inner(this: Self) -> Option<T> { | ||||||
// Make sure that the ordinary `Drop` implementation isn’t called as well | ||||||
let mut this = mem::ManuallyDrop::new(this); | ||||||
|
||||||
// Following the implementation of `drop` and `drop_slow` | ||||||
if this.inner().strong.fetch_sub(1, Release) != 1 { | ||||||
return None; | ||||||
} | ||||||
|
||||||
acquire!(this.inner().strong); | ||||||
|
||||||
// SAFETY: This mirrors the line | ||||||
// | ||||||
// unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) }; | ||||||
// | ||||||
// in `drop_slow`. Instead of dropping the value behind the pointer | ||||||
// it is read and eventually returned; `ptr::read` has the same | ||||||
// safety conditions as `ptr::drop_in_place`. | ||||||
let inner = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) }; | ||||||
|
||||||
drop(Weak { ptr: this.ptr }); | ||||||
|
||||||
Some(inner) | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T> Arc<[T]> { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talking for the future here, but if/when this feature gets stabilized, it will be nice to have a (clippy?) lint for this 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this has been done before, but adding a
FIXME
comment about opening an issue for it on clippy would ensure this is not forgotten when this becomes stable.