hehao9051 发表于 2015-11-13 22:46:31

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

今天看FreeRTOS的源代码,发现它的选择最高优先级就绪任务的代码,似乎调度选择时间是不确定的,代码如下:
#define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                                                                        \
        {                                                                                                                                                                                                                                        \
                /* Find the highest priority queue that contains ready tasks. */                                                                                                \
                while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )                                                                                \
                {                                                                                                                                                                                                                                \
                        configASSERT( uxTopReadyPriority );                                                                                                                                                        \
                        --uxTopReadyPriority;                                                                                                                                                                                \
                }                                                                                                                                                                                                                                \
                                                                                                                                                                                                                                                \
                /* 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[ uxTopReadyPriority ] ) );                                                \
        } /* taskSELECT_HIGHEST_PRIORITY_TASK */
例如当前uxTopReadyPriority的值为10,下个就绪的任务优先级为1,则循环10,如果下个就绪任务优先级为5,则只需要循环5次就行了。

wzd5230 发表于 2015-11-17 17:29:45

freertos不知道,之前研究过ucos的调度机制,它是查表,不管当前需要执行的任务的优先级是高还是低,任务查找的时间都是固定的。

zsmbj 发表于 2015-11-21 23:48:00

本帖最后由 zsmbj 于 2015-11-22 00:08 编辑

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

#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )

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

      #define taskSELECT_HIGHEST_PRIORITY_TASK()   \
      {                                                                      \
                /* Find the highest priority queue that contains ready tasks. */                \
                while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )            \
                {                                                                  \
                        configASSERT( uxTopReadyPriority );      \
                        --uxTopReadyPriority;                                 \
                }               \
                                 \
                /* 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[ uxTopReadyPriority ] ) );      \
      } /* taskSELECT_HIGHEST_PRIORITY_TASK */

#else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */

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

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

#endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */

zsmbj 发表于 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。若为1,则结果为0。

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

ej4tv3 发表于 2016-7-19 16:29:14

{:lol:}{:lol:}{:lol:}

EddieZhu 发表于 2018-8-3 16:24:50

感觉没加特定指令优化的话,还是比ucos, rt-thread等查找最高优先级采用的算法慢一点。
页: [1]
查看完整版本: FreeRTOS任务调度时间不确定吗?