Preparing your design workspace

Loading the problem and your latest saved attempt…

Design an Embedded Thread Sleep and Wakeup Service

Design timed thread blocking, early wakeup, timer interrupts, and low-power idle for a small embedded kernel.

Problem statement

For this exercise, assume a single-core embedded device runs a small preemptive kernel. A thread may call sleep(duration_ms) to stop running until its monotonic deadline; an authorized kernel caller may wake that specific sleep early.

When no thread is ready, the kernel should use a low-power idle mode while still waking for the next deadline or an external interrupt. Design the timer, scheduler, wakeup and power-management boundaries behind this behavior, including the path into and out of an interrupt.

A sleep must not complete as elapsed before its deadline; an early wake and a deadline expiration must produce exactly one completion reason. Use the bounded workload and timing targets below as exercise assumptions.

The problem excludes distributed coordination, wall-clock time, persistence across reset, and a particular chip or RTOS API.

What to build

  1. A running thread can request a monotonic delay and yield the CPU; when its deadline expires it becomes ready and the call eventually returns ELAPSED.
  2. An authorized caller can wake one currently sleeping thread early; exactly one of ELAPSED or WOKEN completes that sleep, and repeated wake requests are harmless.
  3. When no thread is ready, the kernel can enter a compatible low-power mode and resume on the next timer deadline or an external interrupt without losing elapsed time.

At scale

  • For this exercise, assume at most 128 threads and 64 outstanding sleeps, each with a requested delay from 1 ms through 24 hours; kernel sleep bookkeeping has a 12 KiB RAM budget and no allocation after boot.
  • For this exercise, assume a 1 ms monotonic time unit and target moving an expired sleep to the ready queue within 2 ms of its deadline under healthy admitted load. Actual dispatch may wait behind other runnable threads. A sleep must never return ELAPSED early, including across hardware counter wrap.
  • For this exercise, assume an always-on wake timer and an external interrupt can both wake light idle. Consider light idle when the ready queue is empty and the next known timer deadline is at least 5 ms away, or no timer deadline exists; an external interrupt may wake it sooner. A deeper mode that stops the timer is optional and requires an alternative clock or a shorter safe idle choice.