[PATCH v13 16/17] rust: sync: lock/global: Add BackendInContext support to GlobalLock

Lyude Paul
2025-10-13T15:55:03Z

Now that we have the ability to provide an explicit lifetime for a
GlobalGuard and an explicit Backend for a GlobalGuard, we can finally
implement lock_with() and try_lock_with().

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
rust/kernel/sync/lock/global.rs | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)

diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
index f9a9c4bdc46b0..1f28bf5fc8851 100644
--- a/rust/kernel/sync/lock/global.rs
+++ b/rust/kernel/sync/lock/global.rs
@@ -89,6 +89,34 @@ pub fn try_lock(&'static self) -> Option<GlobalGuard<'static, G, G::Backend>> {
inner: self.inner.try_lock()?,
})
}
+
+ /// Lock this global lock with the provided `context`.
+ pub fn lock_with<'a, B>(
+ &'static self,
+ context: <G::Backend as Backend>::Context<'a>,
+ ) -> GlobalGuard<'a, G, B>
+ where
+ G::Backend: Backend<BackendInContext = B>,
+ B: Backend,
+ {
+ GlobalGuard {
+ inner: self.inner.lock_with(context),
+ }
+ }
+
+ /// Try to lock this global lock with the provided `context`.
+ pub fn try_lock_with<'a, B>(
+ &'static self,
+ context: <G::Backend as Backend>::Context<'a>,
+ ) -> Option<GlobalGuard<'a, G, B>>
+ where
+ G::Backend: Backend<BackendInContext = B>,
+ B: Backend,
+ {
+ Some(GlobalGuard {
+ inner: self.inner.try_lock_with(context)?,
+ })
+ }
}

/// A guard for a [`GlobalLock`].
--
2.51.0