11 #ifndef RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_
12 #define RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_
15 #include <condition_variable>
34 template <
typename T = rtctk::AgnosticTopic>
43 : m_identifier(id), m_buf_size(size), m_free_slots(size), m_min_free_slots(size) {
44 m_buf.resize(m_buf_size);
53 : m_identifier(id), m_buf_size(size), m_free_slots(size), m_min_free_slots(size) {
54 m_buf.resize(m_buf_size);
61 RepubBuf(
uint16_t size) : m_buf_size(size), m_free_slots(size), m_min_free_slots(size) {
62 m_buf.resize(m_buf_size);
79 std::unique_lock lock(m_mutex);
80 if (m_free_slots <= 0) {
84 return &(m_buf[m_next_free]);
93 std::unique_lock lock(m_mutex);
94 if (m_next_free >= (m_buf_size - 1)) {
100 if (m_free_slots < m_min_free_slots) {
101 m_min_free_slots = m_free_slots;
105 m_not_empty.notify_one();
125 std::unique_lock lock(m_mutex);
126 if (m_free_slots >= m_buf_size) {
130 return &(m_buf[m_next_avail]);
140 std::unique_lock lock(m_mutex);
142 m_not_empty.wait(lock, [&]() {
143 return ((m_free_slots < m_buf_size) || m_exit_wait);
146 return !m_exit_wait ? &(m_buf[m_next_avail]) :
nullptr;
155 std::unique_lock lock(m_mutex);
156 if (m_next_avail >= (m_buf_size - 1)) {
169 m_not_empty.notify_one();
178 std::unique_lock lock(m_mutex);
188 std::unique_lock lock(m_mutex);
189 return (m_buf_size - m_free_slots);
198 "[%s] Max TelRepub buffer occupancy %d (%f %%).\n",
199 m_identifier.c_str(),
200 (m_buf_size - m_min_free_slots),
201 float(m_buf_size - m_min_free_slots) / m_buf_size);
208 std::unique_lock lock(m_mutex);
209 return (m_buf_size - m_min_free_slots);
219 std::string m_identifier;
221 std::vector<T> m_buf;
230 mutable std::mutex m_mutex;
231 std::condition_variable m_not_empty;
232 std::atomic<bool> m_exit_wait =
false;
238 #endif // RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_