use std::sync::Arc;
use anyhow::Context;
use base::debug;
use base::error;
use base::Event;
use base::EventType;
use sync::Mutex;
use super::interrupter::Interrupter;
use crate::utils::EventHandler;
use crate::utils::EventLoop;
pub struct IntrResampleHandler {
interrupter: Arc<Mutex<Interrupter>>,
resample_evt: Event,
}
impl IntrResampleHandler {
pub fn start(
event_loop: &EventLoop,
interrupter: Arc<Mutex<Interrupter>>,
resample_evt: Event,
) -> Option<Arc<IntrResampleHandler>> {
let handler = Arc::new(IntrResampleHandler {
interrupter,
resample_evt,
});
let tmp_handler: Arc<dyn EventHandler> = handler.clone();
if let Err(e) = event_loop.add_event(
&handler.resample_evt,
EventType::Read,
Arc::downgrade(&tmp_handler),
) {
error!("cannot add intr resample handler to event loop: {}", e);
return None;
}
Some(handler)
}
}
impl EventHandler for IntrResampleHandler {
fn on_event(&self) -> anyhow::Result<()> {
self.resample_evt
.wait()
.context("cannot read resample evt")?;
let mut interrupter = self.interrupter.lock();
if !interrupter.event_ring_is_empty() {
debug!("irq resample re-assert irq event");
interrupter.interrupt().context("cannot send interrupt")?;
}
Ok(())
}
}