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