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/// For a higher-level API to timerfd functionality, see the [timerfd] crate.
23///
24/// [timerfd]: https://crates.io/crates/timerfd
25///
26/// # References
27/// - [Linux]
28///
29/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_create.2.html
30#[inline]
31pub fn timerfd_create(clockid: TimerfdClockId, flags: TimerfdFlags) -> io::Result<OwnedFd> {
32 backend::time::syscalls::timerfd_create(clockid, flags)
33}
34
35/// `timerfd_settime(clockid, flags, new_value)`—Set the time on a timer.
36///
37/// # References
38/// - [Linux]
39///
40/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_settime.2.html
41#[inline]
42pub fn timerfd_settime<Fd: AsFd>(
43 fd: Fd,
44 flags: TimerfdTimerFlags,
45 new_value: &Itimerspec,
46) -> io::Result<Itimerspec> {
47 backend::time::syscalls::timerfd_settime(fd.as_fd(), flags, new_value)
48}
49
50/// `timerfd_gettime(clockid, flags)`—Query a timer.
51///
52/// # References
53/// - [Linux]
54///
55/// [Linux]: https://man7.org/linux/man-pages/man2/timerfd_gettime.2.html
56#[inline]
57pub fn timerfd_gettime<Fd: AsFd>(fd: Fd) -> io::Result<Itimerspec> {
58 backend::time::syscalls::timerfd_gettime(fd.as_fd())
59}