8 stable releases

Uses old Rust 2015

1.5.0 Jun 8, 2023
1.4.1 Mar 30, 2023
1.4.0 Nov 29, 2022
1.3.0 May 4, 2022
0.2.0 Nov 30, 2016

#520 in Algorithms

Download history 20099/week @ 2023-06-01 19834/week @ 2023-06-08 17908/week @ 2023-06-15 20726/week @ 2023-06-22 22723/week @ 2023-06-29 25239/week @ 2023-07-06 25086/week @ 2023-07-13 17663/week @ 2023-07-20 19065/week @ 2023-07-27 18799/week @ 2023-08-03 22239/week @ 2023-08-10 26962/week @ 2023-08-17 14485/week @ 2023-08-24 20975/week @ 2023-08-31 16947/week @ 2023-09-07 12505/week @ 2023-09-14

69,757 downloads per month
Used in 21 crates (7 directly)

MIT license

17KB
267 lines

rust-timerfd

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

Documentation Crates.io


lib.rs:

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.

Dependencies

~2–11MB
~121K SLoC