rustix/time/timerfd.rs
1use crate::fd::{AsFd, OwnedFd};
2use crate::timespec::Timespec;
3use crate::{backend, io};
4
5pub use backend::time::types::{TimerfdClockId, TimerfdFlags, TimerfdTimerFlags};
6
7/// `struct itimerspec` for use with [`timerfd_gettime`] and
8/// [`timerfd_settime`].
9///
10/// [`timerfd_gettime`]: crate::time::timerfd_gettime
11/// [`timerfd_settime`]: crate::time::timerfd_settime
12#[derive(Debug, Clone)]
13pub struct Itimerspec {
14 /// Interval between times.
15 pub it_interval: Timespec,
16 /// Value of the time.
17 pub it_value: Timespec,
18}
19
20/// `timerfd_create(clockid, flags)`—Create a timer.
21///
22/// # References
23/// - [Linux]
24///
25/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_create.2.html
26#[inline]
27pub fn timerfd_create(clockid: TimerfdClockId, flags: TimerfdFlags) -> io::Result<OwnedFd> {
28 backend::time::syscalls::timerfd_create(clockid, flags)
29}
30
31/// `timerfd_settime(clockid, flags, new_value)`—Set the time on a timer.
32///
33/// # References
34/// - [Linux]
35///
36/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_settime.2.html
37#[inline]
38pub fn timerfd_settime<Fd: AsFd>(
39 fd: Fd,
40 flags: TimerfdTimerFlags,
41 new_value: &Itimerspec,
42) -> io::Result<Itimerspec> {
43 backend::time::syscalls::timerfd_settime(fd.as_fd(), flags, new_value)
44}
45
46/// `timerfd_gettime(clockid, flags)`—Query a timer.
47///
48/// # References
49/// - [Linux]
50///
51/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_gettime.2.html
52#[inline]
53pub fn timerfd_gettime<Fd: AsFd>(fd: Fd) -> io::Result<Itimerspec> {
54 backend::time::syscalls::timerfd_gettime(fd.as_fd())
55}