Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()

From: Boqun Feng
Date: Fri Feb 03 2023 - 03:03:59 EST


On Fri, Feb 03, 2023 at 08:38:25AM +0100, Greg KH wrote:
[...]
> > Needless to say that ArcInner::count() can guarantee not reading 0
>
> How? Because you have an implicit reference on it already? If so, then

Yes, roughly a reference ("&") in Rust can be treated as a
compile-time reference count, so the existence of "&" in fact prevents
the underlying data from going away, or in Rust term, being "drop"ped.

To get a "&ArcInner<T>", we need a "&Arc<T>", and as long as there is
a reference to an "Arc<T>", the object won't be dropped, that's the
proof of the underly object still being referenced.

Other folks may explain this better and accurate, but that's the basic
idea ;-)

Regards,
Boqun

> why does reading from it matter at all, as if you have a reference, you
> know it isn't 0, and that's all that you can really care about. You
> don't care about any number other than 0 for a reference count, as by
> definition, that's what a reference count does :)
>

[...]

> thanks,
>
> greg k-h