Rollup merge of #126539 - lukaslueg:patch-1, r=jhpratt

Update `Arc::try_unwrap()` docs

Clarify the language wrt "race condition" not meaning "memory unsafety".

The docs make an important point about a 'logical' race condition that can occur if the `Err`-case in `Arc::try_unwrap()` is not handled properly. The language as is uses the term "race condition", which the reader may associate with "memory unsafety". This PR tries to clarify the scenario and qualify "race condition without memory unsafety".
This commit is contained in:
Jacob Pratt 2024-06-16 03:41:58 -04:00 committed by GitHub
commit 623cf23621
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -940,15 +940,18 @@ pub fn try_new_zeroed_in(alloc: A) -> Result<Arc<mem::MaybeUninit<T>, A>, AllocE
/// This will succeed even if there are outstanding weak references. /// This will succeed even if there are outstanding weak references.
/// ///
/// It is strongly recommended to use [`Arc::into_inner`] instead if you don't /// It is strongly recommended to use [`Arc::into_inner`] instead if you don't
/// want to keep the `Arc` in the [`Err`] case. /// keep the `Arc` in the [`Err`] case.
/// Immediately dropping the [`Err`] payload, like in the expression /// Immediately dropping the [`Err`]-value, as the expression
/// `Arc::try_unwrap(this).ok()`, can still cause the strong count to /// `Arc::try_unwrap(this).ok()` does, can cause the strong count to
/// drop to zero and the inner value of the `Arc` to be dropped: /// drop to zero and the inner value of the `Arc` to be dropped.
/// For instance if two threads each execute this expression in parallel, then /// For instance, if two threads execute such an expression in parallel,
/// there is a race condition. The threads could first both check whether they /// there is a race condition without the possibility of unsafety:
/// have the last clone of their `Arc` via `Arc::try_unwrap`, and then /// The threads could first both check whether they own the last instance
/// both drop their `Arc` in the call to [`ok`][`Result::ok`], /// in `Arc::try_unwrap`, determine that they both do not, and then both
/// taking the strong count from two down to zero. /// discard and drop their instance in the call to [`ok`][`Result::ok`].
/// In this scenario, the value inside the `Arc` is safely destroyed
/// by exactly one of the threads, but neither thread will ever be able
/// to use the value.
/// ///
/// # Examples /// # Examples
/// ///