Tasks and methods

jesseb9900 wrote on Tuesday, October 25, 2016:


In my project I have a lot of functions to calculate things in sequence (transform raw data -> interpreted data -> act on data e.t.c.). Now I want to switch to freertos tasks but I’m not sure how to properly implement it. Is it best to transform them al to separate tasks or just to create one big task? Maybe only the first as task and the rest as functions? What will happen to a (running) function during a context switch?


rtel wrote on Tuesday, October 25, 2016:

There is not enough information here for a proper answer, and even if
there was enough information, the answer would be complex. The first
question to ask yourself is - are the calculations related to each
other? In other words, are they part of the same piece of related
functionality? If so, and as you say they have to happen in sequence,
then the simplest thing would probably be to have them done in the same
task - but that is a general statement rather than a suggestion for your
particular case.

As long as your functions are thread safe, or if only one task calls the
functions, then the output of the calculations will be the same whether
the task is preempted while the calculation is being performed or not.

jesseb9900 wrote on Wednesday, October 26, 2016:

Thank you. Only one task is using the functions. So I think, according to the answer, the best way to implement it is to create seperate functions to do the calculations. The thing I’m not sure about, is what will happen if the task gets preempted while waiting for a return of one of the “sub”-functions. When the task continues, will it also continue the funtion? If so, does this mean for the stack? Does this mean the state of the function get saved (taste like memory problems)?

heinbali01 wrote on Wednesday, October 26, 2016:

Hi Jesse,

what will happen if the task gets preempted while waiting
for a return of one of the “sub”-functions

No problem.
When pre-emption is allowed, you task can indeed be interrupted at any moment, unless task-switching is temporarily suppressed or when all interrupts have been dis-allowed.
Before switching to a new task, the kernel will save all information: next instruction to be done (PC), current stack (SP), and all other important registers.

A new task will run for a while and after that, your “sub”-functions will become active again. It is as if it got frozen for a while and continues where it was.