/** * 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: Queue(); ~Queue(); void stop(); bool empty() const; void push(const T &element); std::optional pop(); void wait(); void wait(const std::chrono::milliseconds &duration); private: std::queue mQueue; std::condition_variable mCondition; std::atomic mStopping; mutable std::mutex mMutex; }; template Queue::Queue() : mStopping(false) {} template Queue::~Queue() { stop(); } template void Queue::stop() { std::lock_guard lock(mMutex); mStopping = true; mCondition.notify_all(); } template bool Queue::empty() const { std::lock_guard lock(mMutex); return mQueue.empty(); } template void Queue::push(const T &element) { std::lock_guard lock(mMutex); if (mStopping) return; mQueue.push(element); mCondition.notify_one(); } template std::optional Queue::pop() { std::unique_lock lock(mMutex); while (mQueue.empty()) { if (mStopping) return nullopt; mCondition.wait(lock); } std::optional element = mQueue.front(); mQueue.pop(); return element; } template void Queue::wait() { std::unique_lock lock(mMutex); if (mQueue.empty() && !mStopping) mCondition.wait(lock); } template void Queue::wait(const std::chrono::milliseconds &duration) { std::unique_lock lock(mMutex); if (mQueue.empty() && !mStopping) mCondition.wait_for(lock, duration); } } // namespace rtc #endif