Nested Critical Region

Will it be a problem when a task(A) yield from within the function vTaskPrioritySet()?(also many other functions such as xQueueSend…)that have the code structure taskENTER_CRITICAL(); { … taskYIELD(); } taskEXIT_CRITICAL(); and if the succeding task(B) also has a code taskENTER_CRITICAL(); { … } taskEXIT_CRITICAL(); From the Source code (ARM IAR port) void vPortEnterCritical( void ) //taskENTER_CRITICAL() {     __disable_interrupt();     ulCriticalNesting++; } void vPortExitCritical( void ) //taskEXIT_CRITICAL() {   if( ulCriticalNesting > portNO_CRITICAL_NESTING ) //(>0)     {         ulCriticalNesting–;      if( ulCriticalNesting == portNO_CRITICAL_NESTING ) // == 0      {        __enable_interrupt();      }   } } My question is, will the interrupt still be disabled after task B leave the critical region ? If yes then it certainly not meet our desire. Do i miss something ?

Nested Critical Region

I need to write an FAQ on this one :o) No it is not a problem for the kernel to switch tasks from within a critical section – in fact the scheduler locking mechanism relies on the ability to do this. Each task maintains its own interrupt state.  Therefore task A may enter a critical section, then yield to task B.  When task A starts executing again it will do so from within the critical section, so interrupts will again be disabled.  Task B may or may not have interrupts enabled, it depends on the interrupt status when task B yielded, so interrupts will not necessarily be disabled between when task A stops executing and subsequently starts executing again. Regards.

Nested Critical Region

>Each task maintains its own interrupt state and each task saves its uxCriticalNesting value as part of its context.

Nested Critical Region

What i mean is task B never expect it will cause the interrupt disabled after leave the critical section.

Nested Critical Region

Thanks for your explanation !

Nested Critical Region

I don’t understand what you mean here.  As far as TaskB is concerned it is the only task in the system.  It does not know or care what TaskA is doing.  It always has the interrupt state as it expects it to have.  If its in a critical region then it expects to have interrupts diabled.  If its out of a critical region then it expects to have interrupts enabled.  This is always the case and is not dependent on when it or any other task yields.  Critical sections can nest quite happily so the critical section will only be exited when the same number of calls are made to portEXIT_CRITICAL as were already made to portENTER_CRITICAL.