Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


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 ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists