Task Switching in Critical Region

Hello,   I am trying to use a IAR version of STR75x port. In which in blocktim.c file there is a task "vPrimaryBlockTimeTestTask". Inside this task there is folowing line of code (line no 115 – 129): portENTER_CRITICAL();             {                 xTimeWhenBlocking = xTaskGetTickCount();                                 /* We should unblock after xTimeToBlock having not received                 anything on the queue. */                 if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )                 {                     xErrorOccurred = pdTRUE;                 }                 /* How long were we blocked for? */                 xBlockedTime = xTaskGetTickCount() – xTimeWhenBlocking;             }             portEXIT_CRITICAL(); After doing the careful observation of these lines of code, it seems the context switching is taking place due to the xQueueReeceive() function. My queries are as follows- 1) How can context switching is allowed inside a critical region? 2) If it is allowed what we need to take care while using it? Can anybody help to understand these points? BR,

Task Switching in Critical Region

Each task maintains its own interrupt status.  This permits context switches to occur within a critical region – but only if the task is coded specifically to do this.  A preemptive context switch will never occur within a critical region. This is a very powerful mechanism, and is used by the scheduler itself to implement the scheduler locking mechanism. If a task enters a critical region, then yields, it is guaranteed that it will next start executing with interrupts disabled.  The tasks that run in between it yielding and then running again might (most probably) have interrupts enabled though. Regards.

Task Switching in Critical Region

Hi Richard,   Thanks for the clarification. I understood this through observation but had doubt as it is mentioned in freeRTOS website that "Preemptive context switches cannot occur when in a critical region." Now regarding my second query- 2)  If it is allowed what we need to take care while using it? To more specific, what about the shared variable or resources which are shared between ISR and Task. As ISR will be enabled after context switching so how to take care whether these shared variables/resources are not getting corrupted during these context switching. Thanks once again for your help. BR,