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

#96 in Unix APIs

Download history 6249/week @ 2024-09-07 7501/week @ 2024-09-14 6530/week @ 2024-09-21 7242/week @ 2024-09-28 10398/week @ 2024-10-05 14533/week @ 2024-10-12 15292/week @ 2024-10-19 13580/week @ 2024-10-26 11519/week @ 2024-11-02 12068/week @ 2024-11-09 7122/week @ 2024-11-16 9485/week @ 2024-11-23 8734/week @ 2024-11-30 10787/week @ 2024-12-07 7117/week @ 2024-12-14 2828/week @ 2024-12-21

31,898 downloads per month
Used in 30 crates (8 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

~1.5–9MB
~107K SLoC