1#![no_std]
6
7#[cfg(test)]
8extern crate self as ksync;
9
10pub use kstring::declare_interned_string;
11pub use ksync_macro::{declare_singleton_lock, guarded};
12pub use pin_init;
13
14#[macro_export]
28macro_rules! lock {
29 (let mut $guard:ident = $lock_init:expr) => {
30 $crate::pin_init::stack_pin_init!(let $guard = $lock_init);
31 let mut $guard = $guard;
32 };
33 (let $guard:ident = $lock_init:expr) => {
34 $crate::pin_init::stack_pin_init!(let $guard = $lock_init);
35 };
36 ($lock_init:expr) => {
37 $crate::pin_init::stack_pin_init!(let _guard = $lock_init);
38 };
39}
40
41#[repr(transparent)]
46#[derive(Copy, Clone, Debug, PartialEq, Eq)]
47pub struct SourceTag(&'static core::ffi::CStr);
48
49impl SourceTag {
50 #[inline]
58 pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &'static [u8]) -> Self {
59 Self(unsafe { core::ffi::CStr::from_bytes_with_nul_unchecked(bytes) })
61 }
62
63 #[inline]
65 pub const fn from_cstr(cstr: &'static core::ffi::CStr) -> Self {
66 Self(cstr)
67 }
68
69 #[inline]
71 pub const fn as_ptr(&self) -> *const core::ffi::c_char {
72 self.0.as_ptr()
73 }
74
75 #[inline]
77 pub const fn as_cstr(&self) -> &'static core::ffi::CStr {
78 self.0
79 }
80}
81
82impl Default for SourceTag {
83 #[inline]
84 fn default() -> Self {
85 Self(c"<unknown>")
86 }
87}
88
89#[macro_export]
92macro_rules! source_tag {
93 () => {{
94 const TAG: $crate::SourceTag = unsafe {
97 $crate::SourceTag::from_bytes_with_nul_unchecked(
98 concat!(file!(), ":", line!(), "\0").as_bytes(),
99 )
100 };
101 TAG
102 }};
103}
104
105mod kcell;
106mod kmutex;
107mod konce_cell;
108mod lock_token;
109mod phantom_mutex;
110mod raw_lock;
111mod singleton;
112
113#[cfg(not(feature = "kernel"))]
114mod raw_userspace_mutex;
115
116#[cfg(feature = "kernel")]
117mod raw_kernel_mutex;
118#[cfg(feature = "kernel")]
119mod raw_spin_lock;
120
121pub use kcell::{KCell, KCellInit, kcell_init};
122pub use konce_cell::{KOnceCell, KOnceCellGuard};
123#[cfg(any(feature = "kernel", test))]
124mod brwlock;
125
126#[cfg(feature = "kernel")]
127mod raw_kernel_brwlock;
128
129#[cfg(all(not(feature = "kernel"), test))]
130mod raw_userspace_brwlock;
131
132pub use kmutex::{
133 AliasedLock, KMutex, KMutexAliasedGuard, KMutexGuard, aliased_lock, aliased_lock_policy,
134 aliased_lock_policy_with, aliased_lock_with,
135};
136pub use lock_token::LockToken;
137pub use lockdep::{LOCK_FLAGS_SINGLETON_LOCK, LockClass, LockClassRegistration, LockFlags};
138pub use phantom_mutex::PhantomMutex;
139pub use raw_lock::{LockPolicy, RawLock};
140pub use singleton::SingletonMutex;
141
142#[cfg(not(feature = "kernel"))]
143pub use raw_userspace_mutex::RawMutex;
144
145#[cfg(not(feature = "kernel"))]
146pub type LockEntryStorage = ();
147
148#[cfg(feature = "kernel")]
149pub use raw_spin_lock::{
150 InterruptSavedState, IrqSavePolicy, MonitoredSpinlockGuardState, NoIrqSavePolicy,
151 RawMonitoredSpinlock, RawSpinlock,
152};
153#[cfg(feature = "kernel")]
154pub type KSpinlock<Class> = KMutex<Class, RawSpinlock>;
155#[cfg(feature = "kernel")]
156pub type KMonitoredSpinlock<Class> = KMutex<Class, RawMonitoredSpinlock>;
157#[cfg(any(feature = "kernel", test))]
158pub use brwlock::{BrwLockPi, BrwLockPiReadGuard, BrwLockPiWriteGuard};
159#[cfg(feature = "kernel")]
160pub use raw_kernel_brwlock::RawBrwLockPi;
161#[cfg(feature = "kernel")]
162pub use raw_kernel_mutex::{LockEntryStorage, RawCriticalMutex, RawMutex};
163#[cfg(all(not(feature = "kernel"), test))]
164pub use raw_userspace_brwlock::RawBrwLockPi;