28template <
typename MsgType,
size_t MaxSubscribers = 4>
32 bool (*post_fn)(
void*,
const MsgType&){
nullptr};
35 static inline Subscriber subs_[MaxSubscribers]{};
36 static inline size_t count_{0};
45 static bool subscribe(
void* actor,
bool (*post_fn)(
void*,
const MsgType&)) {
47 if (count_ >= MaxSubscribers) {
51 for (
size_t i = 0; i < count_; ++i) {
52 if (subs_[i].actor == actor) {
57 subs_[count_++] = {actor, post_fn};
69 for (
size_t i = 0; i < count_; ++i) {
70 if (subs_[i].actor == actor) {
71 for (
size_t j = i; j + 1 < count_; ++j) {
72 subs_[j] = subs_[j + 1];
89 static size_t publish(
const MsgType& msg) {
90 Subscriber local_subs[MaxSubscribers];
95 for (
size_t i = 0; i < n; ++i) {
96 local_subs[i] = subs_[i];
101 size_t delivered = 0;
102 for (
size_t i = 0; i < n; ++i) {
103 if (local_subs[i].post_fn && local_subs[i].post_fn(local_subs[i].actor, msg)) {
134template <
typename MsgType>
146inline size_t publish(uint32_t
id, uintptr_t arg = 0) {
173template <
typename MsgType = Message,
size_t QueueSize = 8>
175 static_assert(QueueSize > 0,
"QueueSize must be greater than 0");
196 return handle_.is_valid();
207 bool post(
const MsgType& msg) {
209 if (count_ >= QueueSize) {
214 tail_ = (tail_ + 1) % QueueSize;
232 if (count_ >= QueueSize) {
236 queue_[tail_] = std::move(msg);
237 tail_ = (tail_ + 1) % QueueSize;
254 template <
typename U = MsgType>
255 typename std::enable_if<std::is_same<U, Message>::value,
bool>::type
256 post(uint32_t
id, uintptr_t arg = 0) {
283 static void task_entry(
void* arg) {
292 while (count_ == 0) {
300 msg = std::move(queue_[head_]);
301 head_ = (head_ + 1) % QueueSize;
311 MsgType queue_[QueueSize]{};
315 bool waiting_{
false};
316 TaskHandle handle_{};
virtual void on_message(const MsgType &msg)=0
メッセージ受信時に呼ばれるハンドラ関数
bool post(const MsgType &msg)
この Actor のメッセージキューへ直接メッセージを送信します (コピー渡し)
Definition actor.hpp:207
std::enable_if< std::is_same< U, Message >::value, bool >::type post(uint32_t id, uintptr_t arg=0)
tk::Message 型の場合のコンビニエンス送信関数
Definition actor.hpp:256
TaskHandle handle() const
Definition actor.hpp:261
bool post(MsgType &&msg)
この Actor のメッセージキューへ直接メッセージを送信します (ムーブ渡し)
Definition actor.hpp:230
virtual ~Actor()
Definition actor.hpp:179
bool start(uint32_t prio=PRIO_NORMAL)
Actor をタスクとして生成し、EventHub へ自動登録します
Definition actor.hpp:191
virtual void on_start()
タスク起動時、イベントループ突入直前に1度だけ呼ばれる初期化関数
Definition actor.hpp:270
メッセージ型ごとの超軽量 Pub/Sub ディスパッチャ
Definition actor.hpp:29
static bool unsubscribe(void *actor)
Actor の購読登録を解除します
Definition actor.hpp:67
static size_t publish(const MsgType &msg)
このメッセージ型を購読しているすべての Actor へ同報配信します
Definition actor.hpp:89
static bool subscribe(void *actor, bool(*post_fn)(void *, const MsgType &))
Actor をこのメッセージ型の購読者として登録します
Definition actor.hpp:45
static size_t subscriber_count()
Definition actor.hpp:111
void block_current_task_locked()
Definition scheduler.cpp:333
uint32_t sched_lock()
Definition scheduler.cpp:325
void sched_unlock(uint32_t save)
Definition scheduler.cpp:329
void wake_task_locked(TaskHandle handle)
Definition scheduler.cpp:340
size_t publish(const MsgType &msg)
メッセージの型を指定して、購読しているすべての Actor へ同報配信します
Definition actor.hpp:135
@ PRIO_NORMAL
通常優先度 (デフォルト)
Definition kernel.hpp:42
TaskHandle create(TaskFn fn, void *arg=nullptr, uint32_t prio=PRIO_NORMAL)
タスクを生成・登録します
Definition scheduler.cpp:204
デフォルトの軽量メッセージ構造体
Definition actor.hpp:14
uintptr_t arg
引数 (ポインタまたは整数値)
Definition actor.hpp:16
uint32_t id
メッセージ識別子 / コマンドID
Definition actor.hpp:15
タスクの識別ハンドル
Definition kernel.hpp:49