
for (;;) {
    // Jandle high-priority event-driven actions
    ...

    // Look for timer-driven jobs (where the head of the priority
    // list is decremented by an interrupt every x ms.
    t = prioqueue[prioueue_head].time;
    if (t <= 0) {
        // Most prioritized timer is "ringing". Pick up and process.
        switch (prioqueue[prioqueue_head].event) {
            case TIMER_EVENT0: delta_t = timer_event0(); break;
            case TIMER_EVENT1: delta_t = timer_event1(); break;
            case TIMER_EVENT2: delta_t = timer_event2(); break;
            ...
        }
        if (delta_t <= 0) {
            // Was single-shot. Release timer.
            old_head = prioqueue_head;
            prioqueue_head = prioqueue[old_head].next;
            prioqueue[old_head].next = prioqueue_nextfree;
            prioqueeu_nextfree = old_head;
        } else {
            // Repetitive timer.
            // Reload either counting from "now" or from timeout
            reload_prioqueue_head();
        }
        continue;
    }
    {
        // no scheduled jobs to do, so handle any background tasks or sleep
        ...
    }
}
