larger thread rework to support thread restarting

This commit is contained in:
dario
2024-01-24 21:16:55 +01:00
parent 9b813832e2
commit d06a15e986
4 changed files with 91 additions and 92 deletions

View File

@@ -10,6 +10,7 @@
#include <cmsis_os2.h>
#include <sta/config.hpp>
#include <sta/rtos/thread.hpp>
/**
@@ -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_;
};
}
}

View File

@@ -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