DBQueue.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. #ifndef DBQUEUE_H
  16. #define DBQUEUE_H
  17. #include <queue>
  18. #include <mutex>
  19. #include <memory>
  20. #include <condition_variable>
  21. namespace eprosima {
  22. namespace fastrtps{
  23. /**
  24. * Double buffered, threadsafe queue for MPSC (multi-producer, single-consumer) comms.
  25. */
  26. template<class T>
  27. class DBQueue {
  28. public:
  29. DBQueue():
  30. mForegroundQueue(&mQueueAlpha),
  31. mBackgroundQueue(&mQueueBeta)
  32. {}
  33. //! Clears foreground queue and swaps queues.
  34. void Swap()
  35. {
  36. std::unique_lock<std::mutex> fgGuard(mForegroundMutex);
  37. std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
  38. // Clear the foreground queue.
  39. std::queue<T>().swap(*mForegroundQueue);
  40. auto* swap = mBackgroundQueue;
  41. mBackgroundQueue = mForegroundQueue;
  42. mForegroundQueue = swap;
  43. }
  44. //! Pushes to the background queue.
  45. void Push(const T& item)
  46. {
  47. std::unique_lock<std::mutex> guard(mBackgroundMutex);
  48. mBackgroundQueue->push(item);
  49. }
  50. //! Returns a reference to the front element
  51. //! in the foregrund queue.
  52. T& Front()
  53. {
  54. std::unique_lock<std::mutex> guard(mForegroundMutex);
  55. return mForegroundQueue->front();
  56. }
  57. const T& Front() const
  58. {
  59. std::unique_lock<std::mutex> guard(mForegroundMutex);
  60. return mForegroundQueue->front();
  61. }
  62. //! Pops from the foreground queue.
  63. void Pop()
  64. {
  65. std::unique_lock<std::mutex> guard(mForegroundMutex);
  66. mForegroundQueue->pop();
  67. }
  68. //! Reports whether the foreground queue is empty.
  69. bool Empty() const
  70. {
  71. std::unique_lock<std::mutex> guard(mForegroundMutex);
  72. return mForegroundQueue->empty();
  73. }
  74. //! Reports whether the both queues are empty.
  75. bool BothEmpty() const
  76. {
  77. std::unique_lock<std::mutex> guard(mForegroundMutex);
  78. std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
  79. return mForegroundQueue->empty() && mBackgroundQueue->empty();
  80. }
  81. //! Reports the size of the foreground queue.
  82. size_t Size() const
  83. {
  84. std::unique_lock<std::mutex> guard(mForegroundMutex);
  85. return mForegroundQueue->size();
  86. }
  87. //! Clears foreground and background.
  88. void Clear()
  89. {
  90. std::unique_lock<std::mutex> fgGuard(mForegroundMutex);
  91. std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
  92. std::queue<T>().swap(*mForegroundQueue);
  93. std::queue<T>().swap(*mBackgroundQueue);
  94. }
  95. private:
  96. // Underlying queues
  97. std::queue<T> mQueueAlpha;
  98. std::queue<T> mQueueBeta;
  99. // Front and background queue references (double buffering)
  100. std::queue<T>* mForegroundQueue;
  101. std::queue<T>* mBackgroundQueue;
  102. mutable std::mutex mForegroundMutex;
  103. mutable std::mutex mBackgroundMutex;
  104. };
  105. } // namespace fastrtps
  106. } // namespace eprosima
  107. #endif