queue.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using amount_function = std::function<size_t(const T &element)>;
  31. Queue(size_t limit = 0, amount_function func = nullptr);
  32. ~Queue();
  33. void stop();
  34. bool empty() const;
  35. bool full() const;
  36. size_t size() const; // elements
  37. size_t amount() const; // amount
  38. void push(T element);
  39. std::optional<T> pop();
  40. std::optional<T> peek();
  41. std::optional<T> exchange(T element);
  42. bool wait(const std::optional<std::chrono::milliseconds> &duration = nullopt);
  43. private:
  44. const size_t mLimit;
  45. size_t mAmount;
  46. std::queue<T> mQueue;
  47. std::condition_variable mPopCondition, mPushCondition;
  48. amount_function mAmountFunction;
  49. bool mStopping = false;
  50. mutable std::mutex mMutex;
  51. };
  52. template <typename T>
  53. Queue<T>::Queue(size_t limit, amount_function func) : mLimit(limit), mAmount(0) {
  54. mAmountFunction = func ? func : [](const T &element) -> size_t {
  55. static_cast<void>(element);
  56. return 1;
  57. };
  58. }
  59. template <typename T> Queue<T>::~Queue() { stop(); }
  60. template <typename T> void Queue<T>::stop() {
  61. std::lock_guard lock(mMutex);
  62. mStopping = true;
  63. mPopCondition.notify_all();
  64. mPushCondition.notify_all();
  65. }
  66. template <typename T> bool Queue<T>::empty() const {
  67. std::lock_guard lock(mMutex);
  68. return mQueue.empty();
  69. }
  70. template <typename T> bool Queue<T>::full() const {
  71. std::lock_guard lock(mMutex);
  72. return mQueue.size() >= mLimit;
  73. }
  74. template <typename T> size_t Queue<T>::size() const {
  75. std::lock_guard lock(mMutex);
  76. return mQueue.size();
  77. }
  78. template <typename T> size_t Queue<T>::amount() const {
  79. std::lock_guard lock(mMutex);
  80. return mAmount;
  81. }
  82. template <typename T> void Queue<T>::push(T element) {
  83. std::unique_lock lock(mMutex);
  84. mPushCondition.wait(lock, [this]() { return !mLimit || mQueue.size() < mLimit || mStopping; });
  85. if (!mStopping) {
  86. mAmount += mAmountFunction(element);
  87. mQueue.emplace(std::move(element));
  88. mPopCondition.notify_one();
  89. }
  90. }
  91. template <typename T> std::optional<T> Queue<T>::pop() {
  92. std::unique_lock lock(mMutex);
  93. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  94. if (!mQueue.empty()) {
  95. mAmount -= mAmountFunction(mQueue.front());
  96. std::optional<T> element{std::move(mQueue.front())};
  97. mQueue.pop();
  98. return element;
  99. } else {
  100. return nullopt;
  101. }
  102. }
  103. template <typename T> std::optional<T> Queue<T>::peek() {
  104. std::unique_lock lock(mMutex);
  105. if (!mQueue.empty()) {
  106. return std::optional<T>{mQueue.front()};
  107. } else {
  108. return nullopt;
  109. }
  110. }
  111. template <typename T> std::optional<T> Queue<T>::exchange(T element) {
  112. std::unique_lock lock(mMutex);
  113. if (!mQueue.empty()) {
  114. std::swap(mQueue.front(), element);
  115. return std::optional<T>{element};
  116. } else {
  117. return nullopt;
  118. }
  119. }
  120. template <typename T>
  121. bool Queue<T>::wait(const std::optional<std::chrono::milliseconds> &duration) {
  122. std::unique_lock lock(mMutex);
  123. if (duration)
  124. mPopCondition.wait_for(lock, *duration, [this]() { return !mQueue.empty() || mStopping; });
  125. else
  126. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  127. return !mStopping;
  128. }
  129. } // namespace rtc
  130. #endif