1 // SPDX-License-Identifier: GPL-2.0
3 //! Synchronisation primitives.
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.
8 use crate::types::Opaque;
15 pub use arc::{Arc, ArcBorrow, UniqueArc};
16 pub use condvar::CondVar;
17 pub use lock::{mutex::Mutex, spinlock::SpinLock};
18 pub use locked_by::LockedBy;
20 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
22 pub struct LockClassKey(Opaque<bindings::lock_class_key>);
24 // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
25 // provides its own synchronization.
26 unsafe impl Sync for LockClassKey {}
29 /// Creates a new lock class key.
30 pub const fn new() -> Self {
31 Self(Opaque::uninit())
34 pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
39 /// Defines a new static lock class and returns a pointer to it.
42 macro_rules! static_lock_class {
44 static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
49 /// Returns the given string, if one is provided, otherwise generates one based on the source code
53 macro_rules! optional_name {
55 $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))