sync.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Synchronisation primitives.
  3. //!
  4. //! This module contains the kernel APIs related to synchronisation that have been ported or
  5. //! wrapped for usage by Rust code in the kernel.
  6. use crate::types::Opaque;
  7. mod arc;
  8. mod condvar;
  9. pub mod lock;
  10. mod locked_by;
  11. pub use arc::{Arc, ArcBorrow, UniqueArc};
  12. pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
  13. pub use lock::mutex::{new_mutex, Mutex};
  14. pub use lock::spinlock::{new_spinlock, SpinLock};
  15. pub use locked_by::LockedBy;
  16. /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
  17. #[repr(transparent)]
  18. pub struct LockClassKey(Opaque<bindings::lock_class_key>);
  19. // SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
  20. // provides its own synchronization.
  21. unsafe impl Sync for LockClassKey {}
  22. impl LockClassKey {
  23. pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
  24. self.0.get()
  25. }
  26. }
  27. /// Defines a new static lock class and returns a pointer to it.
  28. #[doc(hidden)]
  29. #[macro_export]
  30. macro_rules! static_lock_class {
  31. () => {{
  32. static CLASS: $crate::sync::LockClassKey =
  33. // SAFETY: lockdep expects uninitialized memory when it's handed a statically allocated
  34. // lock_class_key
  35. unsafe { ::core::mem::MaybeUninit::uninit().assume_init() };
  36. &CLASS
  37. }};
  38. }
  39. /// Returns the given string, if one is provided, otherwise generates one based on the source code
  40. /// location.
  41. #[doc(hidden)]
  42. #[macro_export]
  43. macro_rules! optional_name {
  44. () => {
  45. $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
  46. };
  47. ($name:literal) => {
  48. $crate::c_str!($name)
  49. };
  50. }