From 05ddf1fb03661a4c56630036cfce387dcaa1bcee Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 3 Jan 2024 14:55:16 +0100 Subject: [PATCH 1/6] Combined watchdog code with latest changes. --- include/sta/tacos/configs/default.hpp | 3 ++ include/sta/tacos/manager.hpp | 11 +++- include/sta/tacos/thread.hpp | 73 +++++++++++++++++++++++++- include/sta/tacos/watchdog.hpp | 74 +++++++++++++++++++++++++++ src/manager.cpp | 15 ++++-- src/thread.cpp | 41 ++++++++++++++- src/watchdog.cpp | 43 ++++++++++++++++ 7 files changed, 250 insertions(+), 10 deletions(-) create mode 100644 include/sta/tacos/watchdog.hpp create mode 100644 src/watchdog.cpp diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index b68739b..8f479c3 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -1,6 +1,9 @@ #ifndef STA_TACOS_CONFIGS_DEFAULT_HPP #define STA_TACOS_CONFIGS_DEFAULT_HPP +// Enable the watchdog provided by TACOS. +#define STA_TACOS_WATCHDOG_ENABLED + // Generally, we assume the TACOS threads to have the highest priorties. #define STA_TACOS_MANAGER_PRIORITY osPriorityHigh #define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh diff --git a/include/sta/tacos/manager.hpp b/include/sta/tacos/manager.hpp index bb300b1..7d8ffd8 100644 --- a/include/sta/tacos/manager.hpp +++ b/include/sta/tacos/manager.hpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -59,10 +60,16 @@ namespace sta */ void registerThread(std::shared_ptr thread, std::list states); + /** + * @brief Get the Active Threads object + * + * @return std::vector> + */ + std::vector> getActiveThreads(); + void init() override; void func() override; - private: static Manager* _instance; @@ -105,7 +112,7 @@ namespace sta * * @ingroup tacos_manager */ - std::set> threads_[STA_TACOS_NUM_STATES]; + std::vector> threads_[STA_TACOS_NUM_STATES]; }; } // namespace tacos } // namespace sta diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 989d081..243d377 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -22,6 +22,45 @@ namespace sta { namespace tacos { + /** + * @brief A status flags for the watchdog. + * + */ + enum class ThreadStatus + { + /** + * @brief This thread wants to be ignored by the watchdog. + * + */ + IGNORED, + + /** + * @brief The thread terminated regularly. The watchdog will not try to restart this thread. + * + */ + STOPPED, + + /** + * @brief The thread's status is unknown. If the watchdog encounters this status, it will try to + * restart the affected thread. + * + */ + UNKNOWN, + + /** + * @brief The thread is running as intended. The watchdog will set this flag back to UNKNOWN. + * + */ + RUNNING, + + /** + * @brief The thread is waiting and might not send a heartbeat signal in a while. The watchdog will + * not do anything in this case. + * + */ + WAITING + }; + /** * @brief Abstract class for thread implementations in Tacos. * @@ -87,8 +126,37 @@ namespace sta */ virtual void cleanup(); - private: +#ifdef STA_TACOS_WATCHDOG_ENABLED + #define BLOCKING(stmt) waiting(); stmt heartbeat() + protected: + /** + * @brief + * + */ + void heartbeat(); + + /** + * @brief Set the thread's status to waiting. Should be called before executing + * + */ + void waiting(); + public: + /** + * @brief Get the current status of the thread. + * + * @return ThreadStatus + */ + ThreadStatus getStatus(); + + /** + * @brief Reset the thread's status to UNKNOWN. Should only be called by the Watchdog. + * + */ + void resetStatus(); +#endif // STA_TACOS_WATCHDOG_ENABLED + + private: /** * @brief Static function to pass to RTOS to run as a thread. Calls the loop function implemented here. */ @@ -98,6 +166,9 @@ namespace sta osThreadId_t instance_; osThreadAttr_t attribs_; bool running_; +#ifdef STA_TACOS_WATCHDOG_ENABLED + ThreadStatus status_; +#endif // STA_TACOS_WATCHDOG_ENABLED }; } } diff --git a/include/sta/tacos/watchdog.hpp b/include/sta/tacos/watchdog.hpp new file mode 100644 index 0000000..cae13e8 --- /dev/null +++ b/include/sta/tacos/watchdog.hpp @@ -0,0 +1,74 @@ +#ifndef STA_TACOS_WATCHDOG_HPP +#define STA_TACOS_WATCHDOG_HPP + +#include +#ifdef STA_TACOS_WATCHDOG_ENABLED + +#ifndef STA_TACOS_WATCHDOG_PRIORITY +# error "TACOS watchdog priority was not specified!" +#endif // STA_TACOS_WATCHDOG_PRIORITY + +#ifndef STA_TACOS_WATCHDOG_FREQUENCY +# error "TACOS watchdog frequency was not specified!" +#endif // STA_TACOS_WATCHDOG_FREQUENCY + + +#include + + +namespace sta +{ + namespace tacos + { + /** + * @brief Watchdog class for TACOS using singleton pattern. + * + */ + class Watchdog: public TacosThread + { + public: + /** + * @brief Getter for the singleton instance. Constructs the instance if no exists. + * + * @return Watchdog* + */ + static Watchdog* instance() + { + static CGuard g; + + if (!_instance) + { + // Create the watchdog singleton instance. + Watchdog::_instance = new Watchdog(); + } + + return _instance; + } + + void func() override; + private: + static Watchdog* _instance; + + class CGuard + { + public: + ~CGuard() + { + if( NULL != Watchdog::_instance ) + { + delete Watchdog::_instance; + Watchdog::_instance = NULL; + } + } + }; + + Watchdog(const Watchdog&); + + Watchdog(); + }; + } // namespace tacos +} // namespace sta + +#endif // STA_TACOS_WATCHDOG_ENABLED + +#endif // STA_TACOS_WATCHDOG_HPP \ No newline at end of file diff --git a/src/manager.cpp b/src/manager.cpp index 468c5fb..bb12329 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace sta @@ -24,10 +25,15 @@ namespace sta { STA_ASSERT(state < STA_TACOS_NUM_STATES); - threads_[state].emplace(thread); + threads_[state].push_back(thread); } } + std::vector> Manager::getActiveThreads() + { + return threads_[tacos::getState()]; + } + void Manager::startThreads(uint16_t state) { STA_ASSERT(state < STA_TACOS_NUM_STATES); @@ -55,7 +61,7 @@ namespace sta for (std::shared_ptr 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 && threads_[state].count(thread) == 0) + if (thread->isRunning() && terminated.count(thread) == 0 && std::count(threads_[state].begin(), threads_[state].end(), thread) == 0) { // ...politely request termination. thread->requestTermination(); @@ -90,13 +96,12 @@ namespace sta } Manager::Manager() - : TacosThread{"Manager", STA_TACOS_MANAGER_PRIORITY} + : TacosThread{"Manager", STA_TACOS_MANAGER_PRIORITY}, + threads_{} { } - //Manager::~Manager(){} - Manager* Manager::_instance = nullptr; } // namespace tacos diff --git a/src/thread.cpp b/src/thread.cpp index 49c6b14..e3a1640 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -23,14 +23,20 @@ namespace sta : RtosThread(RtosHandle(Handle::Deferred(&instance_))), instance_{ NULL }, attribs_{ .name = name, .priority = prio }, - running_{false} + running_{false}, +#ifdef STA_TACOS_WATCHDOG_ENABLED + status_{ThreadStatus::STOPPED} +#endif // STA_TACOS_WATCHDOG_ENABLED {} TacosThread::TacosThread() : RtosThread(RtosHandle(Handle::Deferred(&instance_))), instance_{ NULL }, attribs_{ }, - running_{false} + running_{false}, +#ifdef STA_TACOS_WATCHDOG_ENABLED + status_{ThreadStatus::STOPPED} +#endif // STA_TACOS_WATCHDOG_ENABLED {} void TacosThread::entry_point(void* arg) @@ -94,8 +100,17 @@ namespace sta // Run the thread until the termination flag is set. Reset this while (!isTerminationRequested()) { +#ifdef STA_TACOS_WATCHDOG_ENABLED + // Send a fresh heartbeat signal. + heartbeat(); +#endif // STA_TACOS_WATCHDOG_ENABLED + + // Execute user-space implementation. func(); } +#ifdef STA_TACOS_WATCHDOG_ENABLED + status_ = ThreadStatus::STOPPED; +#endif // STA_TACOS_WATCHDOG_ENABLED // Clear the termination request flag for this thread. deleteTerminationRequest(); @@ -110,6 +125,28 @@ namespace sta void TacosThread::cleanup() {} +#ifdef STA_TACOS_WATCHDOG_ENABLED + void TacosThread::heartbeat() + { + status_ = ThreadStatus::RUNNING; + } + + void TacosThread::waiting() + { + status_ = ThreadStatus::WAITING; + } + + ThreadStatus TacosThread::getStatus() + { + return status_; + } + + void TacosThread::resetStatus() + { + status_ = ThreadStatus::UNKNOWN; + } +#endif // STA_TACOS_WATCHDOG_ENABLED + bool TacosThread::operator==(const TacosThread& other) const { return std::strcmp(this->getName(), other.getName()) == 0; diff --git a/src/watchdog.cpp b/src/watchdog.cpp new file mode 100644 index 0000000..63ff4fe --- /dev/null +++ b/src/watchdog.cpp @@ -0,0 +1,43 @@ +#include + +#ifdef STA_TACOS_WATCHDOG_ENABLED + +#include + +namespace sta +{ + namespace tacos + { + void Watchdog::func() + { + for (std::shared_ptr thread : Manager::instance()->getActiveThreads()) + { + switch (thread->getStatus()) + { + case ThreadStatus::UNKNOWN: + // TODO: Try to restart the thread. + break; + case ThreadStatus::RUNNING: + // Set the thread's status back to UNKNOWN. + thread->resetStatus(); + break; + + default: + break; + } + + sleep(STA_TACOS_WATCHDOG_FREQUENCY); + } + } + + Watchdog::Watchdog() + : TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY} + { + + } + + Watchdog* Watchdog::_instance = nullptr; + } // namespace tacos +} // namespace sta + +#endif // STA_TACOS_WATCHDOG_ENABLED From 12d6303f2e39892b682257e0dfc2848978f3aae2 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 3 Jan 2024 15:17:38 +0100 Subject: [PATCH 2/6] Added working blocking-wrapper --- include/sta/tacos/configs/default.hpp | 2 +- include/sta/tacos/thread.hpp | 9 ++++++++- src/thread.cpp | 8 ++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index 8f479c3..119c247 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -12,4 +12,4 @@ // Per default, we assume state 0 to be the initial state. #define STA_TACOS_INITIAL_STATE 0 -#endif // STA_TACOS_CONFIGS_DEFAULT_HPP \ No newline at end of file +#endif // STA_TACOS_CONFIGS_DEFAULT_HPP diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 243d377..d8f3b29 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -127,7 +127,14 @@ namespace sta virtual void cleanup(); #ifdef STA_TACOS_WATCHDOG_ENABLED - #define BLOCKING(stmt) waiting(); stmt heartbeat() + /** + * @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable. + * + */ + #define blocking(...) \ + waiting(); \ + __VA_ARGS__ \ + heartbeat(); \ protected: /** diff --git a/src/thread.cpp b/src/thread.cpp index e3a1640..662ba62 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -23,9 +23,9 @@ namespace sta : RtosThread(RtosHandle(Handle::Deferred(&instance_))), instance_{ NULL }, attribs_{ .name = name, .priority = prio }, - running_{false}, + running_{false} #ifdef STA_TACOS_WATCHDOG_ENABLED - status_{ThreadStatus::STOPPED} + , status_{ThreadStatus::STOPPED} #endif // STA_TACOS_WATCHDOG_ENABLED {} @@ -33,9 +33,9 @@ namespace sta : RtosThread(RtosHandle(Handle::Deferred(&instance_))), instance_{ NULL }, attribs_{ }, - running_{false}, + running_{false} #ifdef STA_TACOS_WATCHDOG_ENABLED - status_{ThreadStatus::STOPPED} + , status_{ThreadStatus::STOPPED} #endif // STA_TACOS_WATCHDOG_ENABLED {} From 9b813832e252dbc170cbb726691a596101633761 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 18 Jan 2024 00:36:22 +0100 Subject: [PATCH 3/6] Added initWatchdog function to startup --- include/sta/tacos.hpp | 10 ++++++++-- src/startup.cpp | 20 ++++++++++++++++++-- src/watchdog.cpp | 4 +--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index bec1616..747c6a5 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -52,11 +52,17 @@ namespace sta * @tparam T The class of the thread to be created. A subclass of TacosThread. * @param states A list of states in which the thread should run. * @param args The constructor arguments for the provided class. + * + * @return uint16_t A shared pointer to the created thread. */ template - void addThread(std::list states, Args ... args) + std::shared_ptr addThread(std::list states, Args ... args) { - Manager::instance()->registerThread(std::make_shared(args...), states); + std::shared_ptr thread_ptr = std::make_shared(args...); + + Manager::instance()->registerThread(thread_ptr, states); + + return thread_ptr; } } // namespace tacos } diff --git a/src/startup.cpp b/src/startup.cpp index 6550cc1..2130bc6 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -26,6 +26,7 @@ // Tacos-specific includes. #include #include +#include // The UART mutex defined in freertos.c @@ -49,8 +50,6 @@ namespace sta UARTSettings settings = { .mode = UARTMode::RX_TX }; STM32UART * intf_ptr = new STM32UART(&huart2, settings, mutex); Debug = new PrintableUART(intf_ptr); - - STA_DEBUG_PRINTLN("UART SUCCESSFULLY INITIALIZED"); } } #endif // STA_DEBUGGING_ENABLED @@ -86,6 +85,19 @@ namespace sta Manager::instance()->start(); } + +#ifdef STA_TACOS_WATCHDOG_ENABLED + STA_WEAK + void onWatchdogInit() + {} + + void initWatchdog() + { + onWatchdogInit(); + + Watchdog::instance()->start(); + } +#endif // STA_TACOS_WATCHDOG_ENABLED } // namespace tacos @@ -101,6 +113,10 @@ namespace sta tacos::initStatemachine(); tacos::initManager(); + +#ifdef STA_TACOS_WATCHDOG_ENABLED + tacos::initWatchdog(); +#endif // STA_TACOS_WATCHDOG_ENABLED } } // namespace rtos } // namespace sta diff --git a/src/watchdog.cpp b/src/watchdog.cpp index 63ff4fe..f82fa89 100644 --- a/src/watchdog.cpp +++ b/src/watchdog.cpp @@ -32,9 +32,7 @@ namespace sta Watchdog::Watchdog() : TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY} - { - - } + {} Watchdog* Watchdog::_instance = nullptr; } // namespace tacos From d06a15e986b0aa307908f7538502344289ec12c3 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 24 Jan 2024 21:16:55 +0100 Subject: [PATCH 4/6] larger thread rework to support thread restarting --- include/sta/tacos/thread.hpp | 69 ++++++++++++++--------------- include/sta/tacos/watchdog.hpp | 15 ++++++- src/thread.cpp | 81 +++++++++++++--------------------- src/watchdog.cpp | 18 +++++--- 4 files changed, 91 insertions(+), 92 deletions(-) diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index d8f3b29..8517c77 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -10,6 +10,7 @@ #include +#include #include /** @@ -22,6 +23,7 @@ namespace sta { namespace tacos { +#ifdef STA_TACOS_WATCHDOG_ENABLED /** * @brief A status flags for the watchdog. * @@ -60,6 +62,7 @@ namespace sta */ WAITING }; +#endif // STA_TACOS_WATCHDOG_ENABLED /** * @brief Abstract class for thread implementations in Tacos. @@ -74,40 +77,17 @@ namespace sta */ TacosThread(const char* name, osPriority_t prio); - TacosThread(); - virtual ~TacosThread(); - /** - * @brief Start the execution of this thread. - */ - void start(); - /** * @brief Checks if this thread is currently running. */ bool isRunning(); - /** - * @brief Get the currently running instance. - * - * @return The currently running instance id. - */ - osThreadId_t getInstance(); - - /** - * @brief Get the name of this thread. - */ - const char* getName() const; - bool operator==(const TacosThread& other) const; bool operator<(const TacosThread& other) const; - /** - * @brief A function that wraps this task's functionality in a loop. This loop will run until - * termination is requested. - */ - void loop(); + void loop() override; /** * @brief This function is executed first when this thread is started. @@ -126,15 +106,23 @@ namespace sta */ virtual void cleanup(); + /** + * @brief Sleep for a given number of ticks. Sets itself to WAITING if the watchdog is enabled, preventing + * the watchdog from restarting this thread. + * + * @param ticks + */ + void sleep(uint32_t ticks); + #ifdef STA_TACOS_WATCHDOG_ENABLED - /** - * @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable. - * - */ - #define blocking(...) \ - waiting(); \ - __VA_ARGS__ \ - heartbeat(); \ + /** + * @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable. + * + */ + #define blocking(...) \ + waiting(); \ + __VA_ARGS__ \ + heartbeat(); \ protected: /** @@ -163,12 +151,22 @@ namespace sta void resetStatus(); #endif // STA_TACOS_WATCHDOG_ENABLED - private: /** - * @brief Static function to pass to RTOS to run as a thread. Calls the loop function implemented here. + * @brief Send termination request to thread. */ - static void entry_point(void* arg); + void requestTermination(); + /** + * @brief Clear the termination request flag for this thread. + */ + void deleteTerminationRequest(); + + /** + * @brief Resets the terminate bool to false. + * + * @return Returns the previous value of this variable. + */ + bool isTerminationRequested(); private: osThreadId_t instance_; osThreadAttr_t attribs_; @@ -176,6 +174,7 @@ namespace sta #ifdef STA_TACOS_WATCHDOG_ENABLED ThreadStatus status_; #endif // STA_TACOS_WATCHDOG_ENABLED + bool terminate_; }; } } diff --git a/include/sta/tacos/watchdog.hpp b/include/sta/tacos/watchdog.hpp index cae13e8..5953c57 100644 --- a/include/sta/tacos/watchdog.hpp +++ b/include/sta/tacos/watchdog.hpp @@ -46,6 +46,13 @@ namespace sta } void func() override; + + /** + * @brief Get the number of thread restarts during the program's runtime. + * + * @return uint16_t The number of thread restarts. + */ + uint16_t getNumRestarts(); private: static Watchdog* _instance; @@ -62,9 +69,13 @@ namespace sta } }; - Watchdog(const Watchdog&); + Watchdog(); - Watchdog(); + Watchdog(const Watchdog&); + + ~Watchdog() {} + private: + uint16_t restarts_; }; } // namespace tacos } // namespace sta diff --git a/src/thread.cpp b/src/thread.cpp index 662ba62..0f4ebda 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -20,66 +20,19 @@ namespace sta namespace tacos { TacosThread::TacosThread(const char* name, osPriority_t prio) - : RtosThread(RtosHandle(Handle::Deferred(&instance_))), - instance_{ NULL }, - attribs_{ .name = name, .priority = prio }, + : RtosThread{name, prio}, running_{false} #ifdef STA_TACOS_WATCHDOG_ENABLED - , status_{ThreadStatus::STOPPED} + , status_{ThreadStatus::STOPPED}, #endif // STA_TACOS_WATCHDOG_ENABLED + terminate_{false} {} - TacosThread::TacosThread() - : RtosThread(RtosHandle(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(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() @@ -125,6 +78,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() { @@ -147,6 +113,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; diff --git a/src/watchdog.cpp b/src/watchdog.cpp index f82fa89..2550893 100644 --- a/src/watchdog.cpp +++ b/src/watchdog.cpp @@ -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; From 2617521444c6c98af187736a405827d205ec8a09 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 7 Feb 2024 17:04:19 +0100 Subject: [PATCH 5/6] Fixed thread starting to work correctly --- include/sta/tacos/thread.hpp | 4 ++++ src/thread.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 8517c77..842b27e 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -89,6 +89,8 @@ namespace sta void loop() override; + void start() override; + /** * @brief This function is executed first when this thread is started. */ @@ -149,6 +151,8 @@ namespace sta * */ void resetStatus(); + + void watchdogIgnore(); #endif // STA_TACOS_WATCHDOG_ENABLED /** diff --git a/src/thread.cpp b/src/thread.cpp index 0f4ebda..35175e0 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -33,6 +33,18 @@ namespace sta return running_; } + void TacosThread::start() + { + if (getInstance() == NULL) + { + RtosThread::start(); + } + else + { + sysNotify(STA_RTOS_THREAD_FLAG_START); + } + } + void TacosThread::init() {} void TacosThread::loop() @@ -54,8 +66,11 @@ namespace sta while (!isTerminationRequested()) { #ifdef STA_TACOS_WATCHDOG_ENABLED - // Send a fresh heartbeat signal. - heartbeat(); + if (status_ == ThreadStatus::UNKNOWN) + { + // Send a fresh heartbeat signal. + heartbeat(); + } #endif // STA_TACOS_WATCHDOG_ENABLED // Execute user-space implementation. @@ -111,6 +126,11 @@ namespace sta { status_ = ThreadStatus::UNKNOWN; } + + void TacosThread::watchdogIgnore() + { + status_ = ThreadStatus::IGNORED; + } #endif // STA_TACOS_WATCHDOG_ENABLED void TacosThread::requestTermination() From 7e5a64ebf4c6fd3af4a89e293c8d373b07eee018 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 7 Feb 2024 18:48:32 +0100 Subject: [PATCH 6/6] Added comment for watchdogIgnore --- include/sta/tacos/thread.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 842b27e..10a9efe 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -152,6 +152,9 @@ namespace sta */ void resetStatus(); + /** + * @brief This thread status tells the watchdog to ignore this thread. + */ void watchdogIgnore(); #endif // STA_TACOS_WATCHDOG_ENABLED