From 2be9d51b437a8efe57b81a8d3142f050cfceb22f Mon Sep 17 00:00:00 2001 From: LeSingh1 Date: Thu, 11 Jun 2026 13:09:00 -0700 Subject: [PATCH] alloc: clarify comment on ArcInner::weak lock sentinel 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. --- library/alloc/src/sync.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 229fcd2b429cf..fe2f25a396557 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -389,8 +389,12 @@ struct ArcInner { strong: Atomic, // the value usize::MAX acts as a sentinel for temporarily "locking" the - // ability to upgrade weak pointers or downgrade strong ones; this is used - // to avoid races in `make_mut` and `get_mut`. + // weak count, preventing `Arc::downgrade` from racing to create new + // `Weak` references. `Arc::is_unique` (which backs `Arc::get_mut`) + // needs to observe both the strong and weak counts as indicating + // uniqueness in one logical atomic step; since they live in separate + // atomic words, it locks the weak count while reading the strong + // count to keep the two reads consistent. weak: Atomic, data: T,