use std::io;
use crate::DriverContext;
use dope_core::driver::control::ContextControl;
use dope_core::io::pipe::Pipe;
use o3::marker::ThreadBound;
use super::ffi::SignalState;
pub struct ShutdownTrigger {
pipe: Pipe,
}
impl ShutdownTrigger {
pub fn new() -> io::Result<Self> {
Ok(Self { pipe: Pipe::new()? })
}
pub fn try_register(&self, driver: &mut DriverContext<'_, '_>) -> io::Result<()> {
unsafe { driver.register_shutdown_fd(self.pipe.read_end()) }
}
pub fn try_clone(&self) -> io::Result<Self> {
Ok(Self {
pipe: self.pipe.try_clone()?,
})
}
pub fn fire(&self) -> io::Result<()> {
self.pipe.notify()
}
}
pub struct SignalShutdown {
state: SignalState,
_thread: ThreadBound,
}
impl SignalShutdown {
pub fn new() -> io::Result<Self> {
Ok(Self {
state: SignalState::new()?,
_thread: ThreadBound::NEW,
})
}
pub fn try_register(&self, driver: &mut DriverContext<'_, '_>) -> io::Result<()> {
cfg_select! {
target_os = "linux" => {
unsafe { driver.register_shutdown_fd(self.state.fd()) }
}
_ => {
let _ = driver;
unreachable!("SignalShutdown cannot be constructed on this target")
}
}
}
}