# Quetions about the task scheduler

**URL:** <https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339>\
**Category:** Kernel\
**Created:** [August 27, 2021, 8:18am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339 "2021-08-27T08:18:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [August 27, 2021, 8:18am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/1 "2021-08-27T08:18:06Z")

</div>

Hi ,  
configUSE\_PORT\_OPTIMISED\_TASK\_SELECTION is 0 then task selection is performed in a generic way. There might be a racing condition on uxTopReadyPriority.  
Please check this failed case:

1. Tick IRQ is a low priority IRQ.

2. When Tick IRQ finished this line “UBaseType\_t uxTopPriority = uxTopReadyPriority;” , let’s assume uxTopReadyPriority and uxTopPriority is 5 now.  
{   
==\> UBaseType\_t uxTopPriority = uxTopReadyPriority;   
  
/\* Find the highest priority queue that contains ready tasks. _/   
while( listLIST\_IS\_EMPTY( &( pxReadyTasksLists[uxTopPriority] ) ) )   
{   
configASSERT( uxTopPriority );   
–uxTopPriority;   
}_   
   
_/_ listGET\_OWNER\_OF\_NEXT\_ENTRY indexes through the list, so the tasks of   
\* the same priority get an equal share of the processor time. _/   
listGET\_OWNER\_OF\_NEXT\_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[uxTopPriority] ) );   
uxTopReadyPriority = uxTopPriority;   
} /_ taskSELECT\_HIGHEST\_PRIORITY\_TASK \*/

3. A higher priority IRQ occurs, tick IRQ is switched out. The higher priority IRQ adds a ready task into the ready list and sets uxTopReadyPriority to this task’s priority, let’s assume it is 6.

4. The higher priority IRQ finished and tick IRQ contine it’s code. uxTopReadyPriority will be set to a value \<= 5.

5. The task scheduler cannot call priority 6 task on next tick IRQ. That would cause some issues.

Thanks,  
Yueming

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [August 27, 2021, 8:56am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/2 "2021-08-27T08:56:41Z")

</div>

This is exactly why the sys tick irq must run under control of the critical section.

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [August 27, 2021, 9:11am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/3 "2021-08-27T09:11:20Z")

</div>

vTaskStartScheduler doesn’t touch uxTopReadyPriority is another solution.

---

<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:** [August 27, 2021, 3:15pm UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/4 "2021-08-27T15:15:38Z")

</div>

The tick ISR protects against that, and the ISR is different for each port, so if you think there is a bug please also so say which port you are using.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 27, 2021, 9:01pm UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/5 "2021-08-27T21:01:06Z")

</div>

> [@Yueming](#):
>
> A higher priority IRQ occurs, tick IRQ is switched out. The higher priority IRQ adds a ready task into the ready list and sets uxTopReadyPriority to this task’s priority, let’s assume it is 6.

Reading above, I assume that you are talking about ports which support interrupt nesting. Taking an example of Cortex-M4 - before `vTaskSwitchContext` is called, all the interrupts which can call FreeRTOS APIs (and as a result can alter FreeRTOS lists) are masked: [https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM\_CM4F/port.c#L463](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CM4F/port.c#L463). So the scenario you mentioned is not possible. You can look at this response to understand interrupt priorities in Cortex-M: [Understanding priority levels of ISR and FreeRTOS APIs - #16 by aggarg](https://forums.freertos.org/t/understanding-priority-levels-of-isr-and-freertos-apis/10453/16)

Again, as Richard said, if you think there is a bug in a specific port, let us know the port.

Thanks.

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [August 31, 2021, 7:03am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/6 "2021-08-31T07:03:35Z")

</div>

Thanks, Gaurav, Richard.  
My product is with ARM® Cortex®-M33 core. After set configUSE\_PORT\_OPTIMISED\_TASK\_SELECTION to 1 and defined corresponding taskSELECT\_HIGHEST\_PRIORITY\_TASK(), a lot of system issues are fixed.  
My MCU vendor might implement an incorrect tick ISR, which caused an impossible bug happened. They are working on it. However, this generic way that taskSELECT\_HIGHEST\_PRIORITY\_TASK() changes uxTopReadyPriority isn’t a good idea.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [August 31, 2021, 9:05am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/7 "2021-08-31T09:05:27Z")

</div>

> [@Yueming](#):
>
> However, this generic way that taskSELECT\_HIGHEST\_PRIORITY\_TASK() changes uxTopReadyPriority isn’t a good idea.

Would you mind explaining to us why you think so?

Thanks!

---

<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:** [August 31, 2021, 2:11pm UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/8 "2021-08-31T14:11:52Z")

</div>

Do you have configASSERT() defined?

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 1, 2021, 9:45am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/9 "2021-09-01T09:45:16Z")

</div>

> [@rtel](#):
>
> configASSERT

Yes, it is defined by a MCU vendor API.

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 1, 2021, 10:01am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/10 "2021-09-01T10:01:14Z")

</div>

In my humble opinion, searching the highest priority task in the whole pxReadyTasksLists everytime only sacrifices a little bit performance. If a MCU vendor cares about the performance, they should implement their own optimised task selection.  
We cannot assume all MCU vendor can port everything in a perfect shape. Just like my case, we spent a lot of time to figure out our MCU vendor didn’t follow the FreeRTOS reference design.  
Would you like to share your ideas about this? I am long for them.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [September 1, 2021, 10:54am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/11 "2021-09-01T10:54:12Z")

</div>

Hi Yueming,

of course I don’t know about the history of the code, but I believe the issue you raise is similar to that one here:

> [@Hard fault while checking for terminated tasks in idle loop](https://forums.freertos.org/t/hard-fault-while-checking-for-terminated-tasks-in-idle-loop/13303/15):
>
> Thanks for the great debugging effort and information. I’m not at my computer just now so can’t look at the source code to get a clear understanding of the report. Are we saying the kernel code deletes a task twice, or that the application code (test code I think in this case) was deleting the same task twice? To answer your question: there is a separate variable that counts the number of tasks waiting termination when the list already contains a count to prevent the idle task from continuously…

I tend to believe that the reasoning may be similar here: uxTopReadyPriority is only ever written to in the task selector, but read from in several other places. Without having looked at the code explicitly, it seems as if keeping it as a module level static global helps avoiding overuse of the critical section at pxReadyTasksLists access time.

The other issue, of course, is that optimized task selection is about as close to the heart of the OS as one can get, so at least I would assume that whoever tackles that issue should know (or, at the very least, very thoroughly test) what he/she is doing. Programming errors at this level tend to manifest themselves fairly soon (a profiler would probaly testify that the task selection is among the most heavily used code section in FreeRTOS). If there really have been bugs in a vendor’s adaption that you had to pinpoint, it doesn’t shed a bright light on the adaptor’s quality system (my humble opinion).

Whatever, I’m curiously waiting for Richard Barry’s answer to your query as he probably is the one who coded the selection algorithm (and as far as I can tell, he codes very deliberate and meticuluous, so there probably is a good reasons why it is as it is).

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [September 1, 2021, 3:04pm UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/12 "2021-09-01T15:04:20Z")

</div>

> [@Yueming](#):
>
> After set configUSE\_PORT\_OPTIMISED\_TASK\_SELECTION to 1 and defined corresponding taskSELECT\_HIGHEST\_PRIORITY\_TASK(), a lot of system issues are fixed.

This observation is unexpected. The setting of `configUSE_PORT_OPTIMISED_TASK_SELECTION` should not impact proper system operation. It should add/remove a minor optimization, as the name implies.

The “bug” you are reporting in [`taskSELECT_HIGHEST_PRIORITY_TASK()`](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/bb02cf647da5a7d9c7a60b2ad24e308770b1a406/tasks.c#L136-L151) is not actually a bug, as explained by @rtel and @aggarg above.

So how to explain your unexpected observation? One explanation is that you might have configured interrupt priorities incorrectly. What version of FreeRTOS are you using? More recent versions of FreeRTOS catch these configuration errors via `configASSERT()`. Can you share your definition of `configASSERT()`?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 1, 2021, 10:37pm UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/13 "2021-09-01T22:37:41Z")

</div>

> [@Yueming](#):
>
> In my humble opinion, searching the highest priority task in the whole pxReadyTasksLists everytime only sacrifices a little bit performance. If a MCU vendor cares about the performance, they should implement their own optimised task selection.

`uxTopReadyPriority` records the highest priority for which a task is ready to run. It is updated whenever a task is added to ready list: [https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L223](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L223). This way the scheduler does not need to search for the all the ready lists but only the list for `uxTopReadyPriority`.  
Port optimized task selection usually maintain top ready priority as a bit in a 32-bit number and use a hardware instruction to find the topmost set bit: [https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM\_CM4F/portmacro.h#L132](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CM4F/portmacro.h#L132). It is not yet clear how setting `uxTopReadyPriority` can cause any problem.

If you are using Cortex-M33, is there a reason not to use the official port: [https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM\_CM33](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33).

Thanks.

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 2, 2021, 2:41am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/14 "2021-09-02T02:41:33Z")

</div>

Hi Jeff,  
Here is my configASSERT()

```auto
#define configASSERT(x) EFM_ASSERT(x)
#define EFM_ASSERT(expr) ((expr) ? ((void)0) : assertEFM( __FILE__ , __LINE__ ))
66 void assertEFM(const char *file, int line)
67 {
68 (void)file; /* Unused parameter */
69 (void)line; /* Unused parameter */
70  
71 while (true) {
72 }
73 }

Thanks for your help! My freertos is "V10.4.3".

```

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 2, 2021, 2:57am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/15 "2021-09-02T02:57:23Z")

</div>

> It is not yet clear how setting `uxTopReadyPriority` can cause any problem.

I suspect the MCU vendor didn’t set the correct priority for all ISRs.

> If you are using Cortex-M33, is there a reason not to use the official port: [FreeRTOS-Kernel/portable/GCC/ARM\_CM33 at main · FreeRTOS/FreeRTOS-Kernel · GitHub](https://github.com/FreeRTOS/FreeRTOS-Kernel/tree/main/portable/GCC/ARM_CM33).

I don’t know the details. I can double check with my MCU vendor.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [September 2, 2021, 3:13am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/16 "2021-09-02T03:13:12Z")

</div>

Yueming,

That definition of `assertEFM()` isn’t ideal for FreeRTOS. Better to mask interrupts prior to entering the forever loop. That way FreeRTOS can’t continue operating, and your tasks won’t attempt to continue operating. For example, use `taskDISABLE_INTERRUPTS();`.

However, even with your current definition of `assertEFM()`, you probably would have noticed hitting it. Especially if it were called from an interrupt at a priority higher than `configMAX_SYSCALL_INTERRUPT_PRIORITY`, a common error caught by `configASSERT()`.

So, back to explaining your observation that problems go away when you use port-optimized task selection. Did you say that your tick ISR is provided by the MCU vendor? And that the tick ISR doesn’t mask interrupts for the call to `xTaskIncrementTick()`? Maybe that’s still the problem. Has that been resolved yet?

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 2, 2021, 3:29am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/17 "2021-09-02T03:29:42Z")

</div>

> Did you say that your tick ISR is provided by the MCU vendor? And that the tick ISR doesn’t mask interrupts for the call to `xTaskIncrementTick()` ? Maybe that’s still the problem. Has that been resolved yet?

Yes, it is from our MCU vendor. We didn’t find this failure sympton untill MCU vendor replaced portDISABLE\_INTERRUPTS() with portSET\_INTERRUPT\_MASK\_FROM\_ISR() for the call to xTaskIncrementTick().

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [September 2, 2021, 3:39am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/18 "2021-09-02T03:39:47Z")

</div>

In that case, you might be better off with the official freertos port, as @aggarg suggested. This forum and the freertos maintainers can be more helpful that way too. Hard to tell what other mistakes the MCU vendor may have made.

---

<div class="post-metadata">

**Author:** ![Yueming](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yueming](https://forums.freertos.org/u/Yueming)\
**Post date:** [September 2, 2021, 3:47am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/19 "2021-09-02T03:47:48Z")

</div>

Hi Jeff,  
Got it! Thanks for your help!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 2, 2021, 5:31am UTC](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339/20 "2021-09-02T05:31:39Z")

</div>

> [@Yueming](#):
>
> Yes, it is from our MCU vendor. We didn’t find this failure sympton untill MCU vendor replaced portDISABLE\_INTERRUPTS() with portSET\_INTERRUPT\_MASK\_FROM\_ISR() for the call to xTaskIncrementTick().

What are the definitions of these two macros? I suspect that you are not setting the tick interrupt priority correctly. Also, I am interested in the reason of not using the official port.

Thanks.

[Next page](https://forums.freertos.org/t/quetions-about-the-task-scheduler/13339.md?page=2)
