rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <alice@ryhl.io>
To: Boqun Feng <boqun.feng@gmail.com>, Alice Ryhl <aliceryhl@google.com>
Cc: a.hindborg@samsung.com, alex.gaynor@gmail.com,
	benno.lossin@proton.me, bjorn3_gh@protonmail.com,
	gary@garyguo.net, linux-kernel@vger.kernel.org, ojeda@kernel.org,
	rust-for-linux@vger.kernel.org, walmeida@microsoft.com,
	wedsonaf@gmail.com
Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef`
Date: Mon, 25 Sep 2023 17:30:05 +0200	[thread overview]
Message-ID: <0b7fc71c-b3c7-4c29-92a9-587daa46ad59@ryhl.io> (raw)
In-Reply-To: <ZRGknJCB6tFgX3Gr@Boquns-Mac-mini.home>

On 9/25/23 17:17, Boqun Feng wrote:
> On Mon, Sep 25, 2023 at 03:00:47PM +0000, Alice Ryhl wrote:
>>>>> I'm concerned about this change, because an `&WithRef<T>` only has
>>>>> immutable permissions for the allocation. No pointer derived from it
>>>>> may be used to modify the value in the Arc, however, the drop
>>>>> implementation of Arc will do exactly that.
>>>>
>>>> That is indeed a problem. We could put the value in an `UnsafeCell`, but
>>>> that would lose us niche optimizations and probably also other optimizations.
>>>>
>>>
>>> Not sure I understand the problem here, why do we allow modifying the
>>> value in the Arc if you only have a shared ownership?
>>
>> Well, usually it's when you have exclusive access even though the value
>> is in an `Arc`.
>>
>> The main example of this is the destructor of the `Arc`. When the last
>> refcount drops to zero, this gives you exclusive access. This lets you
>> run the destructor. The destructor requires mutable access.
>>
>> Another example would be converting the `Arc` back into an `UniqueArc`
>> by checking that the refcount is 1. Once you have a `UniqueArc`, you can
>> use it to mutate the inner value.
>>
>> Finally, there are methods like `Arc::get_mut_unchecked`, where you
>> unsafely assert that nobody else is using the value while you are
>> modifying it. We don't have that in our version of `Arc` right now, but
>> we might want to add it later.
>>
> 
> Hmm.. but the only way to get an `Arc` from `&WithRef` is
> 
> 	impl From<&WithRef<T>> for Arc<T> {
> 	    ...
> 	}
> 
> , and we clone `Arc` in the that function (i.e. copying the raw
> pointer), so we are still good?
> 

No, the raw pointer in the Arc was created from the immutable reference, 
so the raw pointer has the same restrictions as the immutable reference did.

Alice

  reply	other threads:[~2023-09-25 15:30 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23 14:49 [PATCH v2 0/2] Remove `ArcBorrow` Wedson Almeida Filho
2023-09-23 14:49 ` [PATCH v2 1/2] rust: arc: rename `ArcInner` to `WithRef` Wedson Almeida Filho
2023-09-23 19:31   ` Martin Rodriguez Reboredo
2023-09-24 11:59   ` Benno Lossin
2023-09-24 13:41   ` Jianguo Bao
2023-09-25  6:21   ` Alice Ryhl
2023-09-23 14:49 ` [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef` Wedson Almeida Filho
2023-09-23 19:32   ` Martin Rodriguez Reboredo
2023-09-24 11:59   ` Benno Lossin
2023-09-24 13:36   ` Jianguo Bao
2023-09-25  6:29   ` Alice Ryhl
2023-09-25  9:14     ` Benno Lossin
2023-09-25 14:49       ` Boqun Feng
2023-09-25 15:00         ` Alice Ryhl
2023-09-25 15:17           ` Boqun Feng
2023-09-25 15:30             ` Alice Ryhl [this message]
2023-09-25 16:02               ` Boqun Feng
2023-09-25 16:11                 ` Benno Lossin
2023-09-25 15:07         ` Benno Lossin
2023-09-25 16:16           ` Boqun Feng
2023-09-25 17:00             ` Benno Lossin
2023-09-25 18:51               ` Boqun Feng
2023-09-25 21:03                 ` Benno Lossin
2023-09-25 21:55                   ` Boqun Feng
2023-09-25 21:58                     ` Alice Ryhl
2023-09-25 22:02                       ` Boqun Feng
2023-09-25 22:06                         ` Boqun Feng
2023-09-25 22:26                         ` Benno Lossin
2023-09-25 22:34                           ` Boqun Feng
2023-09-25 23:24                             ` Boqun Feng
2023-09-26  8:26                           ` Gary Guo
2023-09-26 15:24                             ` Boqun Feng
2023-09-26 15:41                               ` Alice Ryhl
2023-09-26 16:35                                 ` Boqun Feng
2023-09-26 17:15                                   ` Benno Lossin
2023-09-26 17:43                                     ` Boqun Feng
2023-09-26 18:26                                       ` Benno Lossin
2023-09-26 21:31                                       ` Alice Ryhl
2023-09-26 18:20                                     ` Boqun Feng
2023-09-26 21:27                                       ` Alice Ryhl
2023-09-25 15:04       ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0b7fc71c-b3c7-4c29-92a9-587daa46ad59@ryhl.io \
    --to=alice@ryhl.io \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=walmeida@microsoft.com \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).