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:
@ -244,13 +244,11 @@ impl<I: Interrupt> IrqExecutor<I> {
|
||||
|
||||
init(unsafe { self.inner.spawner() });
|
||||
|
||||
self.irq.set_handler(
|
||||
|ctx| unsafe {
|
||||
let executor = &*(ctx as *const raw::Executor);
|
||||
executor.run_queued();
|
||||
},
|
||||
&self.inner as *const _ as _,
|
||||
);
|
||||
self.irq.set_handler(|ctx| unsafe {
|
||||
let executor = &*(ctx as *const raw::Executor);
|
||||
executor.run_queued();
|
||||
});
|
||||
self.irq.set_handler_context(&self.inner as *const _ as _);
|
||||
self.irq.enable();
|
||||
}
|
||||
}
|
||||
|
@ -38,10 +38,9 @@ pub unsafe trait Interrupt {
|
||||
#[doc(hidden)]
|
||||
unsafe fn __handler(&self) -> &'static Handler;
|
||||
|
||||
fn set_handler(&self, func: unsafe fn(*mut ()), ctx: *mut ()) {
|
||||
fn set_handler(&self, func: unsafe fn(*mut ())) {
|
||||
let handler = unsafe { self.__handler() };
|
||||
handler.func.store(func as *mut (), Ordering::Release);
|
||||
handler.ctx.store(ctx, Ordering::Release);
|
||||
}
|
||||
|
||||
fn remove_handler(&self) {
|
||||
@ -49,6 +48,11 @@ pub unsafe trait Interrupt {
|
||||
handler.func.store(ptr::null_mut(), Ordering::Release);
|
||||
}
|
||||
|
||||
fn set_handler_context(&self, ctx: *mut ()) {
|
||||
let handler = unsafe { self.__handler() };
|
||||
handler.ctx.store(ctx, Ordering::Release);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn enable(&self) {
|
||||
unsafe {
|
||||
|
@ -93,7 +93,8 @@ impl<'a, I: Interrupt> Drop for InterruptFuture<'a, I> {
|
||||
impl<'a, I: Interrupt> InterruptFuture<'a, I> {
|
||||
pub fn new(interrupt: &'a mut I) -> Self {
|
||||
interrupt.disable();
|
||||
interrupt.set_handler(Self::interrupt_handler, ptr::null_mut());
|
||||
interrupt.set_handler(Self::interrupt_handler);
|
||||
interrupt.set_handler_context(ptr::null_mut());
|
||||
interrupt.unpend();
|
||||
interrupt.enable();
|
||||
|
||||
@ -121,8 +122,10 @@ impl<'a, I: Interrupt> Future for InterruptFuture<'a, I> {
|
||||
|
||||
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
let s = unsafe { self.get_unchecked_mut() };
|
||||
let ctx = unsafe { executor::raw::task_from_waker(&cx.waker()).cast().as_ptr() };
|
||||
s.interrupt.set_handler(Self::interrupt_handler, ctx);
|
||||
s.interrupt.set_handler(Self::interrupt_handler);
|
||||
s.interrupt.set_handler_context(unsafe {
|
||||
executor::raw::task_from_waker(&cx.waker()).cast().as_ptr()
|
||||
});
|
||||
if s.interrupt.is_enabled() {
|
||||
Poll::Pending
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user