xref: /openbmc/linux/rust/kernel/sync.rs (revision 76d4bd59)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Synchronisation primitives.
4 //!
5 //! This module contains the kernel APIs related to synchronisation that have been ported or
6 //! wrapped for usage by Rust code in the kernel.
7 
8 use crate::types::Opaque;
9 
10 mod arc;
11 pub mod lock;
12 
13 pub use arc::{Arc, ArcBorrow, UniqueArc};
14 
15 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
16 #[repr(transparent)]
17 pub struct LockClassKey(Opaque<bindings::lock_class_key>);
18 
19 // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
20 // provides its own synchronization.
21 unsafe impl Sync for LockClassKey {}
22 
23 impl LockClassKey {
24     /// Creates a new lock class key.
25     pub const fn new() -> Self {
26         Self(Opaque::uninit())
27     }
28 
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 }
56