2022-08-14 19:00:22 +00:00
|
|
|
#pragma once
|
2023-06-09 08:43:04 +00:00
|
|
|
#include "debugtrace.hpp"
|
2023-06-06 14:05:24 +00:00
|
|
|
#include "lasp_indatahandler.h"
|
2023-06-10 13:47:52 +00:00
|
|
|
#include "lasp_thread.h"
|
2023-06-06 14:05:24 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2022-08-14 19:00:22 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
using std::placeholders::_1;
|
2022-08-14 19:00:22 +00:00
|
|
|
const us RINGBUFFER_SIZE = 1024;
|
|
|
|
|
2022-10-04 07:48:32 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup dsp
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \defgroup rt Real time signal handlers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
class SafeQueue;
|
2022-09-03 18:59:14 +00:00
|
|
|
/**
|
2023-06-09 08:43:04 +00:00
|
|
|
* @brief Threaded in data handler base. Buffers inCallback data and calls a
|
|
|
|
* callback with the same signature on a different thread. The main function of
|
|
|
|
* this is to offload the thread that handles the stream, such that expensive
|
|
|
|
* computations do not result in stream buffer xruns.
|
2022-09-03 18:59:14 +00:00
|
|
|
*/
|
2023-06-09 08:43:04 +00:00
|
|
|
class ThreadedInDataHandlerBase {
|
2022-08-14 19:00:22 +00:00
|
|
|
/**
|
|
|
|
* @brief The queue used to push elements to the handling thread.
|
|
|
|
*/
|
2023-06-09 08:43:04 +00:00
|
|
|
|
|
|
|
InDataHandler _indatahandler;
|
2022-10-10 17:17:38 +00:00
|
|
|
std::unique_ptr<SafeQueue> _queue;
|
2023-06-09 08:43:04 +00:00
|
|
|
|
2023-06-06 14:05:24 +00:00
|
|
|
mutable std::recursive_mutex _mtx;
|
2022-08-14 19:00:22 +00:00
|
|
|
|
|
|
|
std::atomic<bool> _thread_running{false};
|
2023-06-06 14:05:24 +00:00
|
|
|
std::atomic<bool> _thread_can_safely_run{false};
|
2022-08-14 19:00:22 +00:00
|
|
|
|
2023-06-12 07:11:08 +00:00
|
|
|
GlobalThreadPool _pool;
|
2023-06-10 13:47:52 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
/**
|
|
|
|
* @brief Function pointer that is called when new DaqData arrives.
|
|
|
|
*/
|
|
|
|
const InCallbackType inCallback;
|
|
|
|
|
2022-08-14 19:00:22 +00:00
|
|
|
void threadFcn();
|
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pushes a copy of the daqdata to the thread queue and returns.
|
|
|
|
* Adds a thread to handle the queue, whihc will call inCallback();
|
|
|
|
*
|
|
|
|
* @param daqdata the daq info to push
|
|
|
|
*
|
|
|
|
* @return true, to continue with sampling.
|
|
|
|
*/
|
|
|
|
void _inCallbackFromInDataHandler(const DaqData &daqdata);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadedInDataHandlerBase(SmgrHandle mgr, InCallbackType cb, InResetType reset);
|
|
|
|
~ThreadedInDataHandlerBase();
|
2023-06-06 14:05:24 +00:00
|
|
|
/**
|
|
|
|
* @brief This method should be called from the derived class' constructor,
|
|
|
|
* to start the thread and data is incoming.
|
|
|
|
*/
|
|
|
|
void startThread();
|
|
|
|
/**
|
|
|
|
* @brief This method SHOULD be called from all classes that derive on
|
|
|
|
* ThreadedInDataHandler. It is to make sure the inCallback_threaded()
|
|
|
|
* function is no longer called when the destructor of the derived class is
|
|
|
|
* called. Not calling this function is regarded as a BUG.
|
|
|
|
*/
|
|
|
|
void stopThread();
|
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
};
|
2022-08-14 19:00:22 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
/**
|
|
|
|
* @brief A bit of curiously recurring template pattern, to connect the
|
|
|
|
* specific handlers and connect the proper callbacks in a type-agnostic way.
|
|
|
|
* Using this class, each threaded handler should just implement its reset()
|
|
|
|
* and inCallback() method. Ellides the virtual method calls.
|
|
|
|
*
|
|
|
|
* Usage: class XHandler: public ThreadedInDataHandler<XHandler> {
|
|
|
|
* public:
|
|
|
|
* XHandler(streammgr) : ThreadedInDataHandler(streammgr) {}
|
|
|
|
* void inCallback(const DaqData& d) { ... do something with d }
|
|
|
|
* void reset(const Daq* daq) { ... do something with daq }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* For examples, see PPMHandler, etc.
|
|
|
|
*
|
|
|
|
* @tparam Derived The
|
|
|
|
*/
|
|
|
|
template <typename Derived>
|
|
|
|
class ThreadedInDataHandler : public ThreadedInDataHandlerBase {
|
|
|
|
public:
|
|
|
|
ThreadedInDataHandler(SmgrHandle mgr):
|
|
|
|
ThreadedInDataHandlerBase(mgr,
|
|
|
|
std::bind(&ThreadedInDataHandler::_inCallback, this, _1),
|
|
|
|
std::bind(&ThreadedInDataHandler::_reset, this, _1))
|
|
|
|
{
|
2022-08-14 19:00:22 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _reset(const Daq* daq) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
return static_cast<Derived*>(this)->reset(daq);
|
|
|
|
}
|
|
|
|
void _inCallback(const DaqData& data) {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
return static_cast<Derived*>(this)->inCallback(data);
|
|
|
|
}
|
2022-08-14 19:00:22 +00:00
|
|
|
};
|
2022-10-04 07:48:32 +00:00
|
|
|
|
|
|
|
/** @} */
|
|
|
|
/** @} */
|