blob: 0ab20975a3b5db4dcf913f4adaf6d3faca702dbf [file] [log] [blame]
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +00001// 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
Wedson Almeida Filho6ea5aa02023-04-11 02:45:31 -03008use crate::types::Opaque;
9
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +000010mod arc;
Wedson Almeida Filho19096bc2023-03-26 00:57:38 -030011mod condvar;
Wedson Almeida Filho76d4bd52023-04-11 02:45:32 -030012pub mod lock;
Wedson Almeida Filho7b1f55e2023-04-11 02:45:43 -030013mod locked_by;
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +000014
Wedson Almeida Filho70e42eb2022-12-28 06:03:45 +000015pub use arc::{Arc, ArcBorrow, UniqueArc};
Alice Ryhle283ee22024-01-29 14:58:37 +000016pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
17pub use lock::mutex::{new_mutex, Mutex};
18pub use lock::spinlock::{new_spinlock, SpinLock};
Wedson Almeida Filho7b1f55e2023-04-11 02:45:43 -030019pub use locked_by::LockedBy;
Wedson Almeida Filho6ea5aa02023-04-11 02:45:31 -030020
21/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
22#[repr(transparent)]
23pub struct LockClassKey(Opaque<bindings::lock_class_key>);
24
25// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
26// provides its own synchronization.
27unsafe impl Sync for LockClassKey {}
28
29impl LockClassKey {
30 /// Creates a new lock class key.
31 pub const fn new() -> Self {
32 Self(Opaque::uninit())
33 }
34
Wedson Almeida Filho6ea5aa02023-04-11 02:45:31 -030035 pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
36 self.0.get()
37 }
38}
39
Miguel Ojeda7c81aa82024-04-01 23:23:01 +020040impl Default for LockClassKey {
41 fn default() -> Self {
42 Self::new()
43 }
44}
45
Wedson Almeida Filho6ea5aa02023-04-11 02:45:31 -030046/// Defines a new static lock class and returns a pointer to it.
47#[doc(hidden)]
48#[macro_export]
49macro_rules! static_lock_class {
50 () => {{
51 static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
52 &CLASS
53 }};
54}
55
56/// Returns the given string, if one is provided, otherwise generates one based on the source code
57/// location.
58#[doc(hidden)]
59#[macro_export]
60macro_rules! optional_name {
61 () => {
62 $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
63 };
64 ($name:literal) => {
65 $crate::c_str!($name)
66 };
67}