-
Notifications
You must be signed in to change notification settings - Fork 3
Specialization_of_Tasks
A task is always the processing of data coming from the input channel of the task. It is always a good practice that a task doesn't hold any data except for the configuration of the task, e.g. to define the output channels to start further processings.
To implement the processing of a task, the pure abstract method execute shall be overridden by the specialization of the task.
To simplify the set-up of a task, it is recommended to use the class TaskProvider, which provides the static management data of a task. A task to print the data pushed on the single data channel looks like:
#include <scheduler.h>
#include <schedulePolicyFifo.h>
#include <task.h>
#include "SingleDataChannel.hpp"
class SingleDataPrinterTask: public Tasking::TaskProvider<1u, Tasking::SchedulePolicyFifo>
{
public:
SingleDataPrinterTask(Tasking::Scheduler& scheduler);
void execute(void) override;
};
SingleDataPrinterTask::SingleDataPrinterTask(Tasking::Scheduler& scheduler): TaskProvider(scheduler)
{
inputs[0].configure(1u);
}
void SingleDataPrinterTask::execute(void)
{
std::cout << inputs[0].getChannel<SingleDataChannel<std::string>>()->read() << std::endl;
}The constructor of a task needs a reference to a scheduler. The task will then be managed and executed by this scheduler. The task should configure the settings of its inputs. In this example, the task has one input, which expects one push call on the channel associated with this input. For further details on the input configuration see activation of tasks.
There is a second form to configure the input and associate it directly with a channel. This configuration needs the reference to the channel as a parameter of the constructor or as a child of the task class.
If a secondary configuration step is needed, the specialization of a task can override the abstract method initialize. In the following example, the validity of a task is checked and the timing of an associated event is set to 500 ms after reset. A task is valid when all its inputs are configured and connected to a channel. The method will be called by calling the initialize function of the associated scheduler.
void SingleDataPrinterTask::initialize(void)
{
assert(isValid());
inputs[0].getChannel<Tasking::Event>()->setRelativeTiming(500);
}