xref: /openbmc/linux/rust/kernel/sync.rs (revision 6ea5aa08)
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 
12 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 }
56