Rust timerfd CLOCK_MONOTONIC equivalent

Viewed 24

In my Linux Rust program I would like to wait for the system monotonic clock to reach a specified value. (The Linux monotonic clock counts the elapsed time since system boot).

Here is the C implementation of the desired function, error checking omitted:

void sleep_until_monotonic(int64_t ts)
{
  int fd = timerfd_create(CLOCK_MONOTONIC, 0);

  struct itimerspec its = { .it_value = { .tv_sec = ts }};
  timerfd_settime(fd, TFD_TIMER_ABSTIME, &its, NULL);

  struct pollfd pfd = { .fd = fd, .events = POLLIN };
  poll(&pfd, 1, -1);
  close(fd);
}

Here is a sketch of the Rust API I'm imagining:

  some_lib::sleep_until_monotonic(1234).await?

Is something like this possible in Rust? A Linux-only solution is OK but of course if there is a cross-platform solution available that would be great.

0 Answers
Related