# Nested Critical Region

**URL:** <https://forums.freertos.org/t/nested-critical-region/5760>\
**Category:** Kernel\
**Created:** [October 18, 2007, 6:24am UTC](https://forums.freertos.org/t/nested-critical-region/5760 "2007-10-18T06:24:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 18, 2007, 6:24am UTC](https://forums.freertos.org/t/nested-critical-region/5760/1 "2007-10-18T06:24:51Z")

</div>

**[jdliue](https://sourceforge.net/u/jdliue/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012):

Will it be a problem when a task(A) yield from within the function vTaskPrioritySet()?(also many other functions such as xQueueSend…)that have the code structure

taskENTER\_CRITICAL();  
{  
…  
taskYIELD();  
}  
taskEXIT\_CRITICAL();

and if the succeding task(B) also has a code  
taskENTER\_CRITICAL();  
{  
…  
}  
taskEXIT\_CRITICAL();

From the Source code (ARM IAR port)

void vPortEnterCritical( void ) //taskENTER\_CRITICAL()  
{  
&nbsp;&nbsp;&nbsp; \_\_disable\_interrupt();  
&nbsp;&nbsp;&nbsp; ulCriticalNesting++;  
}

void vPortExitCritical( void ) //taskEXIT\_CRITICAL()  
{  
&nbsp; if( ulCriticalNesting \> portNO\_CRITICAL\_NESTING ) //(\>0)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ulCriticalNesting–;  
&nbsp;&nbsp;&nbsp;&nbsp; if( ulCriticalNesting == portNO\_CRITICAL\_NESTING ) // == 0  
&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \_\_enable\_interrupt();  
&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp; }  
}

My question is, will the interrupt still be disabled after task B leave the critical region ? If yes then it certainly not meet our desire.  
Do i miss something ?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 18, 2007, 6:34am UTC](https://forums.freertos.org/t/nested-critical-region/5760/2 "2007-10-18T06:34:51Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012/7605):

I need to write an FAQ on this one :o)

No it is not a problem for the kernel to switch tasks from within a critical section - in fact the scheduler locking mechanism relies on the ability to do this.

Each task maintains its own interrupt state.&nbsp; Therefore task A may enter a critical section, then yield to task B.&nbsp; When task A starts executing again it will do so from within the critical section, so interrupts will again be disabled.&nbsp; Task B may or may not have interrupts enabled, it depends on the interrupt status when task B yielded, so interrupts will not necessarily be disabled between when task A stops executing and subsequently starts executing again.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 18, 2007, 6:41am UTC](https://forums.freertos.org/t/nested-critical-region/5760/3 "2007-10-18T06:41:14Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012/7605/0a4c):

\>Each task maintains its own interrupt state

and each task saves its uxCriticalNesting value as part of its context.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 18, 2007, 7:08am UTC](https://forums.freertos.org/t/nested-critical-region/5760/4 "2007-10-18T07:08:04Z")

</div>

**[jdliue](https://sourceforge.net/u/jdliue/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012/059f):

What i mean is task B never expect it will cause the interrupt disabled after leave the critical section.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 18, 2007, 7:22am UTC](https://forums.freertos.org/t/nested-critical-region/5760/5 "2007-10-18T07:22:12Z")

</div>

**[jdliue](https://sourceforge.net/u/jdliue/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012/7179):

Thanks for your explanation !

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 18, 2007, 7:28am UTC](https://forums.freertos.org/t/nested-critical-region/5760/6 "2007-10-18T07:28:03Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, October 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/fb58d94e/#8012/059f/dae2):

I don’t understand what you mean here.&nbsp; As far as TaskB is concerned it is the only task in the system.&nbsp; It does not know or care what TaskA is doing.&nbsp; It always has the interrupt state as it expects it to have.&nbsp; If its in a critical region then it expects to have interrupts diabled.&nbsp; If its out of a critical region then it expects to have interrupts enabled.&nbsp; This is always the case and is not dependent on when it or any other task yields.&nbsp; Critical sections can nest quite happily so the critical section will only be exited when the same number of calls are made to portEXIT\_CRITICAL as were already made to portENTER\_CRITICAL.
