Skip to content

File eventQueueFeature.h

File List > features > eventQueueFeature.h

Go to the documentation of this file

#pragma once

#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <optional>


namespace jac {


template<class Next>
class EventQueueFeature : public Next {
private:
    std::deque<std::function<void()>> _scheduledFunctions;
    std::mutex _scheduledFunctionsMutex;
    std::condition_variable _scheduledFunctionsCondition;
public:
    std::optional<std::function<void()>> getEvent(bool wait) {
        std::unique_lock lock(_scheduledFunctionsMutex);
        if (wait && _scheduledFunctions.empty()) {
            _scheduledFunctionsCondition.wait(lock);
        }
        if (_scheduledFunctions.empty()) {
            return std::nullopt;
        }
        auto func = std::move(_scheduledFunctions.front());
        _scheduledFunctions.pop_front();
        lock.unlock();

        return func;
    }

    void scheduleEvent(std::function<void()> func) {
        {
            std::scoped_lock lock(_scheduledFunctionsMutex);
            _scheduledFunctions.push_back(std::move(func));
        }
        _scheduledFunctionsCondition.notify_one();
    }

    void notifyEventLoop() {
        _scheduledFunctionsCondition.notify_one();
    }

    ~EventQueueFeature() {
        notifyEventLoop();
        std::scoped_lock lock(_scheduledFunctionsMutex);
        _scheduledFunctions.clear();
    }
};


} // namespace jac