Skip to content

Commit 6ea5aa0

Browse files
wedsonafojeda
authored andcommitted
rust: sync: introduce LockClassKey
It is a wrapper around C's `lock_class_key`, which is used by the synchronisation primitives that are checked with lockdep. This is in preparation for introducing Rust abstractions for these primitives. Cc: Peter Zijlstra <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Will Deacon <[email protected]> Cc: Waiman Long <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Co-developed-by: Boqun Feng <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent b0cf5d5 commit 6ea5aa0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

rust/kernel/sync.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,51 @@
55
//! This module contains the kernel APIs related to synchronisation that have been ported or
66
//! wrapped for usage by Rust code in the kernel.
77
8+
use crate::types::Opaque;
9+
810
mod arc;
911

1012
pub use arc::{Arc, ArcBorrow, UniqueArc};
13+
14+
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
15+
#[repr(transparent)]
16+
pub struct LockClassKey(Opaque<bindings::lock_class_key>);
17+
18+
// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
19+
// provides its own synchronization.
20+
unsafe impl Sync for LockClassKey {}
21+
22+
impl LockClassKey {
23+
/// Creates a new lock class key.
24+
pub const fn new() -> Self {
25+
Self(Opaque::uninit())
26+
}
27+
28+
#[allow(dead_code)]
29+
pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
30+
self.0.get()
31+
}
32+
}
33+
34+
/// Defines a new static lock class and returns a pointer to it.
35+
#[doc(hidden)]
36+
#[macro_export]
37+
macro_rules! static_lock_class {
38+
() => {{
39+
static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
40+
&CLASS
41+
}};
42+
}
43+
44+
/// Returns the given string, if one is provided, otherwise generates one based on the source code
45+
/// location.
46+
#[doc(hidden)]
47+
#[macro_export]
48+
macro_rules! optional_name {
49+
() => {
50+
$crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
51+
};
52+
($name:literal) => {
53+
$crate::c_str!($name)
54+
};
55+
}

0 commit comments

Comments
 (0)