2018-07-13 18:28:26 +02:00
|
|
|
#include <framework/tasks/ExecutableObjectIF.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
#include <framework/osal/linux/PeriodicPosixTask.h>
|
|
|
|
|
2020-06-19 20:15:58 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2020-05-15 22:53:01 +02:00
|
|
|
PeriodicPosixTask::PeriodicPosixTask(const char* name_, int priority_,
|
|
|
|
size_t stackSize_, uint32_t period_, void(deadlineMissedFunc_)()):
|
2020-06-19 20:15:58 +02:00
|
|
|
PosixThread(name_, priority_, stackSize_), objectList(), started(false),
|
|
|
|
periodMs(period_), deadlineMissedFunc(deadlineMissedFunc_) {
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:19 +02:00
|
|
|
ReturnValue_t PeriodicPosixTask::addComponent(object_id_t object,
|
|
|
|
bool setTaskIF) {
|
2018-07-13 18:28:26 +02:00
|
|
|
ExecutableObjectIF* newObject = objectManager->get<ExecutableObjectIF>(
|
|
|
|
object);
|
2020-06-19 20:14:56 +02:00
|
|
|
if (newObject == nullptr) {
|
|
|
|
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
|
|
|
|
"it implements ExecutableObjectIF" << std::endl;
|
2018-07-13 18:28:26 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-06-23 01:37:25 +02:00
|
|
|
objectList.push_back(newObject);
|
|
|
|
|
2020-06-19 20:14:56 +02:00
|
|
|
if(setTaskIF) {
|
|
|
|
newObject->setTaskIF(this);
|
|
|
|
}
|
2020-06-29 15:55:20 +02:00
|
|
|
|
|
|
|
ReturnValue_t result = newObject->initializeAfterTaskCreation();
|
|
|
|
return result;
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t PeriodicPosixTask::sleepFor(uint32_t ms) {
|
|
|
|
return PosixThread::sleep((uint64_t)ms*1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 01:37:25 +02:00
|
|
|
ReturnValue_t PeriodicPosixTask::startTask(void) {
|
2018-07-13 18:28:26 +02:00
|
|
|
started = true;
|
2020-06-06 15:01:57 +02:00
|
|
|
//sif::info << stackSize << std::endl;
|
|
|
|
PosixThread::createTask(&taskEntryPoint,this);
|
2018-07-13 18:28:26 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-23 01:37:25 +02:00
|
|
|
void PeriodicPosixTask::taskFunctionality(void) {
|
2018-07-13 18:28:26 +02:00
|
|
|
if(!started){
|
|
|
|
suspend();
|
|
|
|
}
|
|
|
|
uint64_t lastWakeTime = getCurrentMonotonicTimeMs();
|
|
|
|
//The task's "infinite" inner loop is entered.
|
|
|
|
while (1) {
|
|
|
|
for (ObjectList::iterator it = objectList.begin();
|
|
|
|
it != objectList.end(); ++it) {
|
|
|
|
(*it)->performOperation();
|
|
|
|
}
|
|
|
|
if(!PosixThread::delayUntil(&lastWakeTime,periodMs)){
|
|
|
|
char name[20] = {0};
|
|
|
|
int status = pthread_getname_np(pthread_self(),name,sizeof(name));
|
2020-06-23 01:37:25 +02:00
|
|
|
if(status == 0){
|
2020-07-08 03:18:09 +02:00
|
|
|
//sif::error << "PeriodicPosixTask " << name << ": Deadline "
|
|
|
|
// "missed." << std::endl;
|
2020-06-23 01:37:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-08 03:18:09 +02:00
|
|
|
//sif::error << "PeriodicPosixTask X: Deadline missed. " <<
|
|
|
|
// status << std::endl;
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
2020-06-23 01:37:25 +02:00
|
|
|
if (this->deadlineMissedFunc != nullptr) {
|
2018-07-13 18:28:26 +02:00
|
|
|
this->deadlineMissedFunc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PeriodicPosixTask::getPeriodMs() const {
|
|
|
|
return periodMs;
|
|
|
|
}
|