larger thread rework to support thread restarting

This commit is contained in:
dario
2024-01-24 21:16:55 +01:00
parent 75fe437f1e
commit 1d1c5f4fc2
4 changed files with 90 additions and 90 deletions

View File

@@ -25,64 +25,19 @@ namespace sta
attribs_{ .name=name, .cb_size=cb_size, .stack_size=stack_size, .priority=prio },
running_{false},
#ifdef STA_TACOS_WATCHDOG_ENABLED
, status_{ThreadStatus::STOPPED}
, status_{ThreadStatus::STOPPED},
#endif // STA_TACOS_WATCHDOG_ENABLED
terminate_{false}
{
STA_ASSERT(stack_size >= 0);
STA_ASSERT(cb_size >= 0);
}
TacosThread::TacosThread()
: RtosThread(RtosHandle<osThreadId_t>(Handle::Deferred(&instance_))),
instance_{ NULL },
attribs_{ },
running_{false}
#ifdef STA_TACOS_WATCHDOG_ENABLED
, status_{ThreadStatus::STOPPED}
#endif // STA_TACOS_WATCHDOG_ENABLED
{}
void TacosThread::entry_point(void* arg)
{
STA_ASSERT(arg != nullptr);
TacosThread* instance = reinterpret_cast<TacosThread*>(arg) ;
instance->loop();
}
void TacosThread::start()
{
STA_ASSERT(!isRunning());
// If this is the first time starting the thread, it has to be started via rtos first.
if (instance_ == NULL)
{
instance_ = osThreadNew(entry_point, this, &attribs_);
STA_ASSERT(instance_ != NULL);
}
// Send a thread start signal.
sysNotify(STA_RTOS_THREAD_FLAG_START);
}
bool TacosThread::isRunning()
{
return running_;
}
osThreadId_t TacosThread::getInstance()
{
STA_ASSERT(isRunning());
return instance_;
}
const char* TacosThread::getName() const
{
return attribs_.name;
}
void TacosThread::init() {}
void TacosThread::loop()
@@ -128,6 +83,19 @@ namespace sta
void TacosThread::cleanup() {}
void TacosThread::sleep(uint32_t ticks)
{
#ifdef STA_TACOS_WATCHDOG_ENABLED
waiting();
#endif // STA_TACOS_WATCHDOG_ENABLED
osDelay(ticks);
#ifdef STA_TACOS_WATCHDOG_ENABLED
heartbeat();
#endif // STA_TACOS_WATCHDOG_ENABLED
}
#ifdef STA_TACOS_WATCHDOG_ENABLED
void TacosThread::heartbeat()
{
@@ -150,6 +118,21 @@ namespace sta
}
#endif // STA_TACOS_WATCHDOG_ENABLED
void TacosThread::requestTermination()
{
terminate_ = true;
}
void TacosThread::deleteTerminationRequest()
{
terminate_ = false;
}
bool TacosThread::isTerminationRequested()
{
return terminate_;
}
bool TacosThread::operator==(const TacosThread& other) const
{
return std::strcmp(this->getName(), other.getName()) == 0;

View File

@@ -15,23 +15,31 @@ namespace sta
switch (thread->getStatus())
{
case ThreadStatus::UNKNOWN:
// TODO: Try to restart the thread.
// Restart the thread.
thread->kill();
thread->start();
restarts_++;
break;
case ThreadStatus::RUNNING:
// Set the thread's status back to UNKNOWN.
thread->resetStatus();
break;
default:
break;
}
sleep(STA_TACOS_WATCHDOG_FREQUENCY);
}
sleep(STA_TACOS_WATCHDOG_FREQUENCY);
}
uint16_t Watchdog::getNumRestarts()
{
return restarts_;
}
Watchdog::Watchdog()
: TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY}
: TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY},
restarts_{ 0 }
{}
Watchdog* Watchdog::_instance = nullptr;