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