// This program will be discussed in the Project section. It extends
// interrupt.cc to occasionally disable and reenable interrupts.

#include <atomic>
#include <iostream>
#include "timer.hh"

using namespace std;

/* Atomic is a quick short cut here to make counter atomic for operations like incrementing
 * without having to worry about race conditions.
 */
atomic<size_t> counter(0);

void timer_interrupt_handler()
{
    cout << "Timer interrupt occurred with counter " << counter << endl;
}

int main(int argc, char *argv[])
{
    int toggle_interval = 1'000'000'000;
    size_t next_toggle = toggle_interval;
    
    timer_init(500000, timer_interrupt_handler);
    while (true) {
        counter++;
        
        // Enable and disable timer interrupts at regular intervals.
        if (counter >= next_toggle) {
            intr_enable(!intr_enabled());
            next_toggle += toggle_interval;
        }
    }
}
