Simvik

Rollover-safe timeout

Decide whether a timeout has expired without being fooled by wrap-around. An easy embedded C problem (Timing, interrupts and concurrency), run and graded in…

easy · Timing, interrupts and concurrency · ticks · timeout

Return true once at least timeout ticks have passed since start.

The starter compares against an absolute deadline. It works until start + timeout wraps past zero.

Starter code

bool timeout_expired(uint32_t now, uint32_t start, uint32_t timeout) {
  return now >= start + timeout;
}

Example tests

Exactly on time

printf("%d\n", timeout_expired(1500, 1000, 500));

Expected output:

1

One tick early

printf("%d\n", timeout_expired(1499, 1000, 500));

Expected output:

0

Learn it first

More Timing, interrupts and concurrency problems