From: "tip-bot2 for Boqun Feng" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Boqun Feng <boqun.feng@gmail.com>,
Benno Lossin <benno.lossin@proton.me>,
Alice Ryhl <aliceryhl@google.com>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/core] rust: sync: lock: Add an example for Guard::lock_ref()
Date: Mon, 03 Mar 2025 18:51:55 -0000 [thread overview]
Message-ID: <174102791558.14745.12750435046437937643.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250223072114.3715-1-boqun.feng@gmail.com>
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 33530ab4613d7544ff43df2d690c54d8ff9288c6
Gitweb: https://git.kernel.org/tip/33530ab4613d7544ff43df2d690c54d8ff9288c6
Author: Boqun Feng <boqun.feng@gmail.com>
AuthorDate: Sat, 22 Feb 2025 23:09:24 -08:00
Committer: Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Tue, 25 Feb 2025 08:52:48 -08:00
rust: sync: lock: Add an example for Guard::lock_ref()
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.
[boqun: Apply feedback from Benno]
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20250223072114.3715-1-boqun.feng@gmail.com
---
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 8e7e6d5..f53e87d 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 the stack.
+ /// 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
}
next prev parent reply other threads:[~2025-03-03 18:52 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-23 7:21 [PATCH] " Boqun Feng
2025-02-23 10:54 ` Benno Lossin
2025-02-23 21:51 ` Boqun Feng
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-bot2 for Boqun Feng [this message]
2025-03-07 23:26 [PATCH locking 08/11] " Boqun Feng
2025-03-08 0:08 ` [tip: locking/core] rust: sync: lock: Add an example for Guard:: Lock_ref() 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=174102791558.14745.12750435046437937643.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=boqun.feng@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=x86@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®