use super::Op;
use crate::future::Future;
pub enum DelayType {
Absolute,
Relative,
}
pub struct Delay {
duration: lx::timespec,
ty: DelayType,
}
unsafe impl Op for Delay {
type Output = lx::Result<()>;
fn fill_sqe(&mut self, sqe: &mut lx::io_uring_sqe) {
sqe.opcode = lx::IORING_OP_TIMEOUT;
sqe.flags = match self.ty {
DelayType::Relative => 0,
DelayType::Absolute => u8::try_from(lx::IORING_TIMEOUT_ABS).unwrap(),
};
sqe.addr = &self.duration as *const lx::timespec as u64;
sqe.len = 1;
}
fn complete(self, ret: i32) -> Self::Output {
if let Ok(err) = lx::Error::try_from(ret) {
if err.code() != lx::ETIME {
return Err(err);
}
}
Ok(())
}
}
pub fn delay(duration: lx::timespec, ty: DelayType) -> Future<Delay> {
Future::new(Delay { duration, ty })
}