Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of a Irq_Mutex (see Issue#160) #162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ ticket_mutex = ["mutex"]
# Enables `FairMutex`.
fair_mutex = ["mutex"]

# Enables `IrqMutex`.
irq_mutex = ["mutex"]

# Enables the non-default ticket mutex implementation for `Mutex`.
use_ticket_mutex = ["mutex", "ticket_mutex"]

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ The crate comes with a few feature flags that you may wish to use.

- `spin_mutex` enables the `SpinMutex` type.

- `irq_mutex` enables the `IrqMutex` type.

- `ticket_mutex` enables the `TicketMutex` type.

- `use_ticket_mutex` switches to a ticket lock for the implementation of `Mutex`. This
Expand Down
45 changes: 45 additions & 0 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use core::arch::asm;
///Contains architecture specific interrupt mask and restore code
///
///



///
/// Masks all maskable interrupts and returns the previous Interrupt State
#[cfg(target_arch = "x86_64")]
#[inline(always)]
pub(crate) fn mask_interrupts() -> bool {

let mut flags: u64;
unsafe{
asm!{
"pushfw",
"popw {}",
"cli",
out(reg) flags
}
}

//Masks of all Bits except the Interrupt Flag
if flags & 0x200 > 0 {
return true;
}

false

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't portable, I think the critical-section crate should be used instead.


/// Restores the Interrupt State to its previous value
#[cfg(target_arch = "x86_64")]
#[inline(always)]
pub(crate) fn restore_interrupts(interrupts: bool) {
if interrupts {
unsafe{
asm!{
"sti",
"nop" //on x86_64 sti creates a Interrupt Shadow, the NOP contains this Sideeffect to the inline ASM
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that if this approach is every used for a RwLock-like primitive, it will be incorrect since one can multiple read guards in arbitrary orders.

3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ pub use relax::{RelaxStrategy, Spin};
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub use rwlock::RwLockReadGuard;

#[cfg(feature = "irq_mutex")]
mod interrupt;

// Avoid confusing inference errors by aliasing away the relax strategy parameter. Users that need to use a different
// relax strategy can do so by accessing the types through their fully-qualified path. This is a little bit horrible
// but sadly adding a default type parameter is *still* a breaking change in Rust (for understandable reasons).
Expand Down
23 changes: 17 additions & 6 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,36 @@ pub mod fair;
#[cfg_attr(docsrs, doc(cfg(feature = "fair_mutex")))]
pub use self::fair::{FairMutex, FairMutexGuard, Starvation};

#[cfg(feature = "irq_mutex")]
#[cfg_attr(docsrs, doc(cfg(feature = "irq_mutex")))]
pub mod interrupt;
#[cfg(feature = "irq_mutex")]
#[cfg_attr(docsrs, doc(cfg(feature = "spin_mutex")))]
pub use self::interrupt::{IrqMutex, IrqMutexGuard};

use crate::{RelaxStrategy, Spin};
use core::{
fmt,
ops::{Deref, DerefMut},
};

#[cfg(all(not(feature = "spin_mutex"), not(feature = "use_ticket_mutex")))]
compile_error!("The `mutex` feature flag was used (perhaps through another feature?) without either `spin_mutex` or `use_ticket_mutex`. One of these is required.");
#[cfg(all(not(feature = "spin_mutex"), not(feature = "use_ticket_mutex"), not(feature = "irq_mutex") ))]
compile_error!("The `mutex` feature flag was used (perhaps through another feature?) without either `spin_mutex`, `use_ticket_mutex` or `irq_mutex`. One of these is required.");

#[cfg(all(not(feature = "use_ticket_mutex"), feature = "spin_mutex"))]
#[cfg(all(not(any(feature = "use_ticket_mutex", feature = "irq_mutex")), feature = "spin_mutex"))]
type InnerMutex<T, R> = self::spin::SpinMutex<T, R>;
#[cfg(all(not(feature = "use_ticket_mutex"), feature = "spin_mutex"))]
#[cfg(all(not(any(feature = "use_ticket_mutex", feature = "irq_mutex")), feature = "spin_mutex"))]
type InnerMutexGuard<'a, T> = self::spin::SpinMutexGuard<'a, T>;

#[cfg(feature = "use_ticket_mutex")]
#[cfg(all(not(any(feature = "irq_mutex", feature = "spin_mutex")), feature = "use_ticket_mutex"))]
type InnerMutex<T, R> = self::ticket::TicketMutex<T, R>;
#[cfg(feature = "use_ticket_mutex")]
#[cfg(all(not(any(feature = "irq_mutex", feature = "spin_mutex")), feature = "use_ticket_mutex"))]
type InnerMutexGuard<'a, T> = self::ticket::TicketMutexGuard<'a, T>;

#[cfg(all(not(any(feature = "use_ticket_mutex", feature = "spin_mutex")), feature = "irq_mutex"))]
type InnerMutex<T, R> = self::interrupt::IrqMutex<T, R>;
#[cfg(all(not(any(feature = "use_ticket_mutex", feature = "spin_mutex")), feature = "irq_mutex"))]
type InnerMutexGuard<'a, T> = self::interrupt::IrqMutexGuard<'a, T>;
Copy link
Collaborator

@zesterer zesterer Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the IRQ mutex should be used as the default implementation. Spin mutexes are supported on every platform, and are still useful on those platforms (even and perhaps especially embedded platforms), and the IRQ mutex has different behaviour to the other mutexes when invoked in an interrupt. For this reason, it should just be its own stand-alone mutex and never accidentally the default implementation.

/// A spin-based lock providing mutually exclusive access to data.
///
/// The implementation uses either a ticket mutex or a regular spin mutex depending on whether the `spin_mutex` or
Expand Down
Loading