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::{new_condvar, CondVar, CondVarTimeoutResult};
17 pub use lock::mutex::{new_mutex, Mutex};
18 pub use lock::spinlock::{new_spinlock, SpinLock};
19 pub use locked_by::LockedBy;
21 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
23 pub struct LockClassKey(Opaque<bindings::lock_class_key>);
25 // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
26 // provides its own synchronization.
27 unsafe impl Sync for LockClassKey {}
30 /// Creates a new lock class key.
31 pub const fn new() -> Self {
32 Self(Opaque::uninit())
35 pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
40 impl Default for LockClassKey {
41 fn default() -> Self {
46 /// Defines a new static lock class and returns a pointer to it.
49 macro_rules! static_lock_class {
51 static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
56 /// Returns the given string, if one is provided, otherwise generates one based on the source code
60 macro_rules! optional_name {
62 $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))