mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-08-02 08:41:54 +00:00
larger thread rework to support thread restarting
This commit is contained in:
parent
9b813832e2
commit
d06a15e986
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include <cmsis_os2.h>
|
#include <cmsis_os2.h>
|
||||||
|
|
||||||
|
#include <sta/config.hpp>
|
||||||
#include <sta/rtos/thread.hpp>
|
#include <sta/rtos/thread.hpp>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,6 +23,7 @@ namespace sta
|
|||||||
{
|
{
|
||||||
namespace tacos
|
namespace tacos
|
||||||
{
|
{
|
||||||
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
/**
|
/**
|
||||||
* @brief A status flags for the watchdog.
|
* @brief A status flags for the watchdog.
|
||||||
*
|
*
|
||||||
@ -60,6 +62,7 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
WAITING
|
WAITING
|
||||||
};
|
};
|
||||||
|
#endif // STA_TACOS_WATCHDOG_ENABLED
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Abstract class for thread implementations in Tacos.
|
* @brief Abstract class for thread implementations in Tacos.
|
||||||
@ -74,40 +77,17 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
TacosThread(const char* name, osPriority_t prio);
|
TacosThread(const char* name, osPriority_t prio);
|
||||||
|
|
||||||
TacosThread();
|
|
||||||
|
|
||||||
virtual ~TacosThread();
|
virtual ~TacosThread();
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Start the execution of this thread.
|
|
||||||
*/
|
|
||||||
void start();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Checks if this thread is currently running.
|
* @brief Checks if this thread is currently running.
|
||||||
*/
|
*/
|
||||||
bool isRunning();
|
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;
|
||||||
bool operator<(const TacosThread& other) const;
|
bool operator<(const TacosThread& other) const;
|
||||||
|
|
||||||
/**
|
void loop() override;
|
||||||
* @brief A function that wraps this task's functionality in a loop. This loop will run until
|
|
||||||
* termination is requested.
|
|
||||||
*/
|
|
||||||
void loop();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function is executed first when this thread is started.
|
* @brief This function is executed first when this thread is started.
|
||||||
@ -126,15 +106,23 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
virtual void cleanup();
|
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
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
/**
|
/**
|
||||||
* @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable.
|
* @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#define blocking(...) \
|
#define blocking(...) \
|
||||||
waiting(); \
|
waiting(); \
|
||||||
__VA_ARGS__ \
|
__VA_ARGS__ \
|
||||||
heartbeat(); \
|
heartbeat(); \
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
@ -163,12 +151,22 @@ namespace sta
|
|||||||
void resetStatus();
|
void resetStatus();
|
||||||
#endif // STA_TACOS_WATCHDOG_ENABLED
|
#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:
|
private:
|
||||||
osThreadId_t instance_;
|
osThreadId_t instance_;
|
||||||
osThreadAttr_t attribs_;
|
osThreadAttr_t attribs_;
|
||||||
@ -176,6 +174,7 @@ namespace sta
|
|||||||
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
ThreadStatus status_;
|
ThreadStatus status_;
|
||||||
#endif // STA_TACOS_WATCHDOG_ENABLED
|
#endif // STA_TACOS_WATCHDOG_ENABLED
|
||||||
|
bool terminate_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,13 @@ namespace sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
void func() override;
|
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:
|
private:
|
||||||
static Watchdog* _instance;
|
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 tacos
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -20,66 +20,19 @@ namespace sta
|
|||||||
namespace tacos
|
namespace tacos
|
||||||
{
|
{
|
||||||
TacosThread::TacosThread(const char* name, osPriority_t prio)
|
TacosThread::TacosThread(const char* name, osPriority_t prio)
|
||||||
: RtosThread(RtosHandle<osThreadId_t>(Handle::Deferred(&instance_))),
|
: RtosThread{name, prio},
|
||||||
instance_{ NULL },
|
|
||||||
attribs_{ .name = name, .priority = prio },
|
|
||||||
running_{false}
|
running_{false}
|
||||||
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
, status_{ThreadStatus::STOPPED}
|
, status_{ThreadStatus::STOPPED},
|
||||||
#endif // STA_TACOS_WATCHDOG_ENABLED
|
#endif // STA_TACOS_WATCHDOG_ENABLED
|
||||||
|
terminate_{false}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
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()
|
bool TacosThread::isRunning()
|
||||||
{
|
{
|
||||||
return running_;
|
return running_;
|
||||||
}
|
}
|
||||||
|
|
||||||
osThreadId_t TacosThread::getInstance()
|
|
||||||
{
|
|
||||||
STA_ASSERT(isRunning());
|
|
||||||
|
|
||||||
return instance_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* TacosThread::getName() const
|
|
||||||
{
|
|
||||||
return attribs_.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TacosThread::init() {}
|
void TacosThread::init() {}
|
||||||
|
|
||||||
void TacosThread::loop()
|
void TacosThread::loop()
|
||||||
@ -125,6 +78,19 @@ namespace sta
|
|||||||
|
|
||||||
void TacosThread::cleanup() {}
|
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
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
||||||
void TacosThread::heartbeat()
|
void TacosThread::heartbeat()
|
||||||
{
|
{
|
||||||
@ -147,6 +113,21 @@ namespace sta
|
|||||||
}
|
}
|
||||||
#endif // STA_TACOS_WATCHDOG_ENABLED
|
#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
|
bool TacosThread::operator==(const TacosThread& other) const
|
||||||
{
|
{
|
||||||
return std::strcmp(this->getName(), other.getName()) == 0;
|
return std::strcmp(this->getName(), other.getName()) == 0;
|
||||||
|
@ -15,23 +15,31 @@ namespace sta
|
|||||||
switch (thread->getStatus())
|
switch (thread->getStatus())
|
||||||
{
|
{
|
||||||
case ThreadStatus::UNKNOWN:
|
case ThreadStatus::UNKNOWN:
|
||||||
// TODO: Try to restart the thread.
|
// Restart the thread.
|
||||||
|
thread->kill();
|
||||||
|
thread->start();
|
||||||
|
|
||||||
|
restarts_++;
|
||||||
break;
|
break;
|
||||||
case ThreadStatus::RUNNING:
|
case ThreadStatus::RUNNING:
|
||||||
// Set the thread's status back to UNKNOWN.
|
// Set the thread's status back to UNKNOWN.
|
||||||
thread->resetStatus();
|
thread->resetStatus();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(STA_TACOS_WATCHDOG_FREQUENCY);
|
|
||||||
}
|
}
|
||||||
|
sleep(STA_TACOS_WATCHDOG_FREQUENCY);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Watchdog::getNumRestarts()
|
||||||
|
{
|
||||||
|
return restarts_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Watchdog::Watchdog()
|
Watchdog::Watchdog()
|
||||||
: TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY}
|
: TacosThread{"Watchdog", STA_TACOS_WATCHDOG_PRIORITY},
|
||||||
|
restarts_{ 0 }
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Watchdog* Watchdog::_instance = nullptr;
|
Watchdog* Watchdog::_instance = nullptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user