| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | str::{CStr, CStrExt as _},
|
| 9 | sync::lock::{Backend, Guard, Lock},
|
| 10 | sync::{LockClassKey, LockedBy},
|
| 11 | types::Opaque,
|
| 12 | };
|
| 13 | use core::{
|
| 14 | cell::UnsafeCell,
|
| 15 | marker::{PhantomData, PhantomPinned},
|
| 16 | pin::Pin,
|
| 17 | };
|
| 18 |
|
| 19 | |
| 20 | |
| 21 | |
| 22 | pub trait GlobalLockBackend {
|
| 23 | |
| 24 | const NAME: &'static CStr;
|
| 25 | |
| 26 | type Item: 'static;
|
| 27 | |
| 28 | type Backend: Backend + 'static;
|
| 29 | |
| 30 | fn get_lock_class() -> Pin<&'static LockClassKey>;
|
| 31 | }
|
| 32 |
|
| 33 | |
| 34 | |
| 35 | |
| 36 | pub struct GlobalLock<B: GlobalLockBackend> {
|
| 37 | inner: Lock<B::Item, B::Backend>,
|
| 38 | }
|
| 39 |
|
| 40 | impl<B: GlobalLockBackend> GlobalLock<B> {
|
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | pub const unsafe fn new(data: B::Item) -> Self {
|
| 48 | Self {
|
| 49 | inner: Lock {
|
| 50 | state: Opaque::uninit(),
|
| 51 | data: UnsafeCell::new(data),
|
| 52 | _pin: PhantomPinned,
|
| 53 | },
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | pub unsafe fn init(&'static self) {
|
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | unsafe {
|
| 71 | B::Backend::init(
|
| 72 | self.inner.state.get(),
|
| 73 | B::NAME.as_char_ptr(),
|
| 74 | B::get_lock_class().as_ptr(),
|
| 75 | )
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | |
| 80 | #[inline]
|
| 81 | pub fn lock(&'static self) -> GlobalGuard<B> {
|
| 82 | GlobalGuard {
|
| 83 | inner: self.inner.lock(),
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | |
| 88 | #[must_use = "if unused, the lock will be immediately unlocked"]
|
| 89 | #[inline]
|
| 90 | pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
|
| 91 | Some(GlobalGuard {
|
| 92 | inner: self.inner.try_lock()?,
|
| 93 | })
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | #[must_use = "the lock unlocks immediately when the guard is unused"]
|
| 101 | pub struct GlobalGuard<B: GlobalLockBackend> {
|
| 102 | inner: Guard<'static, B::Item, B::Backend>,
|
| 103 | }
|
| 104 |
|
| 105 | impl<B: GlobalLockBackend> core::ops::Deref for GlobalGuard<B> {
|
| 106 | type Target = B::Item;
|
| 107 |
|
| 108 | fn deref(&self) -> &Self::Target {
|
| 109 | &self.inner
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | impl<B: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<B>
|
| 114 | where
|
| 115 | B::Item: Unpin,
|
| 116 | {
|
| 117 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 118 | &mut self.inner
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | |
| 123 | |
| 124 | |
| 125 | pub struct GlobalLockedBy<T: ?Sized, B: GlobalLockBackend> {
|
| 126 | _backend: PhantomData<B>,
|
| 127 | value: UnsafeCell<T>,
|
| 128 | }
|
| 129 |
|
| 130 | |
| 131 | unsafe impl<T, B> Send for GlobalLockedBy<T, B>
|
| 132 | where
|
| 133 | T: ?Sized,
|
| 134 | B: GlobalLockBackend,
|
| 135 | LockedBy<T, B::Item>: Send,
|
| 136 | {
|
| 137 | }
|
| 138 |
|
| 139 | |
| 140 | unsafe impl<T, B> Sync for GlobalLockedBy<T, B>
|
| 141 | where
|
| 142 | T: ?Sized,
|
| 143 | B: GlobalLockBackend,
|
| 144 | LockedBy<T, B::Item>: Sync,
|
| 145 | {
|
| 146 | }
|
| 147 |
|
| 148 | impl<T, B: GlobalLockBackend> GlobalLockedBy<T, B> {
|
| 149 | |
| 150 | |
| 151 | |
| 152 | pub fn new(val: T) -> Self {
|
| 153 | Self {
|
| 154 | value: UnsafeCell::new(val),
|
| 155 | _backend: PhantomData,
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | impl<T: ?Sized, B: GlobalLockBackend> GlobalLockedBy<T, B> {
|
| 161 | |
| 162 | |
| 163 | |
| 164 | pub fn as_ref<'a>(&'a self, _guard: &'a GlobalGuard<B>) -> &'a T {
|
| 165 | |
| 166 | unsafe { &*self.value.get() }
|
| 167 | }
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | pub fn as_mut<'a>(&'a self, _guard: &'a mut GlobalGuard<B>) -> &'a mut T {
|
| 173 | |
| 174 | unsafe { &mut *self.value.get() }
|
| 175 | }
|
| 176 |
|
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | pub fn get_mut(&mut self) -> &mut T {
|
| 182 | self.value.get_mut()
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | #[macro_export]
|
| 261 | macro_rules! global_lock {
|
| 262 | {
|
| 263 | $(#[$meta:meta])* $pub:vis
|
| 264 | unsafe(uninit) static $name:ident: $kind:ident<$valuety:ty> = $value:expr;
|
| 265 | } => {
|
| 266 | #[doc = ::core::concat!(
|
| 267 | "Backend type used by [`",
|
| 268 | ::core::stringify!($name),
|
| 269 | "`](static@",
|
| 270 | ::core::stringify!($name),
|
| 271 | ")."
|
| 272 | )]
|
| 273 | #[allow(non_camel_case_types, unreachable_pub)]
|
| 274 | $pub enum $name {}
|
| 275 |
|
| 276 | impl $crate::sync::lock::GlobalLockBackend for $name {
|
| 277 | const NAME: &'static $crate::str::CStr = $crate::c_str!(::core::stringify!($name));
|
| 278 | type Item = $valuety;
|
| 279 | type Backend = $crate::global_lock_inner!(backend $kind);
|
| 280 |
|
| 281 | fn get_lock_class() -> Pin<&'static $crate::sync::LockClassKey> {
|
| 282 | $crate::static_lock_class!()
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | $(#[$meta])*
|
| 287 | $pub static $name: $crate::sync::lock::GlobalLock<$name> = {
|
| 288 | |
| 289 | let init: $valuety = $value;
|
| 290 |
|
| 291 | |
| 292 | |
| 293 | |
| 294 | unsafe { $crate::sync::lock::GlobalLock::new(init) }
|
| 295 | };
|
| 296 | };
|
| 297 | }
|
| 298 | pub use global_lock;
|
| 299 |
|
| 300 | #[doc(hidden)]
|
| 301 | #[macro_export]
|
| 302 | macro_rules! global_lock_inner {
|
| 303 | (backend Mutex) => {
|
| 304 | $crate::sync::lock::mutex::MutexBackend
|
| 305 | };
|
| 306 | (backend SpinLock) => {
|
| 307 | $crate::sync::lock::spinlock::SpinLockBackend
|
| 308 | };
|
| 309 | (backend SpinLockIrq) => {
|
| 310 | $crate::sync::lock::spinlock::SpinLockIrqBackend
|
| 311 | };
|
| 312 | }
|