Crate timerfd

source ·
Expand description

A rust interface to the Linux kernel’s timerfd API.

§Example

use timerfd::{TimerFd, TimerState, SetTimeFlags};
use std::time::Duration;

// Create a new timerfd
// (unwrap is actually fine here for most usecases)
let mut tfd = TimerFd::new().unwrap();

// The timer is initially disarmed
assert_eq!(tfd.get_state(), TimerState::Disarmed);

// Set the timer
tfd.set_state(TimerState::Oneshot(Duration::new(1, 0)), SetTimeFlags::Default);

// Observe that the timer is now set
match tfd.get_state() {
    TimerState::Oneshot(d) => println!("Remaining: {:?}", d),
    _ => unreachable!(),
}

// Wait for the remaining time
tfd.read();

// It was a oneshot timer, so it's now disarmed
assert_eq!(tfd.get_state(), TimerState::Disarmed);

§Usage

Unfortunately, this example can’t show why you would use timerfd in the first place: Because it creates a file descriptor that you can monitor with select(2), poll(2) and epoll(2).

In other words, the primary advantage this offers over any other timer implementation is that it implements the AsFd/AsRawFd traits.

The file descriptor becomes ready/readable whenever the timer expires.

Structs§

Enums§