All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>
Subject: Re: [PATCH] rust: sync: lock: Add an example for Guard::lock_ref()
Date: Sun, 23 Feb 2025 13:51:37 -0800	[thread overview]
Message-ID: <Z7uYaRM6LuwfcS8n@boqun-archlinux> (raw)
In-Reply-To: <f834d8bf-3860-4087-937d-d9937c1be3f2@proton.me>

On Sun, Feb 23, 2025 at 10:54:59AM +0000, Benno Lossin wrote:
> On 23.02.25 08:21, Boqun Feng wrote:
> > To provide examples on usage of `Guard::lock_ref()` along with the unit
> > test, an "assert a lock is held by a guard" example is added.
> > 
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> 
> Reviewed-by: Benno Lossin <benno.lossin@proton.me>
> 

Thanks!

> > ---
> > This depends on Alice's patch:
> > 
> > 	https://lore.kernel.org/all/20250130-guard-get-lock-v1-1-8ed87899920a@google.com/
> > 
> > I'm also OK to fold this in if Alice thinks it's fine.
> > 
> >  rust/kernel/sync/lock.rs | 24 ++++++++++++++++++++++++
> >  1 file changed, 24 insertions(+)
> > 
> > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> > index 3701fac6ebf6..6d868e35b0a3 100644
> > --- a/rust/kernel/sync/lock.rs
> > +++ b/rust/kernel/sync/lock.rs
> > @@ -201,6 +201,30 @@ unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
> > 
> >  impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
> >      /// Returns the lock that this guard originates from.
> > +    ///
> > +    /// # Examples
> > +    ///
> > +    /// The following example shows how to use [`Guard::lock_ref()`] to assert the corresponding
> > +    /// lock is held.
> > +    ///
> > +    /// ```
> > +    /// # use kernel::{new_spinlock, stack_pin_init, sync::lock::{Backend, Guard, Lock}};
> > +    ///
> > +    /// fn assert_held<T, B: Backend>(guard: &Guard<'_, T, B>, lock: &Lock<T, B>) {
> > +    ///     // Address-equal means the same lock.
> > +    ///     assert!(core::ptr::eq(guard.lock_ref(), lock));
> > +    /// }
> > +    ///
> > +    /// // Creates a new lock on stack.
> 
> I would be inclined to write "new lock on the stack.", but maybe that is
> incorrect.
> 

Yes, "on the stack" is better.

Regards,
Boqun

> ---
> Cheers,
> Benno
> 
> > +    /// stack_pin_init!{
> > +    ///     let l = new_spinlock!(42)
> > +    /// }
> > +    ///
> > +    /// let g = l.lock();
> > +    ///
> > +    /// // `g` originates from `l`.
> > +    /// assert_held(&g, &l);
> > +    /// ```
> >      pub fn lock_ref(&self) -> &'a Lock<T, B> {
> >          self.lock
> >      }
> > --
> > 2.39.5 (Apple Git-154)
> > 
> 

  reply	other threads:[~2025-02-23 21:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-23  7:21 [PATCH] rust: sync: lock: Add an example for Guard::lock_ref() Boqun Feng
2025-02-23 10:54 ` Benno Lossin
2025-02-23 21:51   ` Boqun Feng [this message]
2025-02-24  8:08 ` Andreas Hindborg
2025-02-24 10:06   ` Benno Lossin
2025-02-24 11:15     ` Andreas Hindborg
2025-02-24 22:50       ` Benno Lossin
2025-02-25  5:52         ` Andreas Hindborg
2025-02-24 23:31   ` Boqun Feng
2025-02-24 11:19 ` Alice Ryhl
2025-02-25 16:53   ` Boqun Feng
2025-03-03 18:51 ` [tip: locking/core] " tip-bot2 for Boqun Feng

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=Z7uYaRM6LuwfcS8n@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --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.