RTC Toolkit  1.0.0
repubBuf.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_
12 #define RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_
13 
14 #include <array>
15 #include <condition_variable>
16 #include <mutex>
17 
20 
21 namespace rtctk::telRepub {
22 
34 template <typename T = rtctk::AgnosticTopic>
35 class RepubBuf {
36 public:
42  RepubBuf(const std::string &id, uint16_t size)
43  : m_identifier(id), m_buf_size(size), m_free_slots(size), m_min_free_slots(size) {
44  m_buf.resize(m_buf_size);
45  }
46 
52  RepubBuf(const char *id, uint16_t 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);
55  }
56 
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);
63  }
64 
65  ~RepubBuf() noexcept {
66  DumpStatistic();
67  }
68 
69  RepubBuf(RepubBuf &&rhs) = delete;
70  RepubBuf &operator=(RepubBuf &&rhs) = delete;
71 
77  T *GetNextFree() {
78  // TBD added flag check if GetNextFree was *not* called before
79  std::unique_lock lock(m_mutex);
80  if (m_free_slots <= 0) {
81  return nullptr;
82  }
83 
84  return &(m_buf[m_next_free]);
85  } // GetNextFree
86 
91  void MakeAvailable() {
92  // TBD added flag check if GetNextFree was called before
93  std::unique_lock lock(m_mutex);
94  if (m_next_free >= (m_buf_size - 1)) {
95  m_next_free = 0;
96  } else {
97  m_next_free++;
98  }
99  m_free_slots--;
100  if (m_free_slots < m_min_free_slots) {
101  m_min_free_slots = m_free_slots;
102  }
103 
104  lock.unlock();
105  m_not_empty.notify_one();
106  } // MakeAvailable
107 
115  void ReleaseFree() {
116  // TBD added flag check if GetNextFree was called before and set it
117  // std::unique_lock lock(m_mutex);
118  } // ReleaseFree
119 
125  std::unique_lock lock(m_mutex);
126  if (m_free_slots >= m_buf_size) {
127  return nullptr;
128  }
129 
130  return &(m_buf[m_next_avail]);
131  } // GetNextAvail
132 
140  std::unique_lock lock(m_mutex);
141  // if (m_free_slots>=B_SIZE)
142  m_not_empty.wait(lock, [&]() {
143  return ((m_free_slots < m_buf_size) || m_exit_wait);
144  }); // TBD: to be replaced with wait_until and timeout
145 
146  return !m_exit_wait ? &(m_buf[m_next_avail]) : nullptr;
147  } // GetNextAvail
148 
153  void MakeFree() {
154  // TBD added flag check if GetNextAvail was called before
155  std::unique_lock lock(m_mutex);
156  if (m_next_avail >= (m_buf_size - 1)) {
157  m_next_avail = 0;
158  } else {
159  m_next_avail++;
160  }
161  m_free_slots++;
162  } // MakeFree
163 
167  void ReleaseWait() {
168  m_exit_wait = true;
169  m_not_empty.notify_one();
170 
171  } // ReleaseWait
172 
178  std::unique_lock lock(m_mutex);
179  return m_free_slots;
180  }
181 
188  std::unique_lock lock(m_mutex);
189  return (m_buf_size - m_free_slots);
190  }
191 
196  void DumpStatistic() {
197  LOG4CPLUS_DEBUG_FMT(rtctk::componentFramework::GetLogger(),
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);
202  } // dumpStatistic
203 
208  std::unique_lock lock(m_mutex);
209  return (m_buf_size - m_min_free_slots);
210  }
211 
216  return m_buf_size;
217  }
218 private:
219  std::string m_identifier;
220 
221  std::vector<T> m_buf;
222  uint16_t m_buf_size;
223 
224  uint16_t m_free_slots;
225  uint16_t m_min_free_slots;
226 
227  uint16_t m_next_free = 0;
228  uint16_t m_next_avail = 0;
229 
230  mutable std::mutex m_mutex;
231  std::condition_variable m_not_empty;
232  std::atomic<bool> m_exit_wait = false;
233 
234 }; // class RepubBuf
235 
236 } // namespace rtctk::telRepub
237 
238 #endif // RTCTK_REUSABLECOMPONENT_TELREPUB_TELREPUB_BUF_HPP_
rtctk::telRepub::RepubBuf::GetMaxOccupancy
uint16_t GetMaxOccupancy()
Returns maximal occupancy of the buffer.
Definition: repubBuf.hpp:207
rtctk::telRepub::RepubBuf::GetNextFree
T * GetNextFree()
Returns next free slot of the buffer.
Definition: repubBuf.hpp:77
rtctk::telRepub::RepubBuf::MakeFree
void MakeFree()
Returns back slot that has been retrived/given by call WaitNextAvail or GetNextAvail.
Definition: repubBuf.hpp:153
rtctk::telRepub
Definition: ddsPubThread.hpp:27
rtctk::telRepub::RepubBuf::RepubBuf
RepubBuf(const char *id, uint16_t size)
Create buffer with:
Definition: repubBuf.hpp:52
rtctk::telRepub::RepubBuf::ReleaseWait
void ReleaseWait()
Release WaitNextAvail.
Definition: repubBuf.hpp:167
rtctk::telRepub::RepubBuf::operator=
RepubBuf & operator=(RepubBuf &&rhs)=delete
mudpi::uint16_t
unsigned short uint16_t
Definition: mudpi.h:15
rtctk::componentFramework::GetLogger
log4cplus::Logger & GetLogger(const std::string &name="")
Get handle to a specific logger (used with logging macros)
rtctk::telRepub::RepubBuf::RepubBuf
RepubBuf(RepubBuf &&rhs)=delete
rtctk::telRepub::RepubBuf
Implementation of buffer (ring) that allows to have preallocated slots of type T.
Definition: repubBuf.hpp:35
rtctk::telRepub::RepubBuf::RepubBuf
RepubBuf(uint16_t size)
Create buffer without identifier, but with.
Definition: repubBuf.hpp:61
rtctk::telRepub::RepubBuf::~RepubBuf
~RepubBuf() noexcept
Definition: repubBuf.hpp:65
rtctk::telRepub::RepubBuf::GetNextAvail
T * GetNextAvail()
Returns next available (filled/set) slot.
Definition: repubBuf.hpp:124
rtctk::telRepub::RepubBuf::GetAvail
uint16_t GetAvail()
Number of availabel (occupied) (filled/set) slots in buffer that can be retrieved by calling WaitNext...
Definition: repubBuf.hpp:187
rtctk::telRepub::RepubBuf::WaitNextAvail
T * WaitNextAvail()
The call blocks (waits) for next available (filled/set) slot.
Definition: repubBuf.hpp:139
ddsCommon.hpp
Declares common DDS types.
rtctk::telRepub::RepubBuf::ReleaseFree
void ReleaseFree()
Release slot (not filled/set) that has been retrieved by GetNextFree.
Definition: repubBuf.hpp:115
rtctk::telRepub::RepubBuf::DumpStatistic
void DumpStatistic()
Dumps statistic(s): maximum occupancy of the buffer.
Definition: repubBuf.hpp:196
logger.hpp
Logging Support Library based on log4cplus.
rtctk::telRepub::RepubBuf::GetSize
uint16_t GetSize()
Returns (maximal) queue/buffer.
Definition: repubBuf.hpp:215
rtctk::telRepub::RepubBuf::GetFreeCapacity
uint16_t GetFreeCapacity()
Number of free slots in buffer that can be retrieved by calling GetNextFree.
Definition: repubBuf.hpp:177
rtctk::telRepub::RepubBuf::RepubBuf
RepubBuf(const std::string &id, uint16_t size)
Create buffer with:
Definition: repubBuf.hpp:42
rtctk::telRepub::RepubBuf::MakeAvailable
void MakeAvailable()
Returns back (filled/set) slot that has been retrieved by GetNextFree.
Definition: repubBuf.hpp:91