queue.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_IMPL_QUEUE_H
  19. #define RTC_IMPL_QUEUE_H
  20. #include "common.hpp"
  21. #include <atomic>
  22. #include <chrono>
  23. #include <condition_variable>
  24. #include <mutex>
  25. #include <queue>
  26. namespace rtc::impl {
  27. template <typename T> class Queue {
  28. public:
  29. using amount_function = std::function<size_t(const T &element)>;
  30. Queue(size_t limit = 0, amount_function func = nullptr);
  31. ~Queue();
  32. void stop();
  33. bool running() const;
  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. optional<T> pop();
  40. optional<T> tryPop();
  41. optional<T> peek();
  42. optional<T> exchange(T element);
  43. bool wait(const optional<std::chrono::milliseconds> &duration = nullopt);
  44. private:
  45. void pushImpl(T element);
  46. optional<T> popImpl();
  47. const size_t mLimit;
  48. size_t mAmount;
  49. std::queue<T> mQueue;
  50. std::condition_variable mPopCondition, mPushCondition;
  51. amount_function mAmountFunction;
  52. bool mStopping = false;
  53. mutable std::mutex mMutex;
  54. };
  55. template <typename T>
  56. Queue<T>::Queue(size_t limit, amount_function func) : mLimit(limit), mAmount(0) {
  57. mAmountFunction = func ? func : [](const T &element) -> size_t {
  58. static_cast<void>(element);
  59. return 1;
  60. };
  61. }
  62. template <typename T> Queue<T>::~Queue() { stop(); }
  63. template <typename T> void Queue<T>::stop() {
  64. std::lock_guard lock(mMutex);
  65. mStopping = true;
  66. mPopCondition.notify_all();
  67. mPushCondition.notify_all();
  68. }
  69. template <typename T> bool Queue<T>::running() const {
  70. std::lock_guard lock(mMutex);
  71. return !mQueue.empty() || !mStopping;
  72. }
  73. template <typename T> bool Queue<T>::empty() const {
  74. std::lock_guard lock(mMutex);
  75. return mQueue.empty();
  76. }
  77. template <typename T> bool Queue<T>::full() const {
  78. std::lock_guard lock(mMutex);
  79. return mQueue.size() >= mLimit;
  80. }
  81. template <typename T> size_t Queue<T>::size() const {
  82. std::lock_guard lock(mMutex);
  83. return mQueue.size();
  84. }
  85. template <typename T> size_t Queue<T>::amount() const {
  86. std::lock_guard lock(mMutex);
  87. return mAmount;
  88. }
  89. template <typename T> void Queue<T>::push(T element) {
  90. std::unique_lock lock(mMutex);
  91. mPushCondition.wait(lock, [this]() { return !mLimit || mQueue.size() < mLimit || mStopping; });
  92. pushImpl(std::move(element));
  93. }
  94. template <typename T> optional<T> Queue<T>::pop() {
  95. std::unique_lock lock(mMutex);
  96. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  97. return popImpl();
  98. }
  99. template <typename T> optional<T> Queue<T>::tryPop() {
  100. std::unique_lock lock(mMutex);
  101. return popImpl();
  102. }
  103. template <typename T> optional<T> Queue<T>::peek() {
  104. std::unique_lock lock(mMutex);
  105. return !mQueue.empty() ? std::make_optional(mQueue.front()) : nullopt;
  106. }
  107. template <typename T> optional<T> Queue<T>::exchange(T element) {
  108. std::unique_lock lock(mMutex);
  109. if (mQueue.empty())
  110. return nullopt;
  111. std::swap(mQueue.front(), element);
  112. return std::make_optional(std::move(element));
  113. }
  114. template <typename T> bool Queue<T>::wait(const optional<std::chrono::milliseconds> &duration) {
  115. std::unique_lock lock(mMutex);
  116. if (duration) {
  117. return mPopCondition.wait_for(lock, *duration,
  118. [this]() { return !mQueue.empty() || mStopping; });
  119. } else {
  120. mPopCondition.wait(lock, [this]() { return !mQueue.empty() || mStopping; });
  121. return true;
  122. }
  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> optional<T> Queue<T>::popImpl() {
  132. if (mQueue.empty())
  133. return nullopt;
  134. mAmount -= mAmountFunction(mQueue.front());
  135. optional<T> element{std::move(mQueue.front())};
  136. mQueue.pop();
  137. return element;
  138. }
  139. } // namespace rtc::impl
  140. #endif