220 lines
4.1 KiB
C++
220 lines
4.1 KiB
C++
/*
|
|
* threads_actor.hpp
|
|
*
|
|
* Created on: Jun 11, 2021
|
|
* Author: erki
|
|
*/
|
|
|
|
#ifndef SKULLC_THREADS_ACTOR_HPP_
|
|
#define SKULLC_THREADS_ACTOR_HPP_
|
|
|
|
#include <cmsis_os.h>
|
|
#include <freertos_os2.h>
|
|
#include <queue.h>
|
|
|
|
#include <type_traits>
|
|
#include <variant>
|
|
|
|
#include "threads_primitivethread.hpp"
|
|
#include "threads_signal.hpp"
|
|
#include "threads_timer.hpp"
|
|
|
|
namespace Threads
|
|
{
|
|
|
|
template<typename CRTP, typename... Ts>
|
|
class Actor
|
|
{
|
|
public:
|
|
using value_type = std::variant<Ts...>;
|
|
|
|
static_assert(std::is_default_constructible_v<value_type>, "Ts must be default constructible.");
|
|
static_assert(std::is_trivially_copyable_v<value_type>, "Ts must be trivially copyable.");
|
|
|
|
Actor(const std::uint32_t queue_size, const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
|
: thread_([](void* d) {
|
|
auto* dd = static_cast<Actor<CRTP, Ts...>*>(d);
|
|
(*dd)();
|
|
},
|
|
this, name, priority, stack_size),
|
|
msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(this)
|
|
{
|
|
assert(msg_queue_);
|
|
}
|
|
|
|
virtual ~Actor() {}
|
|
|
|
template<typename T>
|
|
struct Signal : Signallable<T>
|
|
{
|
|
using parent = Actor<CRTP, Ts...>;
|
|
|
|
static_assert(std::is_convertible_v<T, parent::value_type>, "value_type cannot be constructed from T.");
|
|
|
|
explicit Signal(parent* q)
|
|
: q_(q)
|
|
{}
|
|
|
|
void emit(const T& data) override
|
|
{
|
|
parent::value_type to_send = data;
|
|
xQueueSend(q_->msg_queue_, &to_send, 0);
|
|
}
|
|
|
|
BaseType_t emitFromIsr(const T& data) override
|
|
{
|
|
parent::value_type to_send = data;
|
|
BaseType_t was_awoken = pdFALSE;
|
|
|
|
xQueueSendFromISR(q_->msg_queue_, &to_send, &was_awoken);
|
|
|
|
return was_awoken;
|
|
}
|
|
|
|
private:
|
|
parent* q_;
|
|
};
|
|
|
|
template<typename U>
|
|
Signal<U> getSignal()
|
|
{
|
|
static_assert(std::is_convertible_v<U, value_type>, "value_type cannot be constructed from U.");
|
|
|
|
return Signal<U>(this);
|
|
}
|
|
|
|
Signallable<value_type>* getStaticSignal()
|
|
{
|
|
return &signal_;
|
|
}
|
|
|
|
protected:
|
|
virtual void init() = 0;
|
|
|
|
private:
|
|
PrimitiveThread thread_;
|
|
QueueHandle_t msg_queue_;
|
|
|
|
struct Visitor_
|
|
{
|
|
using parent = Actor<CRTP, Ts...>;
|
|
parent* q;
|
|
|
|
Visitor_(parent* q)
|
|
: q(q)
|
|
{}
|
|
|
|
template<typename U>
|
|
void operator()(const U& u)
|
|
{
|
|
CRTP* s = static_cast<CRTP*>(q);
|
|
s->onSignal(u);
|
|
}
|
|
};
|
|
|
|
void operator()()
|
|
{
|
|
init();
|
|
|
|
value_type data{};
|
|
|
|
while (true)
|
|
{
|
|
const BaseType_t got = xQueueReceive(msg_queue_, &data, portMAX_DELAY);
|
|
|
|
if (got == pdTRUE)
|
|
{
|
|
std::visit(Visitor_{this}, data);
|
|
}
|
|
}
|
|
}
|
|
|
|
Signal<value_type> signal_;
|
|
};
|
|
|
|
template<typename CRTP>
|
|
class Actor<CRTP, void>
|
|
{
|
|
public:
|
|
using value_type = void;
|
|
|
|
Actor(const std::uint32_t queue_size, const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
|
: thread_([](void* d) {
|
|
auto* dd = static_cast<Actor<CRTP, value_type>*>(d);
|
|
(*dd)();
|
|
},
|
|
this, name, priority, stack_size),
|
|
msg_queue_(xQueueCreate(queue_size, sizeof(int))), signal_(*this)
|
|
{
|
|
assert(msg_queue_);
|
|
}
|
|
|
|
virtual ~Actor() {}
|
|
|
|
Signallable<value_type>* getSignal()
|
|
{
|
|
return &signal_;
|
|
}
|
|
|
|
protected:
|
|
virtual void init() = 0;
|
|
|
|
private:
|
|
PrimitiveThread thread_;
|
|
QueueHandle_t msg_queue_;
|
|
|
|
void operator()()
|
|
{
|
|
init();
|
|
|
|
int data;
|
|
|
|
while (true)
|
|
{
|
|
const BaseType_t got = xQueueReceive(msg_queue_, &data, portMAX_DELAY);
|
|
|
|
if (got == pdTRUE)
|
|
{
|
|
auto* crtp = static_cast<CRTP*>(this);
|
|
crtp->onSignal();
|
|
}
|
|
}
|
|
}
|
|
|
|
friend struct Signal_;
|
|
|
|
struct Signal_ : Signallable<value_type>
|
|
{
|
|
using parent = Actor<CRTP, void>;
|
|
parent& q;
|
|
|
|
explicit Signal_(parent& q)
|
|
: q(q)
|
|
{}
|
|
|
|
void emit() override
|
|
{
|
|
const int data = 0;
|
|
|
|
xQueueSend(q.msg_queue_, &data, 0);
|
|
}
|
|
|
|
BaseType_t emitFromIsr() override
|
|
{
|
|
const int data = 0;
|
|
|
|
BaseType_t was_awoken = pdFALSE;
|
|
xQueueSendFromISR(q.msg_queue_, &data, &was_awoken);
|
|
|
|
return was_awoken;
|
|
}
|
|
};
|
|
|
|
Signal_ signal_;
|
|
};
|
|
|
|
}// namespace Threads
|
|
|
|
|
|
#endif /* SKULLC_THREADS_ACTOR_HPP_ */
|