Threads: integrate Action with Timers and rework the signalling model a bit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Erki 2021-06-24 13:15:39 +03:00
parent 5d5d7d3ef8
commit 7638f37db7

View File

@ -17,6 +17,7 @@
#include "threads_primitivethread.hpp" #include "threads_primitivethread.hpp"
#include "threads_signal.hpp" #include "threads_signal.hpp"
#include "threads_timer.hpp"
namespace Threads namespace Threads
{ {
@ -36,14 +37,53 @@ public:
(*dd)(); (*dd)();
}, },
this, name, priority, stack_size), this, name, priority, stack_size),
msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(*this) msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(this)
{ {
assert(msg_queue_); assert(msg_queue_);
} }
virtual ~Actor() {} virtual ~Actor() {}
Signallable<value_type>* getSignal() 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)
{}
bool emit(const T& data) override
{
parent::value_type to_send = data;
const BaseType_t success = xQueueSend(q_->msg_queue_, &to_send, 0);
return success == pdTRUE;
}
std::pair<bool, BaseType_t> emitFromIsr(const T& data) override
{
parent::value_type to_send = data;
BaseType_t was_awoken = pdFALSE;
const BaseType_t success = xQueueSendFromISR(q_->msg_queue_, &to_send, &was_awoken);
return {success == pdTRUE, 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_; return &signal_;
} }
@ -89,33 +129,7 @@ private:
} }
} }
friend struct Signal_; Signal<value_type> signal_;
struct Signal_ : Signallable<value_type>
{
using parent = Actor<CRTP, Ts...>;
parent& q;
explicit Signal_(parent& q)
: q(q)
{}
bool emit(const value_type& data) override
{
const BaseType_t success = xQueueSend(q.msg_queue_, &data, 0);
return success == pdTRUE;
}
std::pair<bool, BaseType_t> emitFromIsr(const value_type& data) override
{
BaseType_t was_awoken = pdFALSE;
const BaseType_t success = xQueueSendFromISR(q.msg_queue_, &data, &was_awoken);
return {success == pdTRUE, was_awoken};
}
};
Signal_ signal_;
}; };
template<typename CRTP> template<typename CRTP>