2019-11-18 09:13:54 +08:00
|
|
|
use core::ops::{Deref, DerefMut};
|
2019-11-18 09:37:59 +08:00
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
2019-11-18 09:13:54 +08:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use super::asm::*;
|
|
|
|
|
2019-11-18 09:37:59 +08:00
|
|
|
const LOCKED: u32 = 1;
|
|
|
|
const UNLOCKED: u32 = 0;
|
2019-11-18 09:13:54 +08:00
|
|
|
|
|
|
|
pub struct Mutex<T> {
|
2019-11-18 09:37:59 +08:00
|
|
|
locked: AtomicU32,
|
2019-11-18 09:13:54 +08:00
|
|
|
inner: UnsafeCell<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Sync for Mutex<T> {}
|
|
|
|
unsafe impl<T: Send> Send for Mutex<T> {}
|
|
|
|
|
|
|
|
impl<T> Mutex<T> {
|
|
|
|
pub const fn new(inner: T) -> Self {
|
|
|
|
Mutex{
|
2019-11-18 09:37:59 +08:00
|
|
|
locked: AtomicU32::new(UNLOCKED),
|
2019-11-18 09:13:54 +08:00
|
|
|
inner: UnsafeCell::new(inner),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lock(&self) -> MutexGuard<T> {
|
2019-11-18 09:37:59 +08:00
|
|
|
while self.locked.compare_and_swap(UNLOCKED, LOCKED, Ordering::Acquire) != UNLOCKED {}
|
2019-11-18 09:13:54 +08:00
|
|
|
dmb();
|
|
|
|
MutexGuard { mutex: self }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unlock(&self) {
|
|
|
|
dmb();
|
2019-11-18 09:37:59 +08:00
|
|
|
self.locked.store(UNLOCKED, Ordering::Release);
|
|
|
|
dsb();
|
2019-11-18 09:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MutexGuard<'a, T> {
|
|
|
|
mutex: &'a Mutex<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Deref for MutexGuard<'a, T> {
|
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
unsafe { &*self.mutex.inner.get() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> DerefMut for MutexGuard<'a, T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
unsafe { &mut *self.mutex.inner.get() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Drop for MutexGuard<'a, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|