Skip to main content

ax_kspin/
lib.rs

1#![cfg_attr(not(test), no_std)]
2#![doc = include_str!("../README.md")]
3
4#[cfg(any(test, doctest))]
5extern crate std;
6
7mod base;
8#[cfg(feature = "lock_api")]
9mod raw;
10mod rwlock;
11use ax_kernel_guard::{NoOp, NoPreempt, NoPreemptIrqSave};
12#[cfg(feature = "lockdep")]
13pub mod lockdep;
14
15#[cfg(feature = "lock_api")]
16pub use self::raw::{BaseRawSpinLock, RawSpinNoIrq};
17pub use self::{
18    base::{BaseSpinLock, BaseSpinLockGuard},
19    rwlock::{BaseSpinRwLock, BaseSpinRwLockReadGuard, BaseSpinRwLockWriteGuard},
20};
21
22/// Enables or disables the phase-3 lock flow tracing path.
23pub fn set_lockdep_trace_enabled(enabled: bool) {
24    #[cfg(feature = "lockdep")]
25    {
26        ax_lockdep::set_trace_enabled(enabled);
27    }
28
29    #[cfg(not(feature = "lockdep"))]
30    {
31        let _ = enabled;
32    }
33}
34
35/// Dumps the buffered phase-3 trace stream to the raw trace sink.
36pub fn dump_lockdep_trace() {
37    #[cfg(feature = "lockdep")]
38    {
39        ax_lockdep::dump_trace_buffer();
40    }
41}
42
43/// A spin lock that disables kernel preemption while trying to lock, and
44/// re-enables it after unlocking.
45///
46/// It must be used in the local IRQ-disabled context, or never be used in
47/// interrupt handlers.
48pub type SpinNoPreempt<T> = BaseSpinLock<NoPreempt, T>;
49
50/// A guard that provides mutable data access for [`SpinNoPreempt`].
51pub type SpinNoPreemptGuard<'a, T> = BaseSpinLockGuard<'a, NoPreempt, T>;
52
53/// A spin read-write lock with raw spin semantics.
54///
55/// Like the historical raw spin read-write lock, this lock never sleeps and
56/// does not change IRQ or preemption state by itself.
57pub type SpinRwLock<T> = BaseSpinRwLock<NoOp, T>;
58
59/// A guard that provides shared data access for [`SpinRwLock`].
60pub type SpinRwLockReadGuard<'a, T> = BaseSpinRwLockReadGuard<'a, NoOp, T>;
61
62/// A guard that provides exclusive data access for [`SpinRwLock`].
63pub type SpinRwLockWriteGuard<'a, T> = BaseSpinRwLockWriteGuard<'a, NoOp, T>;
64
65/// A spin lock that disables kernel preemption and local IRQs while trying to
66/// lock, and re-enables it after unlocking.
67///
68/// It can be used in the IRQ-enabled context.
69pub type SpinNoIrq<T> = BaseSpinLock<NoPreemptIrqSave, T>;
70
71/// A guard that provides mutable data access for [`SpinNoIrq`].
72pub type SpinNoIrqGuard<'a, T> = BaseSpinLockGuard<'a, NoPreemptIrqSave, T>;
73
74/// A spin read-write lock that disables kernel preemption and local IRQs while held.
75pub type SpinNoIrqRwLock<T> = BaseSpinRwLock<NoPreemptIrqSave, T>;
76
77/// A guard that provides shared data access for [`SpinNoIrqRwLock`].
78pub type SpinNoIrqRwLockReadGuard<'a, T> = BaseSpinRwLockReadGuard<'a, NoPreemptIrqSave, T>;
79
80/// A guard that provides exclusive data access for [`SpinNoIrqRwLock`].
81pub type SpinNoIrqRwLockWriteGuard<'a, T> = BaseSpinRwLockWriteGuard<'a, NoPreemptIrqSave, T>;
82
83/// A raw spin lock that does nothing while trying to lock.
84///
85/// It must be used in the preemption-disabled and local IRQ-disabled context,
86/// or never be used in interrupt handlers.
87pub type SpinRaw<T> = BaseSpinLock<NoOp, T>;
88
89/// A guard that provides mutable data access for [`SpinRaw`].
90pub type SpinRawGuard<'a, T> = BaseSpinLockGuard<'a, NoOp, T>;
91
92/// A raw spin read-write lock that does nothing while held.
93pub type SpinRawRwLock<T> = BaseSpinRwLock<NoOp, T>;
94
95/// A guard that provides shared data access for [`SpinRawRwLock`].
96pub type SpinRawRwLockReadGuard<'a, T> = BaseSpinRwLockReadGuard<'a, NoOp, T>;
97
98/// A guard that provides exclusive data access for [`SpinRawRwLock`].
99pub type SpinRawRwLockWriteGuard<'a, T> = BaseSpinRwLockWriteGuard<'a, NoOp, T>;
100
101#[cfg(all(axtest, feature = "axtest"))]
102pub mod axtest;