Thread rework to support watchdog restarting

This commit is contained in:
dario
2024-01-24 21:17:42 +01:00
parent ce2bb459cf
commit 3feedc948f
3 changed files with 90 additions and 68 deletions

View File

@@ -1,5 +1,5 @@
#include <sta/rtos/thread.hpp>
#include <sta/debug/assert.hpp>
#define IS_THREAD_SYS_FLAGS(flags) ( ( flags & STA_RTOS_THREAD_FLAGS_VALID_SYS_BITS ) == flags )
#define IS_THREAD_USER_FLAGS(flags) ( ( flags & STA_RTOS_THREAD_FLAGS_VALID_USER_BITS ) == flags )
@@ -7,35 +7,64 @@
namespace sta
{
RtosThread::RtosThread()
: handle_(nullptr),
terminate_{false}
RtosThread::RtosThread(const char* name, osPriority_t prio)
: instance_{NULL},
attribs_{ .name = name, .priority = prio }
{}
RtosThread::RtosThread(const Handle & handle)
: handle_{handle},
terminate_{false}
{}
osThreadId_t RtosThread::getInstance()
{
return instance_;
}
const char* RtosThread::getName() const
{
return attribs_.name;
}
void RtosThread::start()
{
// Check if there is no instance that is currently running.
STA_ASSERT(instance_ != NULL);
instance_ = osThreadNew(entry_point, this, &attribs_);
STA_ASSERT(instance_ != NULL);
// Send a thread start signal.
sysNotify(STA_RTOS_THREAD_FLAG_START);
}
void RtosThread::entry_point(void* arg)
{
STA_ASSERT(arg != nullptr);
RtosThread* instance = reinterpret_cast<RtosThread*>(arg) ;
instance->loop();
}
void RtosThread::sleep(uint32_t ticks)
{
// Make sure that the calling thread is this thread. If not, the
// developer very likely has made a mistake.
STA_ASSERT(osThreadGetId() == instance_);
osDelay(ticks);
}
void RtosThread::notify(uint32_t flags)
{
STA_ASSERT(handle_.get() != nullptr);
STA_ASSERT(instance_ != NULL);
STA_ASSERT_MSG(IS_THREAD_USER_FLAGS(flags), "Only user flags allowed");
osThreadFlagsSet(handle_.get(), flags);
osThreadFlagsSet(instance_, flags);
}
void RtosThread::sysNotify(uint32_t flags)
{
STA_ASSERT(handle_.get() != nullptr);
STA_ASSERT(instance_ != NULL);
STA_ASSERT_MSG(IS_THREAD_SYS_FLAGS(flags), "Only system flags allowed");
uint32_t rslt = osThreadFlagsSet(handle_.get(), flags);
uint32_t rslt = osThreadFlagsSet(instance_, flags);
STA_ASSERT_MSG(rslt != osFlagsErrorUnknown, "Unknown thread flag error.");
STA_ASSERT_MSG(rslt != osFlagsErrorParameter, "Parameter thread_id is not a valid thread or flags has highest bit set.");
@@ -44,11 +73,19 @@ namespace sta
uint32_t RtosThread::wait(uint32_t flags)
{
// Make sure that the calling thread is this thread. If not, the
// developer very likely has made a mistake.
STA_ASSERT(osThreadGetId() == instance_);
return osThreadFlagsWait(flags, osFlagsWaitAny, osWaitForever);
}
uint32_t RtosThread::clear(uint32_t flags)
{
// Make sure that the calling thread is this thread. If not, the
// developer very likely has made a mistake.
STA_ASSERT(osThreadGetId() == instance_);
uint32_t setFlags = osThreadFlagsClear(flags);
STA_ASSERT(setFlags != (uint32_t)osError);
@@ -58,29 +95,17 @@ namespace sta
uint32_t RtosThread::getFlags()
{
STA_ASSERT(handle_.get() != nullptr);
// Make sure that the calling thread is this thread. If not, the
// developer very likely has made a mistake.
STA_ASSERT(osThreadGetId() == instance_);
return osThreadFlagsGet();
}
void RtosThread::requestTermination()
{
terminate_ = true;
}
void RtosThread::deleteTerminationRequest()
{
terminate_ = false;
}
bool RtosThread::isTerminationRequested()
{
return terminate_;
}
void RtosThread::kill()
{
STA_ASSERT(handle_.get() != nullptr);
osThreadTerminate(handle_.get());
STA_ASSERT(instance_ != NULL);
osThreadTerminate(instance_);
instance_ = NULL;
}
} // namespace sta