/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#ifndef TANGO_SERVER_SYNCHRONISEDQUEUE_H
#define TANGO_SERVER_SYNCHRONISEDQUEUE_H

#include <condition_variable>
#include <deque>
#include <memory>
#include <mutex>

namespace Tango
{
template <typename T>
class SynchronisedQueue
{
    std::condition_variable cv;
    std::mutex mutex;
    std::deque<T> values;

  public:
    void put(T value)
    {
        {
            std::scoped_lock lock{mutex};
            values.push_back(value);
        }
        cv.notify_one();
    }

    T get()
    {
        std::unique_lock<std::mutex> lock{mutex};
        cv.wait(lock, [this] { return !values.empty(); });
        auto value = values.front();
        values.pop_front();
        lock.unlock();
        return value;
    }
};
} // namespace Tango
#endif
