merge UEFI-FB driver for text mode console into mainline #2

Merged
Nivirx merged 12 commits from next into master 2023-03-18 20:39:28 -04:00
2 changed files with 57 additions and 58 deletions
Showing only changes of commit 86a2d4bd90 - Show all commits

View File

@@ -1,57 +0,0 @@
use core::arch::asm;
use core::sync::atomic::{AtomicBool, Ordering};
use core::cell::UnsafeCell;
pub struct Spinlock<T> {
locked: AtomicBool,
data: UnsafeCell<T>,
}
unsafe impl<T: Send> Sync for Spinlock<T> {}
impl<T> Spinlock<T> {
pub const fn new(data: T) -> Self {
Self {
locked: AtomicBool::new(false),
data: UnsafeCell::new(data),
}
}
pub fn lock(&self) -> SpinlockGuard<T> {
while let Err(_) = self.locked.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) {
while self.locked.load(Ordering::Relaxed) {
// Spin until the lock is released
unsafe { asm!("pause") };
}
}
SpinlockGuard {
spinlock: &self,
}
}
}
pub struct SpinlockGuard<'a, T> {
spinlock: &'a Spinlock<T>,
}
impl<'a, T> core::ops::Deref for SpinlockGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.spinlock.data.get() }
}
}
impl<'a, T> core::ops::DerefMut for SpinlockGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.spinlock.data.get() }
}
}
impl<'a, T> Drop for SpinlockGuard<'a, T> {
fn drop(&mut self) {
self.spinlock.locked.store(false, Ordering::Release);
}
}

View File

@@ -1 +1,57 @@
pub(crate) mod Spinlock;
use core::arch::asm;
use core::sync::atomic::{AtomicBool, Ordering};
use core::cell::UnsafeCell;
pub struct Spinlock<T> {
locked: AtomicBool,
data: UnsafeCell<T>,
}
unsafe impl<T: Send> Sync for Spinlock<T> {}
impl<T> Spinlock<T> {
pub const fn new(data: T) -> Self {
Self {
locked: AtomicBool::new(false),
data: UnsafeCell::new(data),
}
}
pub fn lock(&self) -> SpinlockGuard<T> {
while let Err(_) = self.locked.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) {
while self.locked.load(Ordering::Relaxed) {
// Spin until the lock is released
unsafe { asm!("pause") };
}
}
SpinlockGuard {
spinlock: &self,
}
}
}
pub struct SpinlockGuard<'a, T> {
spinlock: &'a Spinlock<T>,
}
impl<'a, T> core::ops::Deref for SpinlockGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.spinlock.data.get() }
}
}
impl<'a, T> core::ops::DerefMut for SpinlockGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.spinlock.data.get() }
}
}
impl<'a, T> Drop for SpinlockGuard<'a, T> {
fn drop(&mut self) {
self.spinlock.locked.store(false, Ordering::Release);
}
}