/** * Copyright (c) 2019 Paul-Louis Ageneau * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef RTC_QUEUE_H #define RTC_QUEUE_H #include "include.hpp" #include #include #include #include #include #include namespace rtc { 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); std::optional pop(); std::optional tryPop(); std::optional peek(); std::optional exchange(T element); bool wait(const std::optional &duration = nullopt); private: void pushImpl(T element); std::optional popImpl(); const size_t mLimit; size_t mAmount; std::queue mQueue; std::condition_variable mPopCondition, 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; mPopCondition.notify_all(); 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; }); pushImpl(std::move(element)); } template std::optional Queue::pop() { std::unique_lock lock(mMutex); mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; }); return popImpl(); } template std::optional Queue::tryPop() { std::unique_lock lock(mMutex); return popImpl(); } template std::optional Queue::peek() { std::unique_lock lock(mMutex); return !mQueue.empty() ? std::make_optional(mQueue.front()) : nullopt; } template std::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)); } template bool Queue::wait(const std::optional &duration) { std::unique_lock lock(mMutex); if (duration) mPopCondition.wait_for(lock, *duration, [this]() { return !mQueue.empty() || mStopping; }); else mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; }); return !mQueue.empty(); } template void Queue::pushImpl(T element) { if (mStopping) return; mAmount += mAmountFunction(element); mQueue.emplace(std::move(element)); mPopCondition.notify_one(); } template std::optional Queue::popImpl() { if (mQueue.empty()) return nullopt; mAmount -= mAmountFunction(mQueue.front()); std::optional element{std::move(mQueue.front())}; mQueue.pop(); return element; } } // namespace rtc #endif