interrupt: Split set_handler context.

Since introducing the ctx pointer, the handler is now two words, so setting it can
race with the interrupt firing. On race it's possible for the new handler to be
alled with the old ctx pointer or viceversa.

Rather than documenting this, it's better to split the function in two to make it
obvious to the user that it's not atomic. The user can use a critical section, or
disable/enable the interrupt to avoid races if this is a concern.
This commit is contained in:
Dario Nieuwenhuis
2021-02-26 02:04:48 +01:00
parent 17cf301d4f
commit da91779117
10 changed files with 41 additions and 42 deletions

View File

@ -118,7 +118,7 @@ impl Gpiote {
// Enable interrupts
gpiote.events_port.write(|w| w);
gpiote.intenset.write(|w| w.port().set());
irq.set_handler(Self::on_irq, core::ptr::null_mut());
irq.set_handler(Self::on_irq);
irq.unpend();
irq.enable();

View File

@ -146,7 +146,7 @@ impl Qspi {
SIGNAL.reset();
qspi.intenset.write(|w| w.ready().set());
irq.set_handler(irq_handler, core::ptr::null_mut());
irq.set_handler(irq_handler);
irq.unpend();
irq.enable();

View File

@ -109,13 +109,11 @@ impl<T: Instance> RTC<T> {
// Wait for clear
while self.rtc.counter.read().bits() != 0 {}
self.irq.set_handler(
|ptr| unsafe {
let this = &*(ptr as *const () as *const Self);
this.on_interrupt();
},
self as *const _ as *mut _,
);
self.irq.set_handler(|ptr| unsafe {
let this = &*(ptr as *const () as *const Self);
this.on_interrupt();
});
self.irq.set_handler_context(self as *const _ as *mut _);
self.irq.unpend();
self.irq.enable();
}

View File

@ -119,7 +119,7 @@ where
.write(|w| w.endtx().set().txstopped().set().endrx().set().rxto().set());
// Register ISR
irq.set_handler(Self::on_irq, core::ptr::null_mut());
irq.set_handler(Self::on_irq);
irq.unpend();
irq.enable();

View File

@ -33,16 +33,14 @@ impl<S: PeripheralState> PeripheralMutex<S> {
irq.disable();
compiler_fence(Ordering::SeqCst);
irq.set_handler(
|p| {
// Safety: it's OK to get a &mut to the state, since
// - We're in the IRQ, no one else can't preempt us
// - We can't have preempted a with() call because the irq is disabled during it.
let state = unsafe { &mut *(p as *mut S) };
state.on_interrupt();
},
state.get() as *mut (),
);
irq.set_handler(|p| {
// Safety: it's OK to get a &mut to the state, since
// - We're in the IRQ, no one else can't preempt us
// - We can't have preempted a with() call because the irq is disabled during it.
let state = unsafe { &mut *(p as *mut S) };
state.on_interrupt();
});
irq.set_handler_context(state.get() as *mut ());
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
let state = unsafe { &mut *state.get() };