mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-08-02 08:41:54 +00:00
146 lines
2.8 KiB
C++
146 lines
2.8 KiB
C++
/*
|
|
* thread.cpp
|
|
*
|
|
* Created on: Sep 14, 2023
|
|
* Author: Dario
|
|
*/
|
|
|
|
|
|
#include <sta/tacos/thread.hpp>
|
|
#include <sta/debug/assert.hpp>
|
|
#include <sta/debug/debug.hpp>
|
|
#include <sta/rtos/system/events.hpp>
|
|
|
|
#include <functional>
|
|
#include <cstring>
|
|
|
|
|
|
namespace sta
|
|
{
|
|
namespace tacos
|
|
{
|
|
TacosThread::TacosThread(const char* name, osPriority_t prio)
|
|
: RtosThread{name, prio},
|
|
running_{false}
|
|
#ifdef STA_TACOS_WATCHDOG_ENABLED
|
|
, status_{ThreadStatus::STOPPED},
|
|
#endif // STA_TACOS_WATCHDOG_ENABLED
|
|
terminate_{false}
|
|
{}
|
|
|
|
bool TacosThread::isRunning()
|
|
{
|
|
return running_;
|
|
}
|
|
|
|
void TacosThread::init() {}
|
|
|
|
void TacosThread::loop()
|
|
{
|
|
// Infinite loop allowing quick restarts of this thread after termination.
|
|
while (true)
|
|
{
|
|
// The thread has to wait until the system startup has been completed.
|
|
rtos::waitForStartupEvent();
|
|
|
|
// Wait for a thread start flag.
|
|
wait(STA_RTOS_THREAD_FLAG_START);
|
|
|
|
// Call user-defined initialization code.
|
|
running_ = true;
|
|
init();
|
|
|
|
// 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();
|
|
|
|
// Call user-defined cleanup code.
|
|
cleanup();
|
|
running_ = false;
|
|
}
|
|
|
|
osThreadExit();
|
|
}
|
|
|
|
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()
|
|
{
|
|
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
|
|
|
|
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;
|
|
}
|
|
|
|
bool TacosThread::operator<(const TacosThread& other) const
|
|
{
|
|
return std::strcmp(this->getName(), other.getName()) < 0;
|
|
}
|
|
|
|
TacosThread::~TacosThread(){}
|
|
|
|
} // namespace tacos
|
|
} // namespace sta
|
|
|