pub struct TimerFd { /* private fields */ }
Expand description
A timerfd instance. This is also a file descriptor, you can feed it to
other interfaces taking file descriptors as arguments, epoll
for example.
Implementations§
source§impl TimerFd
impl TimerFd
sourcepub fn new(clockid: ClockId, flags: TimerFlags) -> Result<Self>
pub fn new(clockid: ClockId, flags: TimerFlags) -> Result<Self>
Creates a new timer based on the clock defined by clockid
. The
underlying fd can be assigned specific flags with flags
(CLOEXEC,
NONBLOCK). The underlying fd will be closed on drop.
sourcepub fn set(
&self,
expiration: Expiration,
flags: TimerSetTimeFlags,
) -> Result<()>
pub fn set( &self, expiration: Expiration, flags: TimerSetTimeFlags, ) -> Result<()>
Sets a new alarm on the timer.
§Types of alarm
There are 3 types of alarms you can set:
-
one shot: the alarm will trigger once after the specified amount of time. Example: I want an alarm to go off in 60s and then disable itself.
-
interval: the alarm will trigger every specified interval of time. Example: I want an alarm to go off every 60s. The alarm will first go off 60s after I set it and every 60s after that. The alarm will not disable itself.
-
interval delayed: the alarm will trigger after a certain amount of time and then trigger at a specified interval. Example: I want an alarm to go off every 60s but only start in 1h. The alarm will first trigger 1h after I set it and then every 60s after that. The alarm will not disable itself.
§Relative vs absolute alarm
If you do not set any TimerSetTimeFlags
, then the TimeSpec
you pass
to the Expiration
you want is relative. If however you want an alarm
to go off at a certain point in time, you can set TFD_TIMER_ABSTIME
.
Then the one shot TimeSpec and the delay TimeSpec of the delayed
interval are going to be interpreted as absolute.
§Cancel on a clock change
If you set a TFD_TIMER_CANCEL_ON_SET
alongside TFD_TIMER_ABSTIME
and the clock for this timer is CLOCK_REALTIME
or CLOCK_REALTIME_ALARM
,
then this timer is marked as cancelable if the real-time clock undergoes
a discontinuous change.
§Disabling alarms
Note: Only one alarm can be set for any given timer. Setting a new alarm actually removes the previous one.
Note: Setting a one shot alarm with a 0s TimeSpec disables the alarm altogether.
sourcepub fn get(&self) -> Result<Option<Expiration>>
pub fn get(&self) -> Result<Option<Expiration>>
Get the parameters for the alarm currently set, if any.