搜索
bottom↓
回复: 5

FreeRTOS任务调度时间不确定吗?

[复制链接]

出0入0汤圆

发表于 2015-11-13 22:46:31 | 显示全部楼层 |阅读模式
今天看FreeRTOS的源代码,发现它的选择最高优先级就绪任务的代码,似乎调度选择时间是不确定的,代码如下:
  1. #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                                                                        \
  2.         {                                                                                                                                                                                                                                        \
  3.                 /* Find the highest priority queue that contains ready tasks. */                                                                                                \
  4.                 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )                                                                                \
  5.                 {                                                                                                                                                                                                                                \
  6.                         configASSERT( uxTopReadyPriority );                                                                                                                                                        \
  7.                         --uxTopReadyPriority;                                                                                                                                                                                \
  8.                 }                                                                                                                                                                                                                                \
  9.                                                                                                                                                                                                                                                 \
  10.                 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of                                                                                \
  11.                 the        same priority get an equal share of the processor time. */                                                                                                        \
  12.                 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );                                                \
  13.         } /* taskSELECT_HIGHEST_PRIORITY_TASK */
复制代码

例如当前uxTopReadyPriority的值为10,下个就绪的任务优先级为1,则循环10,如果下个就绪任务优先级为5,则只需要循环5次就行了。

出0入0汤圆

发表于 2015-11-17 17:29:45 | 显示全部楼层
freertos不知道,之前研究过ucos的调度机制,它是查表,不管当前需要执行的任务的优先级是高还是低,任务查找的时间都是固定的。

出0入0汤圆

发表于 2015-11-21 23:48:00 | 显示全部楼层
本帖最后由 zsmbj 于 2015-11-22 00:08 编辑

不知你看的是那个版本。根据configUSE_PORT_OPTIMISED_TASK_SELECTION的定义,Freertos有2种方法。在V8.2.3里边:

  1. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )

  2.         /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  3.         performed in a generic way that is not optimised to any particular
  4.         microcontroller architecture. */

  5.         #define taskSELECT_HIGHEST_PRIORITY_TASK()   \
  6.         {                                                                      \
  7.                 /* Find the highest priority queue that contains ready tasks. */                \
  8.                 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )            \
  9.                 {                                                                    \
  10.                         configASSERT( uxTopReadyPriority );      \
  11.                         --uxTopReadyPriority;                                 \
  12.                 }               \
  13.                                  \
  14.                 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of    \
  15.                 the        same priority get an equal share of the processor time. */                                              \
  16.                 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );        \
  17.         } /* taskSELECT_HIGHEST_PRIORITY_TASK */

  18. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */

  19.         /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  20.         performed in a way that is tailored to the particular microcontroller
  21.         architecture being used. */

  22.         #define taskSELECT_HIGHEST_PRIORITY_TASK()           \
  23.         {                                \
  24.         UBaseType_t uxTopPriority;                 \
  25.                                                                 \
  26.                 /* Find the highest priority queue that contains ready tasks. */     \
  27.                 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                        \
  28.                 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );              \
  29.                 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );              \
  30.         } /* taskSELECT_HIGHEST_PRIORITY_TASK() */

  31. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
复制代码

出0入0汤圆

发表于 2015-11-22 00:07:03 | 显示全部楼层
查了一下:portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); 根据不同处理器不同,在M3内核,这个定义如下

#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31 - __CLZ( ( uxReadyPriorities ) ) )

__CLZ是前导零计数指令 功能:CLZ(Count Leading Zeros)指令对Rm中值的高位(leading zeros)个数进行计数,结果放到Rd中。若源寄存器全为0,则结果为32。若[31]为1,则结果为0。

这样一步就可以计算出最高优先级来。比那个查表法要快很多。当然这个指令需要处理器支持。

出0入0汤圆

发表于 2016-7-19 16:29:14 | 显示全部楼层

出0入0汤圆

发表于 2018-8-3 16:24:50 | 显示全部楼层
感觉没加特定指令优化的话,还是比ucos, rt-thread等查找最高优先级采用的算法慢一点。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 07:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表