2020-12-18

Timerfd

Creating a timer with libc:

let flags = libc::EFD_CLOEXEC | libc::EFD_NONBLOCK;
let clock_id = libc::CLOCK_MONOTONIC; 
let fd = libc::timerfd_create(clock_id, flags);

// Set the old value to null,
// as we are not updating a timer, but rather
// creating one.
let old_value = std::ptr::null();

// Expiration
// Start after ten seconds
let expiration = libc::timespec {
    tv_sec = 10i64,
    tv_nsec = 0i64,
};

// Interval
// Repeat every second
let interval = libc::timespec {
    tv_sec = 1i64,
    tv_nsec = 0i64,
};

let new_value = libc::itmerspec {
    it_value = expiration // initial expiration?
    it_interval = interval
};


libc::timerfd_settime(fd, new_value, old_value);

Monotonic clock means that the clock can never go backwards.