HLCC Documentation 3.0.0
Loading...
Searching...
No Matches
circularBufferConcurrent.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2020-2025 European Southern Observatory (ESO)
2//
3// SPDX-License-Identifier: LGPL-3.0-only
4
5/*
6 * circularBufferConcurrent.hpp
7 *
8 * Created on: Jul 7, 2021
9 * Author: hsommer
10 */
11#ifndef HLCC_OLDBMUX_CIRCULARBUFFERCONCURRENT_HPP
12#define HLCC_OLDBMUX_CIRCULARBUFFERCONCURRENT_HPP
13
14#include <mutex>
15#include <condition_variable>
16#include <functional>
17#include <optional>
18
19#include <log4cplus/logger.h>
20#include <rad/logger.hpp>
21
22#include <boost/circular_buffer.hpp>
23
24
25// HSO TODO: Check async calls in ProcessRequest in ~/git-rtc/rtctk/componentFramework/services/oldb/src/oldbAdapter.cpp
26
27
28namespace hlcc::oldbmux {
29
52template<typename T>
54{
55public:
56
57 explicit CircularBufferConcurrent(const std::string& name, std::size_t capacity,
58 const log4cplus::Logger& logger);
59
61
64
73 void Push(T&& data);
74
85 std::optional<T> Poll(std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
86
87 void Clear();
88
89 int Size() const;
90
96 const std::unique_ptr<std::scoped_lock<std::recursive_mutex>> Lock() const;
97
108 void SetDiscardListener(std::function<void(T&)> discard_listener);
109
114 boost::circular_buffer<T>& GetCb();
115
116 /*
117 * Closes the buffer. This notifies (releases) any thread in the Poll method
118 * (if the thread called Poll with a timeout)
119 * and turns future invocations of methods Push and Poll into no-ops.
120 * Existing data in the buffer gets discarded, with notification of client.
121 *
122 * This method can be used to prepare a CircularBufferConcurrent for destruction,
123 * for which no other thread must be in its code.
124 *
125 * The current implementation returns already while another thread may still be inside Poll.
126 * This is OK for the use of the buffer inside CiiOlbdDataPointAsync, which has its own sync mechanism
127 * to wait for that thread.
128 */
129 void Close();
130
131private:
135 const log4cplus::Logger m_logger;
136
137 /*
138 * See Close().
139 */
140 std::atomic<bool> m_closed;
141
148 boost::circular_buffer<T> cb;
149
163 mutable std::recursive_mutex mutex;
164
171 std::condition_variable_any buffer_not_empty;
172
177 std::function<void(T&)> discard_listener;
178
182 const std::string name;
183
184};
185
186} // end namespace hlcc::oldbmux
187
188// include template class definition from separate header file.
190
191#endif /* HLCC_OLDBMUX_CIRCULARBUFFERCONCURRENT_HPP */
void Close()
Definition circularBufferConcurrent.ipp:47
int Size() const
Definition circularBufferConcurrent.ipp:154
const std::unique_ptr< std::scoped_lock< std::recursive_mutex > > Lock() const
Definition circularBufferConcurrent.ipp:161
CircularBufferConcurrent(const CircularBufferConcurrent &)=delete
std::optional< T > Poll(std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())
Gets the oldest element from the circular buffer.
Definition circularBufferConcurrent.ipp:115
CircularBufferConcurrent & operator=(const CircularBufferConcurrent &)=delete
CircularBufferConcurrent(const std::string &name, std::size_t capacity, const log4cplus::Logger &logger)
Definition circularBufferConcurrent.ipp:24
boost::circular_buffer< T > & GetCb()
Definition circularBufferConcurrent.ipp:173
~CircularBufferConcurrent()
Definition circularBufferConcurrent.ipp:41
void Clear()
Definition circularBufferConcurrent.ipp:147
void SetDiscardListener(std::function< void(T &)> discard_listener)
Definition circularBufferConcurrent.ipp:167
void Push(T &&data)
Adds new data to the circular buffer.
Definition circularBufferConcurrent.ipp:83
Definition ciiOldbDataPointAsync.hpp:35
ccsinsdetifllnetio::PointingKernelPositions data
Definition pkp_llnetio_subscriber.cpp:29