xref: /linux/rust/kernel/sync.rs (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
19dc04365SWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
29dc04365SWedson Almeida Filho 
39dc04365SWedson Almeida Filho //! Synchronisation primitives.
49dc04365SWedson Almeida Filho //!
59dc04365SWedson Almeida Filho //! This module contains the kernel APIs related to synchronisation that have been ported or
69dc04365SWedson Almeida Filho //! wrapped for usage by Rust code in the kernel.
79dc04365SWedson Almeida Filho 
86ea5aa08SWedson Almeida Filho use crate::types::Opaque;
96ea5aa08SWedson Almeida Filho 
109dc04365SWedson Almeida Filho mod arc;
1119096bceSWedson Almeida Filho mod condvar;
1276d4bd59SWedson Almeida Filho pub mod lock;
137b1f55e3SWedson Almeida Filho mod locked_by;
149dc04365SWedson Almeida Filho 
1570e42ebbSWedson Almeida Filho pub use arc::{Arc, ArcBorrow, UniqueArc};
16e283ee23SAlice Ryhl pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
17e283ee23SAlice Ryhl pub use lock::mutex::{new_mutex, Mutex};
18e283ee23SAlice Ryhl pub use lock::spinlock::{new_spinlock, SpinLock};
197b1f55e3SWedson Almeida Filho pub use locked_by::LockedBy;
206ea5aa08SWedson Almeida Filho 
216ea5aa08SWedson Almeida Filho /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
226ea5aa08SWedson Almeida Filho #[repr(transparent)]
236ea5aa08SWedson Almeida Filho pub struct LockClassKey(Opaque<bindings::lock_class_key>);
246ea5aa08SWedson Almeida Filho 
256ea5aa08SWedson Almeida Filho // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
266ea5aa08SWedson Almeida Filho // provides its own synchronization.
276ea5aa08SWedson Almeida Filho unsafe impl Sync for LockClassKey {}
286ea5aa08SWedson Almeida Filho 
296ea5aa08SWedson Almeida Filho impl LockClassKey {
306ea5aa08SWedson Almeida Filho     /// Creates a new lock class key.
new() -> Self316ea5aa08SWedson Almeida Filho     pub const fn new() -> Self {
326ea5aa08SWedson Almeida Filho         Self(Opaque::uninit())
336ea5aa08SWedson Almeida Filho     }
346ea5aa08SWedson Almeida Filho 
as_ptr(&self) -> *mut bindings::lock_class_key356ea5aa08SWedson Almeida Filho     pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
366ea5aa08SWedson Almeida Filho         self.0.get()
376ea5aa08SWedson Almeida Filho     }
386ea5aa08SWedson Almeida Filho }
396ea5aa08SWedson Almeida Filho 
40*7c81aa85SMiguel Ojeda impl Default for LockClassKey {
default() -> Self41*7c81aa85SMiguel Ojeda     fn default() -> Self {
42*7c81aa85SMiguel Ojeda         Self::new()
43*7c81aa85SMiguel Ojeda     }
44*7c81aa85SMiguel Ojeda }
45*7c81aa85SMiguel Ojeda 
466ea5aa08SWedson Almeida Filho /// Defines a new static lock class and returns a pointer to it.
476ea5aa08SWedson Almeida Filho #[doc(hidden)]
486ea5aa08SWedson Almeida Filho #[macro_export]
496ea5aa08SWedson Almeida Filho macro_rules! static_lock_class {
506ea5aa08SWedson Almeida Filho     () => {{
516ea5aa08SWedson Almeida Filho         static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
526ea5aa08SWedson Almeida Filho         &CLASS
536ea5aa08SWedson Almeida Filho     }};
546ea5aa08SWedson Almeida Filho }
556ea5aa08SWedson Almeida Filho 
566ea5aa08SWedson Almeida Filho /// Returns the given string, if one is provided, otherwise generates one based on the source code
576ea5aa08SWedson Almeida Filho /// location.
586ea5aa08SWedson Almeida Filho #[doc(hidden)]
596ea5aa08SWedson Almeida Filho #[macro_export]
606ea5aa08SWedson Almeida Filho macro_rules! optional_name {
616ea5aa08SWedson Almeida Filho     () => {
626ea5aa08SWedson Almeida Filho         $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
636ea5aa08SWedson Almeida Filho     };
646ea5aa08SWedson Almeida Filho     ($name:literal) => {
656ea5aa08SWedson Almeida Filho         $crate::c_str!($name)
666ea5aa08SWedson Almeida Filho     };
676ea5aa08SWedson Almeida Filho }
68