feat: Aurora Neural OS v2.6 x86_64 microkernel, white theme GUI, and page replacement research
d097788 verified Download CA6_Rust_Micro_OS_Project/src/sync.rs from vedraj1309/oslab: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/datasets/vedraj1309/oslab/resolve/main/CA6_Rust_Micro_OS_Project/src/sync.rs
- Command line
-
hf download hf://datasets/vedraj1309/oslab/CA6_Rust_Micro_OS_Project/src/sync.rs
-
curl -L -o sync.rs https://huggingface.co/datasets/vedraj1309/oslab/resolve/main/CA6_Rust_Micro_OS_Project/src/sync.rs
1.35 kB
| use std::cell::UnsafeCell; | |
| use std::sync::atomic::{AtomicBool, Ordering}; | |
| pub struct SpinLock<T> { | |
| locked: AtomicBool, | |
| value: UnsafeCell<T>, | |
| } | |
| // The lock serializes access to the value between threads. | |
| unsafe impl<T: Send> Sync for SpinLock<T> {} | |
| unsafe impl<T: Send> Send for SpinLock<T> {} | |
| pub struct SpinLockGuard<'a, T> { | |
| lock: &'a SpinLock<T>, | |
| } | |
| impl<T> SpinLock<T> { | |
| pub const fn new(value: T) -> Self { | |
| Self { | |
| locked: AtomicBool::new(false), | |
| value: UnsafeCell::new(value), | |
| } | |
| } | |
| pub fn lock(&self) -> SpinLockGuard<'_, T> { | |
| while self | |
| .locked | |
| .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) | |
| .is_err() | |
| { | |
| std::hint::spin_loop(); | |
| } | |
| SpinLockGuard { lock: self } | |
| } | |
| } | |
| impl<T> std::ops::Deref for SpinLockGuard<'_, T> { | |
| type Target = T; | |
| fn deref(&self) -> &Self::Target { | |
| unsafe { &*self.lock.value.get() } | |
| } | |
| } | |
| impl<T> std::ops::DerefMut for SpinLockGuard<'_, T> { | |
| fn deref_mut(&mut self) -> &mut Self::Target { | |
| unsafe { &mut *self.lock.value.get() } | |
| } | |
| } | |
| impl<T> Drop for SpinLockGuard<'_, T> { | |
| fn drop(&mut self) { | |
| self.lock.locked.store(false, Ordering::Release); | |
| } | |
| } | |