2
0

queue.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_QUEUE_H
  19. #define RTC_QUEUE_H
  20. #include "include.hpp"
  21. #include <atomic>
  22. #include <chrono>
  23. #include <condition_variable>
  24. #include <mutex>
  25. #include <optional>
  26. #include <queue>
  27. namespace rtc {
  28. template <typename T> class Queue {
  29. public:
  30. Queue(std::size_t limit = 0);
  31. ~Queue();
  32. void stop();
  33. bool empty() const;
  34. size_t size() const;
  35. void push(const T &element);
  36. std::optional<T> pop();
  37. std::optional<T> tryPop();
  38. void wait();
  39. void wait(const std::chrono::milliseconds &duration);
  40. private:
  41. const size_t mLimit;
  42. std::queue<T> mQueue;
  43. std::condition_variable mPopCondition, mPushCondition;
  44. bool mStopping = false;
  45. mutable std::mutex mMutex;
  46. };
  47. template <typename T> Queue<T>::Queue(size_t limit) : mLimit(limit) {}
  48. template <typename T> Queue<T>::~Queue() { stop(); }
  49. template <typename T> void Queue<T>::stop() {
  50. std::lock_guard<std::mutex> lock(mMutex);
  51. mStopping = true;
  52. mPopCondition.notify_all();
  53. mPushCondition.notify_all();
  54. }
  55. template <typename T> bool Queue<T>::empty() const {
  56. std::lock_guard<std::mutex> lock(mMutex);
  57. return mQueue.empty();
  58. }
  59. template <typename T> size_t Queue<T>::size() const {
  60. std::lock_guard<std::mutex> lock(mMutex);
  61. return mQueue.size();
  62. }
  63. template <typename T> void Queue<T>::push(const T &element) {
  64. std::unique_lock<std::mutex> lock(mMutex);
  65. mPushCondition.wait(lock, [this]() { return !mLimit || mQueue.size() < mLimit || mStopping; });
  66. if (!mStopping) {
  67. mQueue.push(element);
  68. mPopCondition.notify_one();
  69. }
  70. }
  71. template <typename T> std::optional<T> Queue<T>::pop() {
  72. std::unique_lock<std::mutex> lock(mMutex);
  73. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  74. if (!mQueue.empty()) {
  75. std::optional<T> element(std::move(mQueue.front()));
  76. mQueue.pop();
  77. return element;
  78. } else {
  79. return nullopt;
  80. }
  81. }
  82. template <typename T> std::optional<T> Queue<T>::tryPop() {
  83. std::unique_lock<std::mutex> lock(mMutex);
  84. if (!mQueue.empty()) {
  85. std::optional<T> element(std::move(mQueue.front()));
  86. mQueue.pop();
  87. return element;
  88. } else {
  89. return nullopt;
  90. }
  91. }
  92. template <typename T> void Queue<T>::wait() {
  93. std::unique_lock<std::mutex> lock(mMutex);
  94. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  95. }
  96. template <typename T> void Queue<T>::wait(const std::chrono::milliseconds &duration) {
  97. std::unique_lock<std::mutex> lock(mMutex);
  98. mPopCondition.wait_for(lock, duration, [this]() { return !mQueue.empty() || mStopping; });
  99. }
  100. } // namespace rtc
  101. #endif