use core::{mem};
use crate::{
kty::{c_int, itimerspec, TFD_TIMER_ABSTIME},
syscall::{close, timerfd_settime, timerfd_gettime, read},
fd::{FdContainer},
util::retry::{retry},
time::{Time, time_to_timespec, time_from_timespec},
result::{Result},
lmem,
};
pub struct Timer {
fd: c_int,
owned: bool,
}
impl Timer {
pub fn disable(&self) -> Result {
let arg = lmem::zeroed();
rv!(timerfd_settime(self.fd, 0, &arg, None))
}
pub fn interval(&self, iv: Time) -> Result {
let arg = itimerspec {
it_interval: time_to_timespec(iv),
it_value: time_to_timespec(iv),
};
rv!(timerfd_settime(self.fd, 0, &arg, None))
}
pub fn interval_from(&self, iv: Time, start: Time) -> Result {
let arg = itimerspec {
it_interval: time_to_timespec(iv),
it_value: time_to_timespec(start),
};
rv!(timerfd_settime(self.fd, TFD_TIMER_ABSTIME, &arg, None))
}
pub fn interval_in(&self, iv: Time, when: Time) -> Result {
let arg = itimerspec {
it_interval: time_to_timespec(iv),
it_value: time_to_timespec(when),
};
rv!(timerfd_settime(self.fd, 0, &arg, None))
}
pub fn once_at(&self, when: Time) -> Result {
let arg = itimerspec {
it_interval: lmem::zeroed(),
it_value: time_to_timespec(when),
};
rv!(timerfd_settime(self.fd, TFD_TIMER_ABSTIME, &arg, None))
}
pub fn once_in(&self, when: Time) -> Result {
let arg = itimerspec {
it_interval: lmem::zeroed(),
it_value: time_to_timespec(when),
};
rv!(timerfd_settime(self.fd, 0, &arg, None))
}
pub fn status(&self) -> Result<(Time, Time)> {
let mut arg = lmem::zeroed();
rv!(timerfd_gettime(self.fd, &mut arg))?;
Ok((time_from_timespec(arg.it_interval), time_from_timespec(arg.it_value)))
}
pub fn ticks(&self) -> Result<u64> {
let mut buf = 0;
retry(|| read(self.fd, lmem::as_mut_data(&mut buf)))?;
Ok(buf)
}
}
impl Drop for Timer {
fn drop(&mut self) {
if self.owned {
close(self.fd);
}
}
}
impl From<Timer> for c_int {
fn from(timer: Timer) -> Self {
let fd = timer.fd;
mem::forget(timer);
fd
}
}
impl FdContainer for Timer {
fn is_owned(&self) -> bool {
self.owned
}
fn borrow(&self) -> c_int {
self.fd
}
fn from_owned(fd: c_int) -> Timer {
Timer { fd, owned: true }
}
fn from_borrowed(fd: c_int) -> Timer {
Timer { fd, owned: false }
}
}