/** * Copyright (c) 2019 Paul-Louis Ageneau * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #ifndef RTC_IMPL_QUEUE_H #define RTC_IMPL_QUEUE_H #include "common.hpp" #include #include #include #include #include namespace rtc::impl { template class Queue { public: using amount_function = std::function; Queue(size_t limit = 0, amount_function func = nullptr); ~Queue(); void stop(); bool running() const; bool empty() const; bool full() const; size_t size() const; // elements size_t amount() const; // amount void push(T element); optional pop(); optional peek(); optional exchange(T element); private: const size_t mLimit; size_t mAmount; std::queue mQueue; std::condition_variable mPushCondition; amount_function mAmountFunction; bool mStopping = false; mutable std::mutex mMutex; }; template Queue::Queue(size_t limit, amount_function func) : mLimit(limit), mAmount(0) { mAmountFunction = func ? func : [](const T &element) -> size_t { static_cast(element); return 1; }; } template Queue::~Queue() { stop(); } template void Queue::stop() { std::lock_guard lock(mMutex); mStopping = true; mPushCondition.notify_all(); } template bool Queue::running() const { std::lock_guard lock(mMutex); return !mQueue.empty() || !mStopping; } template bool Queue::empty() const { std::lock_guard lock(mMutex); return mQueue.empty(); } template bool Queue::full() const { std::lock_guard lock(mMutex); return mQueue.size() >= mLimit; } template size_t Queue::size() const { std::lock_guard lock(mMutex); return mQueue.size(); } template size_t Queue::amount() const { std::lock_guard lock(mMutex); return mAmount; } template void Queue::push(T element) { std::unique_lock lock(mMutex); mPushCondition.wait(lock, [this]() { return !mLimit || mQueue.size() < mLimit || mStopping; }); if (mStopping) return; mAmount += mAmountFunction(element); mQueue.emplace(std::move(element)); } template optional Queue::pop() { std::unique_lock lock(mMutex); if (mQueue.empty()) return nullopt; mAmount -= mAmountFunction(mQueue.front()); optional element{std::move(mQueue.front())}; mQueue.pop(); return element; } template optional Queue::peek() { std::unique_lock lock(mMutex); return !mQueue.empty() ? std::make_optional(mQueue.front()) : nullopt; } template optional Queue::exchange(T element) { std::unique_lock lock(mMutex); if (mQueue.empty()) return nullopt; std::swap(mQueue.front(), element); return std::make_optional(std::move(element)); } } // namespace rtc::impl #endif