Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Task Switching in Critical Region

Posted by vipin on July 6, 2007
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):

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;

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?

RE: Task Switching in Critical Region

Posted by Richard on July 6, 2007
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.


RE: Task Switching in Critical Region

Posted by vipin on July 6, 2007
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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner