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