TACOS/Tacos/src/thread.cpp

96 lines
1.7 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 <functional>
#include <cstring>
namespace sta
{
namespace tacos
{
TacosThread::TacosThread(const char* name, osPriority_t prio)
: RtosThread(RtosHandle<osThreadId_t>(Handle::Deferred(&instance_))),
instance_{ NULL },
attribs_{ .name = name, .priority = prio }
{}
TacosThread::TacosThread()
: RtosThread(RtosHandle<osThreadId_t>(Handle::Deferred(&instance_))),
instance_{ NULL },
attribs_{ }
{}
void TacosThread::entry_point(void* arg)
{
STA_ASSERT(arg != nullptr);
TacosThread* instance = reinterpret_cast<TacosThread*>(arg) ;
instance->loop();
}
void TacosThread::start()
{
STA_ASSERT(!isRunning());
instance_ = osThreadNew(entry_point, this, &attribs_);
STA_ASSERT(instance_ != NULL);
}
bool TacosThread::isRunning()
{
return instance_ != NULL;
}
osThreadId_t TacosThread::getInstance()
{
STA_ASSERT(isRunning());
return instance_;
}
const char* TacosThread::getName() const
{
return attribs_.name;
}
void TacosThread::loop()
{
init();
while ((osEventFlagsGet(instance_) & STA_RTOS_THREAD_FLAG_TERMINATE) == 0)
{
func();
}
instance_ = NULL;
osThreadExit();
}
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