MutexSupport framework

master
WangRunji 7 years ago
parent eaaace0d48
commit 71e49e3959

@ -1,6 +1,6 @@
use memory::MemoryController; use memory::MemoryController;
use spin::Once; use spin::Once;
use sync::Mutex; use sync::SpinNoIrqLock;
use core::slice; use core::slice;
use alloc::String; use alloc::String;
@ -13,7 +13,8 @@ mod scheduler;
pub fn init(mut mc: MemoryController) { pub fn init(mut mc: MemoryController) {
PROCESSOR.call_once(|| {Mutex::new({ PROCESSOR.call_once(|| {
SpinNoIrqLock::new({
let initproc = Process::new_init(&mut mc); let initproc = Process::new_init(&mut mc);
let idleproc = Process::new("idle", idle_thread, 0, &mut mc); let idleproc = Process::new("idle", idle_thread, 0, &mut mc);
let mut processor = Processor::new(); let mut processor = Processor::new();
@ -21,11 +22,11 @@ pub fn init(mut mc: MemoryController) {
processor.add(idleproc); processor.add(idleproc);
processor processor
})}); })});
MC.call_once(|| Mutex::new(mc)); MC.call_once(|| SpinNoIrqLock::new(mc));
} }
pub static PROCESSOR: Once<Mutex<Processor>> = Once::new(); pub static PROCESSOR: Once<SpinNoIrqLock<Processor>> = Once::new();
pub static MC: Once<Mutex<MemoryController>> = Once::new(); pub static MC: Once<SpinNoIrqLock<MemoryController>> = Once::new();
extern fn idle_thread(arg: usize) -> ! { extern fn idle_thread(arg: usize) -> ! {
loop { loop {

@ -1,37 +1,42 @@
//! Spin & no-interrupt lock //! Mutex (Spin, Spin-NoInterrupt, Yield)
//! //!
//! Modified from spin::mutex. //! Modified from spin::mutex.
//! Search 'interrupt::' for difference.
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering}; use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::fmt; use core::fmt;
use core::marker::PhantomData;
use arch::interrupt; use arch::interrupt;
pub type SpinLock<T> = Mutex<T, Spin>;
pub type SpinNoIrqLock<T> = Mutex<T, SpinNoIrq>;
pub type YieldLock<T> = Mutex<T, Yield>;
/// Spin & no-interrupt lock /// Spin & no-interrupt lock
pub struct Mutex<T: ?Sized> pub struct Mutex<T: ?Sized, S: MutexSupport>
{ {
lock: AtomicBool, lock: AtomicBool,
support: PhantomData<S>,
data: UnsafeCell<T>, data: UnsafeCell<T>,
} }
/// A guard to which the protected data can be accessed /// A guard to which the protected data can be accessed
/// ///
/// When the guard falls out of scope it will release the lock. /// When the guard falls out of scope it will release the lock.
pub struct MutexGuard<'a, T: ?Sized + 'a> pub struct MutexGuard<'a, T: ?Sized + 'a, S: MutexSupport>
{ {
lock: &'a AtomicBool, lock: &'a AtomicBool,
data: &'a mut T, data: &'a mut T,
flags: usize, support: S,
} }
// Same unsafe impls as `std::sync::Mutex` // Same unsafe impls as `std::sync::Mutex`
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {} unsafe impl<T: ?Sized + Send, S: MutexSupport> Sync for Mutex<T, S> {}
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} unsafe impl<T: ?Sized + Send, S: MutexSupport> Send for Mutex<T, S> {}
impl<T> Mutex<T> impl<T, S: MutexSupport> Mutex<T, S>
{ {
/// Creates a new spinlock wrapping the supplied data. /// Creates a new spinlock wrapping the supplied data.
/// ///
@ -49,10 +54,11 @@ impl<T> Mutex<T>
/// drop(lock); /// drop(lock);
/// } /// }
/// ``` /// ```
pub const fn new(user_data: T) -> Mutex<T> { pub const fn new(user_data: T) -> Mutex<T, S> {
Mutex { Mutex {
lock: ATOMIC_BOOL_INIT, lock: ATOMIC_BOOL_INIT,
data: UnsafeCell::new(user_data), data: UnsafeCell::new(user_data),
support: PhantomData,
} }
} }
@ -65,13 +71,13 @@ impl<T> Mutex<T>
} }
} }
impl<T: ?Sized> Mutex<T> impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
{ {
fn obtain_lock(&self) { fn obtain_lock(&self) {
while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false { while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false {
// Wait until the lock looks unlocked before retrying // Wait until the lock looks unlocked before retrying
while self.lock.load(Ordering::Relaxed) { while self.lock.load(Ordering::Relaxed) {
unsafe { asm!("pause" :::: "volatile"); } S::cpu_relax();
} }
} }
} }
@ -91,14 +97,14 @@ impl<T: ?Sized> Mutex<T>
/// } /// }
/// ///
/// ``` /// ```
pub fn lock(&self) -> MutexGuard<T> pub fn lock(&self) -> MutexGuard<T, S>
{ {
let flags = unsafe { interrupt::disable_and_store() }; let support = S::before_lock();
self.obtain_lock(); self.obtain_lock();
MutexGuard { MutexGuard {
lock: &self.lock, lock: &self.lock,
data: unsafe { &mut *self.data.get() }, data: unsafe { &mut *self.data.get() },
flags, support,
} }
} }
@ -115,53 +121,111 @@ impl<T: ?Sized> Mutex<T>
/// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns /// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns
/// a guard within Some. /// a guard within Some.
pub fn try_lock(&self) -> Option<MutexGuard<T>> { pub fn try_lock(&self) -> Option<MutexGuard<T, S>> {
let flags = unsafe { interrupt::disable_and_store() }; let support = S::before_lock();
if self.lock.compare_and_swap(false, true, Ordering::Acquire) == false { if self.lock.compare_and_swap(false, true, Ordering::Acquire) == false {
Some(MutexGuard { Some(MutexGuard {
lock: &self.lock, lock: &self.lock,
data: unsafe { &mut *self.data.get() }, data: unsafe { &mut *self.data.get() },
flags, support,
}) })
} else { } else {
unsafe { interrupt::restore(flags) }; support.after_unlock();
None None
} }
} }
} }
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> impl<T: ?Sized + fmt::Debug, S: MutexSupport> fmt::Debug for Mutex<T, S>
{ {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() { match self.try_lock() {
Some(guard) => write!(f, "Mutex {{ data: {:?} }}", &*guard), Some(guard) => write!(f, "Mutex<{:?}> {{ data: {:?} }}", self.support, &*guard),
None => write!(f, "Mutex {{ <locked> }}"), None => write!(f, "Mutex<{:?}> {{ <locked> }}", self.support),
} }
} }
} }
impl<T: ?Sized + Default> Default for Mutex<T> { impl<T: ?Sized + Default, S: MutexSupport> Default for Mutex<T, S> {
fn default() -> Mutex<T> { fn default() -> Mutex<T, S> {
Mutex::new(Default::default()) Mutex::new(Default::default())
} }
} }
impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> impl<'a, T: ?Sized, S: MutexSupport> Deref for MutexGuard<'a, T, S>
{ {
type Target = T; type Target = T;
fn deref<'b>(&'b self) -> &'b T { &*self.data } fn deref<'b>(&'b self) -> &'b T { &*self.data }
} }
impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> impl<'a, T: ?Sized, S: MutexSupport> DerefMut for MutexGuard<'a, T, S>
{ {
fn deref_mut<'b>(&'b mut self) -> &'b mut T { &mut *self.data } fn deref_mut<'b>(&'b mut self) -> &'b mut T { &mut *self.data }
} }
impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> impl<'a, T: ?Sized, S: MutexSupport> Drop for MutexGuard<'a, T, S>
{ {
/// The dropping of the MutexGuard will release the lock it was created from. /// The dropping of the MutexGuard will release the lock it was created from.
fn drop(&mut self) { fn drop(&mut self) {
self.lock.store(false, Ordering::Release); self.lock.store(false, Ordering::Release);
self.support.after_unlock();
}
}
/// Low-level support for mutex
pub trait MutexSupport {
/// Called when failing to acquire the lock
fn cpu_relax();
/// Called before lock() & try_lock()
fn before_lock() -> Self;
/// Called when MutexGuard dropping & try_lock() failed
fn after_unlock(&self);
}
/// Spin lock
pub struct Spin;
impl MutexSupport for Spin {
fn cpu_relax() {
unsafe { asm!("pause" :::: "volatile"); }
}
fn before_lock() -> Self {
Spin
}
fn after_unlock(&self) {}
}
/// Spin & no-interrupt lock
pub struct SpinNoIrq {
flags: usize,
}
impl MutexSupport for SpinNoIrq {
fn cpu_relax() {
unsafe { asm!("pause" :::: "volatile"); }
}
fn before_lock() -> Self {
SpinNoIrq {
flags: unsafe { interrupt::disable_and_store() },
}
}
fn after_unlock(&self) {
unsafe { interrupt::restore(self.flags) }; unsafe { interrupt::restore(self.flags) };
} }
} }
/// With thread support
pub struct Yield;
impl MutexSupport for Yield {
fn cpu_relax() {
use thread;
thread::yield_now();
}
fn before_lock() -> Self {
unimplemented!()
}
fn after_unlock(&self) {
unimplemented!()
}
}
Loading…
Cancel
Save