queue.hpp 4.5 KB

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