aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / sync / spinlock.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Sovereign SpinLock for bare-metal `#![no_std]` concurrency. |
| 17 | |
| 18 | use core::cell::UnsafeCell; |
| 19 | use core::ops::{Deref, DerefMut}; |
| 20 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 21 | |
| 22 | /// A simple, sovereign spinlock for bare-metal concurrency. |
| 23 | pub struct SpinLock<T> { |
| 24 | locked: AtomicBool, |
| 25 | data: UnsafeCell<T>, |
| 26 | } |
| 27 | |
| 28 | unsafe impl<T: Send> Sync for SpinLock<T> {} |
| 29 | unsafe impl<T: Send> Send for SpinLock<T> {} |
| 30 | |
| 31 | impl<T> SpinLock<T> { |
| 32 | /// Create a new SpinLock protecting `data`. |
| 33 | pub const fn new(data: T) -> Self { |
| 34 | Self { |
| 35 | locked: AtomicBool::new(false), |
| 36 | data: UnsafeCell::new(data), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Acquire the spinlock, spinning until available. |
| 41 | pub fn lock(&self) -> SpinLockGuard<'_, T> { |
| 42 | while self.locked.swap(true, Ordering::Acquire) { |
| 43 | while self.locked.load(Ordering::Relaxed) { |
| 44 | core::hint::spin_loop(); |
| 45 | } |
| 46 | } |
| 47 | SpinLockGuard { lock: self } |
| 48 | } |
| 49 | |
| 50 | /// Try to acquire the spinlock without blocking. |
| 51 | pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T>> { |
| 52 | if !self.locked.swap(true, Ordering::Acquire) { |
| 53 | Some(SpinLockGuard { lock: self }) |
| 54 | } else { |
| 55 | None |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// RAII guard for `SpinLock`. |
| 61 | pub struct SpinLockGuard<'a, T> { |
| 62 | lock: &'a SpinLock<T>, |
| 63 | } |
| 64 | |
| 65 | impl<'a, T> Deref for SpinLockGuard<'a, T> { |
| 66 | type Target = T; |
| 67 | fn deref(&self) -> &Self::Target { |
| 68 | unsafe { &*self.lock.data.get() } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<'a, T> DerefMut for SpinLockGuard<'a, T> { |
| 73 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 74 | unsafe { &mut *self.lock.data.get() } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<'a, T> Drop for SpinLockGuard<'a, T> { |
| 79 | fn drop(&mut self) { |
| 80 | self.lock.locked.store(false, Ordering::Release); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[cfg(test)] |
| 85 | mod tests { |
| 86 | use super::*; |
| 87 | |
| 88 | #[test] |
| 89 | fn test_spinlock_basic() { |
| 90 | let lock = SpinLock::new(42); |
| 91 | { |
| 92 | let mut guard = lock.lock(); |
| 93 | assert_eq!(*guard, 42); |
| 94 | *guard += 1; |
| 95 | } |
| 96 | { |
| 97 | let guard = lock.lock(); |
| 98 | assert_eq!(*guard, 43); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn test_spinlock_try_lock() { |
| 104 | let lock = SpinLock::new(100); |
| 105 | let guard1 = lock.try_lock(); |
| 106 | assert!(guard1.is_some()); |
| 107 | let guard2 = lock.try_lock(); |
| 108 | assert!(guard2.is_none()); |
| 109 | drop(guard1); |
| 110 | let guard3 = lock.try_lock(); |
| 111 | assert!(guard3.is_some()); |
| 112 | } |
| 113 | } |