All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: sync: add accessor for the lock behind a given guard
Date: Tue, 4 Feb 2025 06:47:33 -0800	[thread overview]
Message-ID: <Z6IohVx6DEV4a6cK@Mac.home> (raw)
In-Reply-To: <CAH5fLggDna0E9MtarHDxoS=w3zp-2HipASOLGfYFzTg8fxUu2A@mail.gmail.com>

On Tue, Feb 04, 2025 at 02:39:33PM +0100, Alice Ryhl wrote:
[...]
> > > > How about we name this as `lock_ref()` or something else, because
> > > > `lock()` itself is already used by `Lock` for the lock *operation*, and
> > > > this is just an accessor, I would like we don't confuse code readers
> > > > when they see code like "let b = a.lock()".
> > >
> > > The usual name for this operation in userspace is "mutex".
> > > https://docs.rs/lock_api/0.4.12/lock_api/struct.MutexGuard.html#method.mutex
> > >
> > > But since our code is generic over many lock types, I went for "lock".
> > > But I guess it could make sense to rename it.
> > >
> >
> > Got it. The good thing about the naming of lock_api is that the name
> > "mutex" is not used for other purpose, while "lock" is a bit different.
> >
> > > > Moreover, if the only usage
> > > > of this is for asserting the right lock, maybe we can instead add an:
> > > >
> > > >     pub fn assert_lock_associated(&self, lock_ptr: *const Lock<T, B>)
> > >
> > > I guess, though there is precedent for having a method that gets the
> > > underlying lock, so I think it makes sense. If we had an assertion, it
> >
> > I don't disagree, but I just feel we should be careful about introducing
> > two "lock()" that one is an operation and the other is an accessor.
> >
> > > would probably take an &Lock<T,B>.
> > >
> >
> > How about:
> >
> >     impl<T, B: Backend> Lock<T, B> {
> >         pub fn assert_held_then<O>(
> >             &self, proof: &Guard<'_, T, B>, f: FnOnce() -> O
> >         ) -> O {
> >             <assert `proof` is associated with `&self`>
> >             f()
> >         }
> >     }
> >
> > In this way, not only we can assert the correct lock is held, but we can
> > also guarantee `f()` is called with the lock held. Thoughts?
> 
> I need mutable access to the guard during the function, though? I
> don't think a closure is helpful in this case.
> 
> How about I rename to `lock_ref()` instead?
> 

That would work for me, thanks!

Regards,
Boqun

> Alice

      reply	other threads:[~2025-02-04 14:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-30 11:39 [PATCH] rust: sync: add accessor for the lock behind a given guard Alice Ryhl
2025-01-30 11:51 ` Fiona Behrens
2025-02-05 13:42   ` Alice Ryhl
2025-01-30 15:33 ` Boqun Feng
2025-01-30 15:43   ` Alice Ryhl
2025-01-30 17:14     ` Boqun Feng
2025-02-04 13:39       ` Alice Ryhl
2025-02-04 14:47         ` Boqun Feng [this message]

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=Z6IohVx6DEV4a6cK@Mac.home \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.