-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support basic mutex based on SPIN
- Loading branch information
Showing
20 changed files
with
180 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod lazy_init; | ||
mod mutex_no_irq; | ||
|
||
pub use lazy_init::LazyInit; | ||
pub use mutex_no_irq::{MutexNoIrq, MutexNoIrqGuard}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use core::ops::{Deref, DerefMut}; | ||
use spin::{Mutex, MutexGuard}; | ||
|
||
use crate::irq::IRQ; | ||
|
||
pub struct MutexNoIrq<T: ?Sized> { | ||
lock: Mutex<T>, | ||
} | ||
|
||
/// Irq Status Struct. | ||
/// This structure contains the status of the current IRQ | ||
/// And it will restore irq status after dropping. | ||
struct IrqStatus { | ||
irq_enabled: bool, | ||
} | ||
|
||
/// Restore the IRQ status when dropping | ||
impl Drop for IrqStatus { | ||
fn drop(&mut self) { | ||
if self.irq_enabled { | ||
IRQ::int_enable(); | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<T: ?Sized + Send> Sync for MutexNoIrq<T> {} | ||
unsafe impl<T: ?Sized + Send> Send for MutexNoIrq<T> {} | ||
|
||
impl<T> MutexNoIrq<T> { | ||
pub const fn new(data: T) -> MutexNoIrq<T> { | ||
MutexNoIrq { | ||
lock: Mutex::new(data), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn into_inner(self) -> T { | ||
self.lock.into_inner() | ||
} | ||
} | ||
|
||
impl<T: ?Sized> MutexNoIrq<T> { | ||
#[inline] | ||
pub fn lock(&self) -> MutexNoIrqGuard<T> { | ||
loop { | ||
match self.try_lock() { | ||
Some(guard) => return guard, | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn is_locked(&self) -> bool { | ||
self.lock.is_locked() | ||
} | ||
|
||
pub unsafe fn force_unlock(&self) { | ||
self.lock.force_unlock() | ||
} | ||
|
||
#[inline] | ||
pub fn try_lock(&self) -> Option<MutexNoIrqGuard<T>> { | ||
if self.lock.is_locked() { | ||
return None; | ||
} | ||
let _irq_status = IrqStatus { | ||
irq_enabled: IRQ::int_enabled(), | ||
}; | ||
IRQ::int_disable(); | ||
self.lock | ||
.try_lock() | ||
.map(|guard| MutexNoIrqGuard { guard, _irq_status }) | ||
} | ||
} | ||
|
||
/// The Mutex Guard. | ||
pub struct MutexNoIrqGuard<'a, T: ?Sized + 'a> { | ||
guard: MutexGuard<'a, T>, | ||
_irq_status: IrqStatus, | ||
} | ||
|
||
impl<'a, T: ?Sized> Deref for MutexNoIrqGuard<'a, T> { | ||
type Target = T; | ||
|
||
fn deref<'b>(&'b self) -> &'b T { | ||
&*(self.guard) | ||
} | ||
} | ||
|
||
impl<'a, T: ?Sized> DerefMut for MutexNoIrqGuard<'a, T> { | ||
fn deref_mut<'b>(&'b mut self) -> &'b mut T { | ||
&mut *(self.guard) | ||
} | ||
} |
Oops, something went wrong.