This commit is contained in:
CarlWachter 2025-04-22 16:59:37 +02:00
parent 03d0486f74
commit b07cf9bb22

View File

@ -24,8 +24,6 @@ namespace sta
failsafeTimer_{[](void *){}, nullptr}, failsafeTimer_{[](void *){}, nullptr},
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH}, queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH},
threads_{} threads_{}
queue_{STA_TACOS_STATEMACHINE_QUEUE_LENGTH},
threads_{}
{ {
STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES); STA_ASSERT(STA_TACOS_INITIAL_STATE < STA_TACOS_NUM_STATES);
} }
@ -221,66 +219,6 @@ namespace sta
startThreads(state); startThreads(state);
} }
void Statemachine::registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states)
{
for (uint16_t state : states)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
threads_[state].push_back(thread);
}
}
std::vector<std::shared_ptr<TacosThread>> Statemachine::getActiveThreads()
{
return threads_[tacos::getState()];
}
void Statemachine::startThreads(uint16_t state)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
for (std::shared_ptr<TacosThread> thread : threads_[state])
{
if (!thread->isRunning())
{
thread->start();
}
}
}
void Statemachine::stopThreads(uint16_t state)
{
std::set<std::shared_ptr<TacosThread>> terminated;
for (uint16_t other = 0; other < STA_TACOS_NUM_STATES; ++other)
{
if (other == state)
{
continue;
}
for (std::shared_ptr<TacosThread> thread : threads_[other])
{
// If the thread is currently running but not part of the set of threads that should be running...
if (thread->isRunning() && terminated.count(thread) == 0 && std::count(threads_[state].begin(), threads_[state].end(), thread) == 0)
{
// ...politely request termination.
thread->requestTermination();
terminated.emplace(thread);
}
}
}
}
void Statemachine::updateThreads()
{
uint16_t state = getCurrentState();
stopThreads(state);
startThreads(state);
}
Statemachine* Statemachine::_instance = nullptr; Statemachine* Statemachine::_instance = nullptr;
RtosEvent Statemachine::stateChangeEvent; RtosEvent Statemachine::stateChangeEvent;