#linux-kernel #linux-api #timer #oneshot #state

timerfd

A rust interface to the Linux kernel's timerfd API

9 stable releases

Uses old Rust 2015

1.6.0 Mar 21, 2024
1.5.0 Jun 8, 2023
1.4.1 Mar 30, 2023
1.4.0 Nov 29, 2022
0.2.0 Nov 30, 2016

#553 in Unix APIs

Download history 17266/week @ 2025-10-20 20557/week @ 2025-10-27 25807/week @ 2025-11-03 24722/week @ 2025-11-10 27550/week @ 2025-11-17 27677/week @ 2025-11-24 26209/week @ 2025-12-01 35616/week @ 2025-12-08 33753/week @ 2025-12-15 18589/week @ 2025-12-22 20244/week @ 2025-12-29 30461/week @ 2026-01-05 33883/week @ 2026-01-12 34562/week @ 2026-01-19 48941/week @ 2026-01-26 86266/week @ 2026-02-02

206,853 downloads per month
Used in 33 crates (9 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–15MB
~167K SLoC