Preventing context - switch

cnimo wrote on Thursday, May 02, 2019:


I am trying to analyze the stack usage of some functions in my code,
and in order to do that I want to prevent context - switching during those functions.
I am unable to use ‘vTaskSuspendAll’ or ‘taskENTER_CRITICAL’ .
Willsimply assigning the task that runs those functions with the highest priority prevent context switching?
If not, is there any other way to prevent context switching?


richarddamon wrote on Thursday, May 02, 2019:

If the task is the only task at the highest priority, then there can never be another task that becomes the highest priority ready task, so the only way for a context switch to happen would be for the task to block, when a context switch basically MUST happen.

Not sure why you couldn’t use vTaskSuspendAll() for this case, as it basically enforces the same sort of condition.

Also, since context switching changes the stack to the new task, unless you are using some stack checking hardware, a context switch generally won’t cause much issue with the tasks stack usage, at most it adds the tasks context to the stack.