70 lines
1.0 KiB
C++
70 lines
1.0 KiB
C++
/*
|
|
* threads_actor_thread.cpp
|
|
*
|
|
* Created on: Sep 25, 2021
|
|
* Author: erki
|
|
*/
|
|
|
|
#include "threads_actor_thread.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "threads_actor.hpp"
|
|
|
|
namespace Threads
|
|
{
|
|
|
|
void ActorThread::run_(void* data)
|
|
{
|
|
auto* t = reinterpret_cast<ActorThread*>(data);
|
|
|
|
for (IActor* p : t->actors_)
|
|
{
|
|
if (p)
|
|
p->init();
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
bool remainingWork = false;
|
|
|
|
for (IActor* p : t->actors_)
|
|
{
|
|
if (p && p->hasWork())
|
|
{
|
|
p->doWork();
|
|
remainingWork = remainingWork || p->hasWork();
|
|
t->yield();
|
|
}
|
|
}
|
|
|
|
if (!remainingWork)
|
|
{
|
|
const bool unused = t->notifyWait(portMAX_DELAY);
|
|
(void)unused;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ActorThread::acceptActor_(IActor* actor)
|
|
{
|
|
auto it = std::find(actors_.begin(), actors_.end(), nullptr);
|
|
|
|
if (it == actors_.end())
|
|
return;
|
|
|
|
*it = actor;
|
|
}
|
|
|
|
void ActorThread::removeActor_(IActor* actor)
|
|
{
|
|
auto it = std::find(actors_.begin(), actors_.end(), actor);
|
|
|
|
if (it == actors_.end())
|
|
return;
|
|
|
|
*it = nullptr;
|
|
}
|
|
|
|
}
|