13#ifndef RTCTK_DATATASK_READERHELPERS_HPP
14#define RTCTK_DATATASK_READERHELPERS_HPP
20#include <system_error>
35[[deprecated]]
inline std::chrono::milliseconds
36CalcTimeout(
size_t count,
float loop_frequency,
float error_margin) {
37 using namespace std::chrono;
38 return milliseconds{
static_cast<int>(ceil(1000.0 * count / loop_frequency) * error_margin)};
54template <
typename ReaderType,
typename Operation>
55[[deprecated]] std::error_code
56Read(ReaderType& reader, Operation&& op,
size_t count,
float loop_frequency,
float error_margin) {
57 using namespace std::chrono;
61 auto timeout_short =
CalcTimeout(1, loop_frequency, error_margin);
63 auto timeout_total =
CalcTimeout(count, loop_frequency, error_margin);
67 std::error_code
const ok;
68 std::pair<std::error_code, size_t> ret;
69 milliseconds time_elapsed{0};
71 auto time_start = steady_clock::now();
74 ret = reader.Read(std::forward<Operation>(op), count - read, timeout_short);
75 if (ret.first != ok) {
76 LOG4CPLUS_ERROR(logger,
"!Reading from shm timed out: check if queue is being filled");
77 LOG4CPLUS_ERROR(logger,
78 "Read: " << ret.second <<
" in " << timeout_short.count() <<
" ms");
79 LOG4CPLUS_ERROR(logger,
"Expected: " << count);
87 time_elapsed = duration_cast<milliseconds>(steady_clock::now() - time_start);
88 if (time_elapsed > timeout_total) {
89 LOG4CPLUS_ERROR(logger,
"Reading from shm timed out: check if queue is being filled");
90 LOG4CPLUS_ERROR(logger,
"Read: " << read <<
" in " << time_elapsed.count() <<
" ms");
91 LOG4CPLUS_ERROR(logger,
"Expected: " << read);
92 return std::make_error_code(std::errc::timed_out);
108template <
typename ReaderType>
109[[deprecated]] std::error_code
110Skip(ReaderType& reader,
size_t count,
float loop_frequency,
float error_margin) {
111 using namespace std::chrono;
115 auto timeout_short =
CalcTimeout(1, loop_frequency, error_margin);
117 auto timeout_total =
CalcTimeout(count, loop_frequency, error_margin);
121 std::error_code
const ok;
122 std::pair<std::error_code, size_t> ret;
123 milliseconds time_elapsed{0};
125 auto time_start = steady_clock::now();
128 ret = reader.Skip(count - skipped, timeout_short);
129 if (ret.first != ok) {
130 LOG4CPLUS_ERROR(logger,
"!Skipping from shm timed out: check if queue is being filled");
131 LOG4CPLUS_ERROR(logger,
132 "Read: " << ret.second <<
" in " << timeout_short.count() <<
" ms");
133 LOG4CPLUS_ERROR(logger,
"Expected: " << count);
136 skipped += ret.second;
137 if (skipped == count) {
141 time_elapsed = duration_cast<milliseconds>(steady_clock::now() - time_start);
142 if (time_elapsed > timeout_total) {
143 LOG4CPLUS_ERROR(logger,
"Skipping from shm timed out: check if queue is being filled");
144 LOG4CPLUS_ERROR(logger,
"Read: " << skipped <<
" in " << time_elapsed.count() <<
" ms");
145 LOG4CPLUS_ERROR(logger,
"Expected: " << skipped);
146 return std::make_error_code(std::errc::timed_out);
159template <
typename ReaderType>
160[[deprecated]] std::error_code
Reset(ReaderType& reader) {
161 if (reader.Size() != 0) {
162 return reader.Reset();
175template <
typename ReaderType>
176[[deprecated]]
size_t NumFree(ReaderType& reader) {
177 return reader.Size() - reader.NumAvailable();
Logging Support Library based on log4cplus.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition: logger.cpp:180
std::chrono::milliseconds CalcTimeout(size_t count, float loop_frequency, float error_margin)
Helper function to calculate the estimated time to read the a number of samples at a given frequency.
Definition: readerHelpers.hpp:36
std::error_code Read(ReaderType &reader, Operation &&op, size_t count, float loop_frequency, float error_margin)
Helper function to wrap the ipcq.read with handling of timeouts and count values.
Definition: readerHelpers.hpp:56
std::error_code Skip(ReaderType &reader, size_t count, float loop_frequency, float error_margin)
Helper function to wrap the ipcq.skip with handling of timeouts and count values.
Definition: readerHelpers.hpp:110
std::error_code Reset(ReaderType &reader)
Helper function to reset the ipcq.reader to latest sample.
Definition: readerHelpers.hpp:160
size_t NumFree(ReaderType &reader)
Helper function to get the free space in the shm.
Definition: readerHelpers.hpp:176
Definition: computationBase.hpp:33