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




Loading

portBASE_TYPE & critical section

Posted by c77er on February 19, 2010
I was comparing the latest FreeROTS version (6.0.2) and an earlier one (version 4.4.0). Here's what puzzles me:

In file tasks.c, function vTaskSuspendAll(), the statement "++uxSchedulerSuspended;" was protected by a critical section in version 4.4.0. While in 6.0.2, it is not, and the comment in 6.0.2 gave the rationale: "A critical section is not required as the variable is of type portBASE_TYPE."

I'm confused - even if the variable is a base type, the "++" essentially is still a load-increment-store. If the current process got interrupted right between "load" and "increment", and preempted by another process which also calls this same function, the race condition is still there and would cause an error.

Am I missing something here?

Thanks in advance.

RE: portBASE_TYPE & critical section

Posted by Ricky on February 19, 2010
The key is when the variable gets written. If it's written in one operation then all is well.

Consider the following scenario:

Task 1 calls vTaskSuspendAll and grabs uxSchedulerSuspended and increments it but doesn't get a chance to store it because an interrupt occurs. Task 2 gains control and calls the same function which halts the scheduler. When task 2 finishes, uxSchedulerSuspended gets decremented back to its original value. Task 1 regains control and stores the incremented value to suspend the scheduler. When Task 1 finishes, uxSchedulerSuspended gets decremented and the scheduler starts again.

If it took two writes to store uxSchedulerSuspended, then there would be a danger since the first write would corrupt the value until the second write was done. An interrupt between the writes would cause any task using that variable to use a corrupted value.

RE: portBASE_TYPE & critical section

Posted by Richard Damon on February 20, 2010
Another way to take a look at this, and it is based on the essential nature of the variable, is to look at what happens with different values of the variable.

If it is non-zero at the start, then if an interrupt comes between the read and the write, so no problems can occur.

If it is zero at the start, then an interrupt might cause a task switch, but if it does, when it get back to this task, the variable will be zero again.

The key feature is there is only a single value of this variable that allows a task change, so there is no danger of it change behind the operations back.


[ 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