[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLggrRANz1GrjEa671Vj0m9=UDeEcGV5vhOxq8XtR6EjUSg@mail.gmail.com>
Date: Mon, 11 Mar 2024 10:03:54 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Andreas Hindborg <a.hindborg@...sung.com>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 2/2] rust: sync: add `Arc::into_unique_or_drop`
On Sat, Mar 9, 2024 at 2:02 PM Benno Lossin <benno.lossin@...ton.me> wrote:
>
> On 2/28/24 14:00, Alice Ryhl wrote:
> > + // SAFETY: If the refcount reaches a non-zero value, then we have destroyed this `Arc` and
> > + // will return without further touching the `Arc`. If the refcount reaches zero, then there
> > + // are no other arcs, and we can create a `UniqueArc`.
>
> This comment is not explaining why it is safe to call
> `refcount_dec_and_test` on `refcount`.
> It dose however explain what you are going to do, so please keep it, but
> not as a SAFETY comment.
I'll reword.
> > + let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
> > + if is_zero {
> > + // SAFETY: We have exclusive access to the arc, so we can perform unsynchronized
> > + // accesses to the refcount.
> > + unsafe { core::ptr::write(refcount, bindings::REFCOUNT_INIT(1)) };
> > +
> > + // SAFETY: We own one refcount, so we can create a `UniqueArc`. It needs to be pinned,
> > + // since an `Arc` is pinned.
>
> The `unsafe` block is only needed due to the `new_unchecked` call, which
> you could avoid by using `.into()`. The `SAFETY` should also be an
> `INVARIANT` comment instead.
>
> > + unsafe {
> > + Some(Pin::new_unchecked(UniqueArc {
> > + inner: Arc::from_inner(me.ptr),
> > + }))
> > + }
The from_inner method is also unsafe.
I think that using new_unchecked here makes more sense. That method is
usually used in the case where something is already pinned, whereas
into() is usually used to pin something that was not previously
pinned.
Alice
Powered by blists - more mailing lists