alloc: clarify comment on ArcInner::weak lock sentinel#156787
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| // `Arc::get_mut`) to atomically confirm that only the implicit weak | ||
| // reference exists before checking the strong count for uniqueness. |
There was a problem hiding this comment.
I think this could be phrased more precisely. The implicit-weak-reference check is trivial to achieve atomically, it's just that the entire operation (both the strong count and weak count comparison) has to be atomic, and the only way to achieve that is by locking one of the counts.
|
This looks fine to me. Could you please squash your commits? |
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
Explain that the weak count doubles as a lock taken by get_mut and downgrade, and that the value 1 is the implicit weak reference owned by the strong references rather than an absence of weak handles.
7d9e442 to
2be9d51
Compare
|
Squashed into a single commit. @rustbot ready |
|
Great, thanks! |
Rollup merge of #156787 - LeSingh1:fix-arc-inner-weak-comment, r=joboet alloc: clarify comment on ArcInner::weak lock sentinel The comment on the `weak` field of `ArcInner` currently states that the `usize::MAX` sentinel locks "the ability to upgrade weak pointers or downgrade strong ones", and that this is used to avoid races in `make_mut` and `get_mut`. Both halves of that description are inaccurate. `Weak::upgrade` never inspects the weak count, so the lock has no effect on it. The operation only CASes on the strong count and is happy to succeed while the weak count is locked. `Arc::make_mut` also does not participate in the locking protocol, and the function body already contains a comment noting that observing `usize::MAX` for the weak count there is impossible because only a thread holding a strong reference can take the lock. The lock is actually only taken in `Arc::is_unique` (which backs `Arc::get_mut`), and the only operation that observes and waits on it is `Arc::downgrade`, which spins while `weak == usize::MAX` before attempting its CAS to create a new `Weak`. This PR rewrites the comment to describe what the lock is and what it actually prevents. Closes #144296
The comment on the
weakfield ofArcInnercurrently states that theusize::MAXsentinel locks "the ability to upgrade weak pointers or downgrade strong ones", and that this is used to avoid races inmake_mutandget_mut. Both halves of that description are inaccurate.Weak::upgradenever inspects the weak count, so the lock has no effect on it. The operation only CASes on the strong count and is happy to succeed while the weak count is locked.Arc::make_mutalso does not participate in the locking protocol, and the function body already contains a comment noting that observingusize::MAXfor the weak count there is impossible because only a thread holding a strong reference can take the lock.The lock is actually only taken in
Arc::is_unique(which backsArc::get_mut), and the only operation that observes and waits on it isArc::downgrade, which spins whileweak == usize::MAXbefore attempting its CAS to create a newWeak.This PR rewrites the comment to describe what the lock is and what it actually prevents.
Closes #144296