fsfw/osal/linux/PeriodicPosixTask.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

2020-08-28 18:33:29 +02:00
#include "../../tasks/ExecutableObjectIF.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
#include <errno.h>
2020-09-04 14:49:27 +02:00
#include "PeriodicPosixTask.h"
2020-08-28 18:33:29 +02:00
PeriodicPosixTask::PeriodicPosixTask(const char* name_, int priority_,
size_t stackSize_, uint32_t period_, void(deadlineMissedFunc_)()):
PosixThread(name_, priority_, stackSize_), objectList(), started(false),
periodMs(period_), deadlineMissedFunc(deadlineMissedFunc_) {
}
PeriodicPosixTask::~PeriodicPosixTask() {
//Not Implemented
}
void* PeriodicPosixTask::taskEntryPoint(void* arg) {
//The argument is re-interpreted as PollingTask.
PeriodicPosixTask *originalTask(reinterpret_cast<PeriodicPosixTask*>(arg));
//The task's functionality is called.
originalTask->taskFunctionality();
return NULL;
}
ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object) {
ExecutableObjectIF* newObject = objectManager->get<ExecutableObjectIF>(
object);
if (newObject == nullptr) {
2020-09-04 14:49:27 +02:00
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
<< " it implements ExecutableObjectIF!" << std::endl;
2020-08-28 18:33:29 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
objectList.push_back(newObject);
newObject->setTaskIF(this);
2020-09-04 14:49:27 +02:00
return HasReturnvaluesIF::RETURN_OK;
2020-08-28 18:33:29 +02:00
}
ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) {
return PosixThread::sleep((uint64_t)ms*1000000);
}
ReturnValue_t PeriodicPosixTask::startTask(void) {
started = true;
//sif::info << stackSize << std::endl;
PosixThread::createTask(&taskEntryPoint,this);
return HasReturnvaluesIF::RETURN_OK;
}
void PeriodicPosixTask::taskFunctionality(void) {
2020-09-04 14:49:27 +02:00
if(not started) {
2020-08-28 18:33:29 +02:00
suspend();
}
2020-09-04 14:49:27 +02:00
for (auto const &object: objectList) {
object->initializeAfterTaskCreation();
}
2020-08-28 18:33:29 +02:00
uint64_t lastWakeTime = getCurrentMonotonicTimeMs();
//The task's "infinite" inner loop is entered.
while (1) {
2020-09-04 14:49:27 +02:00
for (auto const &object: objectList) {
object->performOperation();
2020-08-28 18:33:29 +02:00
}
2020-09-04 14:49:27 +02:00
if(not PosixThread::delayUntil(&lastWakeTime, periodMs)){
2020-08-28 18:33:29 +02:00
char name[20] = {0};
2020-09-04 14:49:27 +02:00
int status = pthread_getname_np(pthread_self(), name, sizeof(name));
if(status == 0) {
sif::error << "PeriodicPosixTask " << name << ": Deadline "
"missed." << std::endl;
2020-08-28 18:33:29 +02:00
}
else {
2020-09-04 14:49:27 +02:00
sif::error << "PeriodicPosixTask X: Deadline missed. " <<
status << std::endl;
2020-08-28 18:33:29 +02:00
}
if (this->deadlineMissedFunc != nullptr) {
this->deadlineMissedFunc();
}
}
}
}
uint32_t PeriodicPosixTask::getPeriodMs() const {
return periodMs;
}