Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated May 2025

taskENTER_CRITICAL_FROM_ISR(), taskEXIT_CRITICAL_FROM_ISR()

[RTOS Kernel Control]

1task. h
2
3UBaseType_t taskENTER_CRITICAL_FROM_ISR( void );
4void taskEXIT_CRITICAL_FROM_ISR( UBaseType_t uxSavedInterruptStatus );

Versions of taskENTER_CRITICAL() and taskEXIT_CRITICAL() that can be used in an interrupt service routine (ISR).

In an ISR critical sections are entered by calling taskENTER_CRITICAL_FROM_ISR(), and subsequently exited by calling taskEXIT_CRITICAL_FROM_ISR().

The taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() macros provide a basic critical section implementation that works by simply disabling interrupts, either globally, or up to a specific interrupt priority level.

If the FreeRTOS port being used supports interrupt nesting then calling taskENTER_CRITICAL_FROM_ISR() will disable interrupts at and below the interrupt priority set by the configMAX_SYSCALL_INTERRUPT_PRIORITY (or configMAX_API_CALL_INTERRUPT_PRIORITY) kernel configuration constant, and leave all other interrupt priorities enabled. If the FreeRTOS port being used does not support interrupt nesting then taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() will have no effect.

Calls to taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() are designed to nest, but the semantics of how the macros are used is different to the taskENTER_CRITICAL() and taskEXIT_CRITICAL() equivalents.

Critical sections must be kept very short, otherwise they will adversely affect the response times of higher priority interrupts that would otherwise nest. Every call to taskENTER_CRITICAL_FROM_ISR() must be closely paired with a call to taskEXIT_CRITICAL_FROM_ISR().

FreeRTOS API functions must not be called from within a critical section.

Parameters:

  • uxSavedInterruptStatus

    taskEXIT_CRITICAL_FROM_ISR() takes uxSavedInterruptStatus as its only parameter. The value used as the uxSavedInterruptStatus parameter must be the value returned from the matching call to taskENTER_CRITICAL_FROM_ISR().

    taskENTER_CRITICAL_FROM_ISR() does not take any parameters.

Returns:

taskENTER_CRITICAL_FROM_ISR() returns the interrupt mask state as it was before the macro was called. The value returned by taskENTER_CRITICAL_FROM_ISR() must be used as the uxSavedInterruptStatus parameter in the matching call to taskEXIT_CRITICAL_FROM_ISR().

taskEXIT_CRITICAL_FROM_ISR() does not return a value.

Example usage:

1/* A function called from an ISR. */
2void vDemoFunction( void )
3{
4UBaseType_t uxSavedInterruptStatus;
5
6 /* Enter the critical section. In this example, this function is itself called from
7 within a critical section, so entering this critical section will result in a nesting
8 depth of 2. Save the value returned by taskENTER_CRITICAL_FROM_ISR() into a local
9 stack variable so it can be passed into taskEXIT_CRITICAL_FROM_ISR(). */
10 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
11
12 /* Perform the action that is being protected by the critical section here. */
13
14 /* Exit the critical section. In this example, this function is itself called from a
15 critical section, so interrupts will have already been disabled before a value was
16 stored in uxSavedInterruptStatus, and therefore passing uxSavedInterruptStatus into
17 taskEXIT_CRITICAL_FROM_ISR() will not result in interrupts being re-enabled. */
18 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
19}
20
21/* A task that calls vDemoFunction() from within an interrupt service routine. */
22void vDemoISR( void )
23{
24UBaseType_t uxSavedInterruptStatus;
25
26 /* Call taskENTER_CRITICAL_FROM_ISR() to create a critical section, saving the
27 returned value into a local stack variable. */
28 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
29
30
31 /* Execute the code that requires the critical section here. */
32
33
34 /* Calls to taskENTER_CRITICAL_FROM_ISR() can be nested so it is safe to call a
35 function that includes its own calls to taskENTER_CRITICAL_FROM_ISR() and
36 taskEXIT_CRITICAL_FROM_ISR(). */
37 vDemoFunction();
38
39 /* The operation that required the critical section is complete so exit the
40 critical section. Assuming interrupts were enabled on entry to this ISR, the value
41 saved in uxSavedInterruptStatus will result in interrupts being re-enabled.*/
42 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
43}