|
| 1 | +// Copyright 2024 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 | +/** |
| 16 | + * @file DBQueue.hpp |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef DBQUEUE_HPP |
| 20 | +#define DBQUEUE_HPP |
| 21 | + |
| 22 | +#include <condition_variable> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | +#include <queue> |
| 26 | + |
| 27 | +namespace eprosima { |
| 28 | +namespace utils { |
| 29 | +namespace event { |
| 30 | + |
| 31 | +/** |
| 32 | + * Double buffered, threadsafe queue for MPSC (multi-producer, single-consumer) comms. |
| 33 | + */ |
| 34 | +template<class T> |
| 35 | +class DBQueue |
| 36 | +{ |
| 37 | + |
| 38 | +public: |
| 39 | + |
| 40 | + DBQueue() |
| 41 | + : m_foreground_queue(&m_queue_alpha) |
| 42 | + , m_background_queue(&m_queue_beta) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + //! Clears foreground queue and swaps queues. |
| 47 | + void swap() |
| 48 | + { |
| 49 | + std::unique_lock<std::mutex> fg_guard(m_foreground_mutex); |
| 50 | + std::unique_lock<std::mutex> bg_guard(m_background_mutex); |
| 51 | + |
| 52 | + // Clear the foreground queue. |
| 53 | + std::queue<T>().swap(*m_foreground_queue); |
| 54 | + |
| 55 | + auto* swap = m_background_queue; |
| 56 | + m_background_queue = m_foreground_queue; |
| 57 | + m_foreground_queue = swap; |
| 58 | + } |
| 59 | + |
| 60 | + //! Pushes to the background queue. Copy constructor. |
| 61 | + void push( |
| 62 | + const T& item) |
| 63 | + { |
| 64 | + std::unique_lock<std::mutex> guard(m_background_mutex); |
| 65 | + m_background_queue->push(item); |
| 66 | + } |
| 67 | + |
| 68 | + //! Pushes to the background queue. Move constructor. |
| 69 | + void push( |
| 70 | + T&& item) |
| 71 | + { |
| 72 | + std::unique_lock<std::mutex> guard(m_background_mutex); |
| 73 | + m_background_queue->push(std::move(item)); |
| 74 | + } |
| 75 | + |
| 76 | + //! Returns a reference to the front element |
| 77 | + //! in the foregrund queue. |
| 78 | + T& front() |
| 79 | + { |
| 80 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 81 | + return m_foreground_queue->front(); |
| 82 | + } |
| 83 | + |
| 84 | + const T& front() const |
| 85 | + { |
| 86 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 87 | + return m_foreground_queue->front(); |
| 88 | + } |
| 89 | + |
| 90 | + //! Pops from the foreground queue. |
| 91 | + void pop() |
| 92 | + { |
| 93 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 94 | + m_foreground_queue->pop(); |
| 95 | + } |
| 96 | + |
| 97 | + //! Return the front element in the foreground queue by moving it and erase it from the queue. |
| 98 | + T front_and_pop() |
| 99 | + { |
| 100 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 101 | + |
| 102 | + // Get value by moving the internal queue reference to a new value |
| 103 | + T value = std::move(m_foreground_queue->front()); |
| 104 | + // At this point m_foreground_queue contains a non valid element, but mutex is taken and next instruction erase it |
| 105 | + |
| 106 | + // Pop value from queue |
| 107 | + m_foreground_queue->pop(); |
| 108 | + |
| 109 | + // Return value (as it has been created in this scope, it will not be copied but moved or directly forwarded) |
| 110 | + return value; |
| 111 | + } |
| 112 | + |
| 113 | + //! Reports whether the foreground queue is empty. |
| 114 | + bool empty() const |
| 115 | + { |
| 116 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 117 | + return m_foreground_queue->empty(); |
| 118 | + } |
| 119 | + |
| 120 | + //! Reports whether the both queues are empty. |
| 121 | + bool both_empty() const |
| 122 | + { |
| 123 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 124 | + std::unique_lock<std::mutex> bg_guard(m_background_mutex); |
| 125 | + return m_foreground_queue->empty() && m_background_queue->empty(); |
| 126 | + } |
| 127 | + |
| 128 | + //! Reports the size of the foreground queue. |
| 129 | + size_t size() const |
| 130 | + { |
| 131 | + std::unique_lock<std::mutex> guard(m_foreground_mutex); |
| 132 | + return m_foreground_queue->size(); |
| 133 | + } |
| 134 | + |
| 135 | + //! Clears foreground and background. |
| 136 | + void clear() |
| 137 | + { |
| 138 | + std::unique_lock<std::mutex> fg_guard(m_foreground_mutex); |
| 139 | + std::unique_lock<std::mutex> bg_guard(m_background_mutex); |
| 140 | + std::queue<T>().swap(*m_foreground_queue); |
| 141 | + std::queue<T>().swap(*m_background_queue); |
| 142 | + } |
| 143 | + |
| 144 | +private: |
| 145 | + |
| 146 | + // Underlying queues |
| 147 | + std::queue<T> m_queue_alpha; |
| 148 | + std::queue<T> m_queue_beta; |
| 149 | + |
| 150 | + // Front and background queue references (double buffering) |
| 151 | + std::queue<T>* m_foreground_queue; |
| 152 | + std::queue<T>* m_background_queue; |
| 153 | + |
| 154 | + mutable std::mutex m_foreground_mutex; |
| 155 | + mutable std::mutex m_background_mutex; |
| 156 | +}; |
| 157 | + |
| 158 | +} /* namespace event */ |
| 159 | +} /* namespace utils */ |
| 160 | +} /* namespace eprosima */ |
| 161 | + |
| 162 | +#endif // ifndef DBQUEUE_H |
0 commit comments