1 // Copyright 2025 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 use core::cell::UnsafeCell; 16 use core::ops::{Deref, DerefMut}; 17 18 use crate::arch::{Arch, ArchInterface}; 19 20 pub use crate::arch::BareSpinLock as BareSpinLockApi; 21 22 pub type BareSpinLock = <Arch as ArchInterface>::BareSpinLock; 23 24 pub struct SpinLockGuard<'lock, T> { 25 lock: &'lock SpinLock<T>, 26 _inner_guard: <BareSpinLock as BareSpinLockApi>::Guard<'lock>, 27 } 28 29 impl<T> Deref for SpinLockGuard<'_, T> { 30 type Target = T; 31 deref(&self) -> &T32 fn deref(&self) -> &T { 33 unsafe { &*self.lock.data.get() } 34 } 35 } 36 37 impl<T> DerefMut for SpinLockGuard<'_, T> { deref_mut(&mut self) -> &mut T38 fn deref_mut(&mut self) -> &mut T { 39 unsafe { &mut *self.lock.data.get() } 40 } 41 } 42 43 pub struct SpinLock<T> { 44 data: UnsafeCell<T>, 45 inner: BareSpinLock, 46 } 47 48 // As long as the inner type is `Send` the lock can be shared between threads. 49 unsafe impl<T: Send> Sync for SpinLock<T> {} 50 51 impl<T> SpinLock<T> { new(initial_value: T) -> Self52 pub const fn new(initial_value: T) -> Self { 53 Self { 54 data: UnsafeCell::new(initial_value), 55 inner: BareSpinLock::new(), 56 } 57 } 58 try_lock(&self) -> Option<SpinLockGuard<'_, T>>59 pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T>> { 60 self.inner.try_lock().map(|guard| SpinLockGuard { 61 lock: self, 62 _inner_guard: guard, 63 }) 64 } 65 lock(&self) -> SpinLockGuard<'_, T>66 pub fn lock(&self) -> SpinLockGuard<'_, T> { 67 let inner_guard = self.inner.lock(); 68 SpinLockGuard { 69 lock: self, 70 _inner_guard: inner_guard, 71 } 72 } 73 } 74