#include <why-spinlock.h>                              // <-- include it
#include <why-thread.h>
#include <why-irq.h>
#include <why-irqengine.h>
#include <why-logger.h>

#include <string>
#include <list>
#include <thread>
#include <print>
#include <unistd.h>

using namespace std::chrono_literals;


std::list<std::string> the_list;
Why::Spinlock the_lock;                                // <-- instantiate one

int main()
{
    Why::init();

    auto sensor_isr = [](int){
        the_lock.lock();                               // <-- use it
        the_list.push_back("some data");
        the_lock.unlock();                             // <--
    };
    Why::IRQ::connect(2, sensor_isr);

    auto device_buffer_empty = [](int){
        the_lock.lock();                               // <--
        if (! the_list.empty())
            the_list.pop_front();
        the_lock.unlock();                             // <--
    };

    Why::IRQ::connect(3, device_buffer_empty);

    size_t last_num = 0;
    while (true) {
        if (last_num != the_list.size()) {
            last_num = the_list.size();
            std::println("#elems: {}", the_list.size());
        }
        std::this_thread::sleep_for(0.5s);
    }
    return 0;
}
