nix/sys/timer.rs
1//! Timer API via signals.
2//!
3//! Timer is a POSIX API to create timers and get expiration notifications
4//! through queued Unix signals, for the current process. This is similar to
5//! Linux's timerfd mechanism, except that API is specific to Linux and makes
6//! use of file polling.
7//!
8//! For more documentation, please read [timer_create](https://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html).
9//!
10//! # Examples
11//!
12//! Create an interval timer that signals SIGALARM every 250 milliseconds.
13//!
14//! ```no_run
15//! use nix::sys::signal::{self, SigEvent, SigHandler, SigevNotify, Signal};
16//! use nix::sys::timer::{Expiration, Timer, TimerSetTimeFlags};
17//! use nix::time::ClockId;
18//! use std::convert::TryFrom;
19//! use std::sync::atomic::{AtomicU64, Ordering};
20//! use std::thread::yield_now;
21//! use std::time::Duration;
22//!
23//! const SIG: Signal = Signal::SIGALRM;
24//! static ALARMS: AtomicU64 = AtomicU64::new(0);
25//!
26//! extern "C" fn handle_alarm(signal: libc::c_int) {
27//! let signal = Signal::try_from(signal).unwrap();
28//! if signal == SIG {
29//! ALARMS.fetch_add(1, Ordering::Relaxed);
30//! }
31//! }
32//!
33//! fn main() {
34//! let clockid = ClockId::CLOCK_MONOTONIC;
35//! let sigevent = SigEvent::new(SigevNotify::SigevSignal {
36//! signal: SIG,
37//! si_value: 0,
38//! });
39//!
40//! let mut timer = Timer::new(clockid, sigevent).unwrap();
41//! let expiration = Expiration::Interval(Duration::from_millis(250).into());
42//! let flags = TimerSetTimeFlags::empty();
43//! timer.set(expiration, flags).expect("could not set timer");
44//!
45//! let handler = SigHandler::Handler(handle_alarm);
46//! unsafe { signal::signal(SIG, handler) }.unwrap();
47//!
48//! loop {
49//! let alarms = ALARMS.load(Ordering::Relaxed);
50//! if alarms >= 10 {
51//! println!("total alarms handled: {}", alarms);
52//! break;
53//! }
54//! yield_now()
55//! }
56//! }
57//! ```
58use crate::sys::signal::SigEvent;
59use crate::sys::time::timer::TimerSpec;
60pub use crate::sys::time::timer::{Expiration, TimerSetTimeFlags};
61use crate::time::ClockId;
62use crate::{errno::Errno, Result};
63use core::mem;
64
65/// A Unix signal per-process timer.
66#[derive(Debug)]
67#[repr(transparent)]
68pub struct Timer(libc::timer_t);
69
70impl Timer {
71 /// Creates a new timer based on the clock defined by `clockid`. The details
72 /// of the signal and its handler are defined by the passed `sigevent`.
73 pub fn new(clockid: ClockId, mut sigevent: SigEvent) -> Result<Self> {
74 let mut timer_id: mem::MaybeUninit<libc::timer_t> = mem::MaybeUninit::uninit();
75 Errno::result(unsafe {
76 libc::timer_create(
77 clockid.as_raw(),
78 sigevent.as_mut_ptr(),
79 timer_id.as_mut_ptr(),
80 )
81 })
82 .map(|_| {
83 // SAFETY: libc::timer_create is responsible for initializing
84 // timer_id.
85 unsafe { Self(timer_id.assume_init()) }
86 })
87 }
88
89 /// Set a new alarm on the timer.
90 ///
91 /// # Types of alarm
92 ///
93 /// There are 3 types of alarms you can set:
94 ///
95 /// - one shot: the alarm will trigger once after the specified amount of
96 /// time.
97 /// Example: I want an alarm to go off in 60s and then disable itself.
98 ///
99 /// - interval: the alarm will trigger every specified interval of time.
100 /// Example: I want an alarm to go off every 60s. The alarm will first
101 /// go off 60s after I set it and every 60s after that. The alarm will
102 /// not disable itself.
103 ///
104 /// - interval delayed: the alarm will trigger after a certain amount of
105 /// time and then trigger at a specified interval.
106 /// Example: I want an alarm to go off every 60s but only start in 1h.
107 /// The alarm will first trigger 1h after I set it and then every 60s
108 /// after that. The alarm will not disable itself.
109 ///
110 /// # Relative vs absolute alarm
111 ///
112 /// If you do not set any `TimerSetTimeFlags`, then the `TimeSpec` you pass
113 /// to the `Expiration` you want is relative. If however you want an alarm
114 /// to go off at a certain point in time, you can set `TFD_TIMER_ABSTIME`.
115 /// Then the one shot TimeSpec and the delay TimeSpec of the delayed
116 /// interval are going to be interpreted as absolute.
117 ///
118 /// # Disabling alarms
119 ///
120 /// Note: Only one alarm can be set for any given timer. Setting a new alarm
121 /// actually removes the previous one.
122 ///
123 /// Note: Setting a one shot alarm with a 0s TimeSpec disable the alarm
124 /// altogether.
125 pub fn set(&mut self, expiration: Expiration, flags: TimerSetTimeFlags) -> Result<()> {
126 let timerspec: TimerSpec = expiration.into();
127 Errno::result(unsafe {
128 libc::timer_settime(
129 self.0,
130 flags.bits(),
131 timerspec.as_ref(),
132 core::ptr::null_mut(),
133 )
134 })
135 .map(drop)
136 }
137
138 /// Get the parameters for the alarm currently set, if any.
139 pub fn get(&self) -> Result<Option<Expiration>> {
140 let mut timerspec = TimerSpec::none();
141 Errno::result(unsafe { libc::timer_gettime(self.0, timerspec.as_mut()) }).map(|_| {
142 if timerspec.as_ref().it_interval.tv_sec == 0
143 && timerspec.as_ref().it_interval.tv_nsec == 0
144 && timerspec.as_ref().it_value.tv_sec == 0
145 && timerspec.as_ref().it_value.tv_nsec == 0
146 {
147 None
148 } else {
149 Some(timerspec.into())
150 }
151 })
152 }
153
154 /// Return the number of timers that have overrun
155 ///
156 /// Each timer is able to queue one signal to the process at a time, meaning
157 /// if the signal is not handled before the next expiration the timer has
158 /// 'overrun'. This function returns how many times that has happened to
159 /// this timer, up to `libc::DELAYTIMER_MAX`. If more than the maximum
160 /// number of overruns have happened the return is capped to the maximum.
161 pub fn overruns(&self) -> i32 {
162 unsafe { libc::timer_getoverrun(self.0) }
163 }
164}
165
166impl Drop for Timer {
167 fn drop(&mut self) {
168 if !std::thread::panicking() {
169 let result = Errno::result(unsafe { libc::timer_delete(self.0) });
170 if let Err(Errno::EINVAL) = result {
171 panic!("close of Timer encountered EINVAL");
172 }
173 }
174 }
175}