vTaskSuspendAll, xTaskResumeAll and configUSE_PREEMPTION = 0

pronkin wrote on Thursday, October 24, 2013:

Hi everybody, for one of my project I use Cooperative Multi-threading (configUSE_PREEMPTION = 0)

And think that vTaskSuspendAll and xTaskResumeAll are useless in this case
Since xTaskResumeAll may cause YIELD and I use heap_3 Malloc scheme
#vTaskSuspendAll();
#{

pvReturn = malloc( xWantedSize );

traceMALLOC( pvReturn, xWantedSize );

#}
#xTaskResumeAll(); //It can YIELD here
It means pvPortMalloc can cause a context switch (little confusing)

Here is some bad scenario:

  1. Some work with critical data structures
  2. Decide to malloc some memory, while data structures not correct
  3. After pvPortMalloc other task can get access to wrong data
  4. End work with critical data structures and free memory

Maybe in case of configUSE_PREEMPTION = 0 vTaskSuspendAll and xTaskResumeAll should be empty?

edwards3 wrote on Thursday, October 24, 2013:

Initial thought is to keep them, but to just not yield if configUSE_PREEMPTION is 0, but second thought is you might be right to just compile them out all together.

When the RTOS is suspended interrupts can happen but context switches cant, but if you are using cooperative scheduling then context switches are not going to happen anyway.

But even when configUSE_PREEMPTION is 0 a context switch can happen if you for example send to a queue and there is a task blocked on the queue - but that is more expected behavior whereas calling resumeAll and getting a context switch isnt.

pronkin wrote on Thursday, October 24, 2013:

hm, yes vTaskSuspendAll can be used in cooperative sheduller to avoid
context switch in QueueSend or SemaphoreGive, so vTaskSuspendAll can’t be empty.
But context switch in pvPortMalloc not expected (for me)

rtel wrote on Thursday, November 07, 2013:

Take a look at the head revision in SVN - the co-operative behaviour has been changed, and as such it will probably be released as V7.6.0.

Now a context switch is only performed if the calling task is blocked. For example, if you attempt to write to a queue with a block time specified and the queue is full then the task will block and another task will start running. However, if you successfully write to the queue, and writing to the queue causes a task of higher priority to unblock, then a context switch will not occur - the original task will continue executing until it either blocks or calls taskYIELD().

Likewise when changing the priority of another task, or unsuspending another task, etc. A context switch will not occur even if the API call makes a higher priority task ready to run.

Regards.