rtel wrote on Thursday, February 18, 2016:
I don’t think this can be done in a portable way very easily. To truly
restart a task (so you get the parameter passed in again, the thread
local storage was cleared, etc.) you would need an API function that:
Deletes the existing task to ensure it is not referenced from any
state lists or other objects. The easiest way of doing that would be to
Create a new task so the TCB was re-initialised, the initial stack of
the task was set back to its original state, the register values were
set back to their initial state, etc. The easiest way of doing that
would be to call xTaskCreate() again - however you would not know what
the initial value of the task parameter was.
With dynamically allocated memory this is tricky because once the task
has been deleted there is no stack, so xTaskCreate() could not be
called. However, with statically allocated memory that is not the case,
the stack memory can be borrowed between the delete and subsequent
re-creation of the task.
In fact, probably the easiest way of doing this would be to allow the
RTOS daemon task/timer service task to perform these actions, rather
than have the task do it itself. That way you could also do it with
dynamic memory allocation.
However, maybe if you really want a task to restart the simplest thing
to do would be to have the task jump back up to the start of its
implementing function at the appropriate time? You would have to
re-initialise any stack variables manually, as the function prologue
code would not execute.