[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <f834d8bf-3860-4087-937d-d9937c1be3f2@proton.me>
Date: Sun, 23 Feb 2025 10:54:59 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Boqun Feng <boqun.feng@...il.com>, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org
Cc: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>, Will Deacon <will@...nel.org>, Waiman Long <longman@...hat.com>, Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>
Subject: Re: [PATCH] rust: sync: lock: Add an example for Guard::lock_ref()
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@...il.com>
Reviewed-by: Benno Lossin <benno.lossin@...ton.me>
> ---
> 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.
---
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)
>
Powered by blists - more mailing lists