Quality RTOS & Embedded Software

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


taskENTER_CRITICAL() vs vTaskSuspendAll()

Posted by ivernot on July 9, 2008
I am writing code that will be used by several threads (a circular buffer module) so I need to protect a few lines of 'critical section' code.

My question is which method (ie taskENTER_CRITICAL() vs vTaskSuspendAll()) is 'preferred'
My 'requiremnets' are
1. minimum blocking of interrupts..
2. minimum execution time of the chosen enter/exit fuctions

The module will not be used in ISRs so does not need to be protected from interrupts - in fact, I would prefer that interrupts continue to run while I am in the critical section.

I have looked through the documentation of the FreeRTOS web site but found no mention of which method is prefered & when.

Thank you in advance.

Ivan Vernot

RE: taskENTER_CRITICAL() vs vTaskSuspendAll()

Posted by Richard on July 9, 2008
Critical sections: Sledge hammer approach that is very quick to enter and exit, but crude in implementation as it just disables interrupts. The effect of disabling interrupts depends on the port being used. Some disable interrupts globally, some mask to a certain level only. This is the best method to use the section of code being protected is very short, a few lines. Critical sections protect against other tasks accessing the protected data and against interrupts accessing the protected data.

Scheduler locking: More elegant, but only protects against other tasks accessing the protected data. Very quick to enter but can take longer to exit if there are lots of tasks move out of the blocked state while the scheduler was locked (by interrupts operating while the scheduler is locked) or if lots of tick interrupts are occur while the scheduler was locked - in either of these two cases a critical section is used in the unlock while the clean up is done so you can end up with interrupts disabled for a while anyway. Scheduler locking is best when the region requiring protection is longer, with the caveat that unlocking is complex if lots are tasks become unblocked while the scheduler is locked.


RE: taskENTER_CRITICAL() vs vTaskSuspendAll()

Posted by Vinay Verma on July 22, 2008
In addition to what richard has said:

if no protection from ISRs is required, you should use "vTaskSuspendAll()", as the taskENTER_CRITICAL() is also #defined to "vPortEnterCritical()", which only disable interrupts:

void vPortEnterCritical( void )
/* Disable interrupts first! */

/* Now interrupts are disabled ulCriticalNesting can be accessed
directly. Increment ulCriticalNesting to keep a count of how many times
portENTER_CRITICAL() has been called. */

[ 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