Skip to main content

kernel_api/sync/
mod.rs

1//! Provides kernel synchronisation primitives
2//!
3//! These are currently based on spinlocks but this may be changed in future
4
5use core::ops::{Deref, DerefMut};
6#[cfg(not(feature = "use_std"))]
7pub use mutex::{Spinlock, SpinlockGuard, SpinlockGuardExt, MappedSpinlockGuard};
8#[cfg(feature = "use_std")]
9pub use parking_lot::{Mutex as Spinlock, MutexGuard as SpinlockGuard, MappedMutexGuard as MappedSpinlockGuard};
10#[cfg(not(feature = "use_std"))]
11pub use send_wrapper::*;
12
13#[cfg(not(feature = "use_std"))]
14pub use once::{LazyLock, Once, OnceLock, BootstrapOnceLock};
15#[cfg(feature = "use_std")]
16pub use std::sync::{LazyLock, Once, OnceLock};
17
18#[cfg(not(feature = "use_std"))]
19pub use rwlock::{RwSpinlock, RwReadGuard, RwUpgradableReadGuard, RwWriteGuard};
20#[cfg(feature = "use_std")]
21pub use parking_lot::{RwLock as RwSpinlock, RwLockReadGuard as RwReadGuard, RwLockUpgradableReadGuard as RwUpgradableReadGuard, RwLockWriteGuard as RwWriteGuard};
22
23#[cfg(not(feature = "use_std"))]
24pub use irq_cell::{IrqCell, IrqGuard};
25
26#[cfg(not(feature = "use_std"))]
27mod mutex;
28
29#[cfg(not(feature = "use_std"))]
30pub(crate) mod rwlock;
31
32#[cfg(not(feature = "use_std"))]
33mod once;
34
35#[cfg(not(feature = "use_std"))]
36mod irq_cell;
37
38#[cfg(not(feature = "use_std"))]
39mod send_wrapper;
40
41pub struct Syncify<T>(T);
42
43impl<T> Syncify<T> {
44	pub unsafe fn new(t: T) -> Self { Self(t) }
45
46	pub fn into_inner(self) -> T { self.0 }
47}
48
49impl<T> Deref for Syncify<T> {
50	type Target = T;
51
52	fn deref(&self) -> &Self::Target {
53		&self.0
54	}
55}
56
57impl<T> DerefMut for Syncify<T> {
58	fn deref_mut(&mut self) -> &mut Self::Target {
59		&mut self.0
60	}
61}
62
63unsafe impl<T> Sync for Syncify<T> {}
64unsafe impl<T> Send for Syncify<T> {}