你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】FreeRTOS列表和列表项详解

[复制链接]
STMCU小助手 发布时间:2022-3-26 14:00
01. 概述& y1 X4 B) M$ W$ X
要想看懂FreeRTOS 源码并学习其原理,有一个东西绝对跑不了,那就是FreeRTOS 的列表和列表项。列表和列表项是FreeRTOS 的一个数据结构,FreeRTOS 大量使用到了列表和列表项,它是FreeRTOS 的基石。要想深入学习并理解FreeRTOS,那么列表和列表项就必须首先掌握,否则后面根本就没法进行。
; X* q: M7 D2 W7 u9 h, d
9 g0 T" Q4 z& T$ d- u4 l* j! s列表被FreeRTOS调度器使用,用于跟踪任务,处于就绪、挂起、延时的任务,都会被挂接到各自的列表中。用户程序如果有需要,也可以使用列表。9 c' K# t5 U; r; X( \  P
) Z6 P6 u# k6 C& u
FreeRTOS列表使用指针指向列表项。一个列表(list)下面可能有很多个列表项(list item),每个列表项都有一个指针指向列表。如下图所示。
4 c: Q9 P" ~2 [) x8 F5 i
8 N, X5 R: a8 p$ p8 B( O 1K2XUWH(U$[(NNQ]HB2$@_9.png
) Z  q. n. r7 j% s4 I( c( j: B+ O8 o
4 L+ A' Z9 W. W9 ^- ^2 q: @7 T# T02. 列表& N5 }' n: @. N6 l
列表是FreeRTOS 中的一个数据结构,概念上和链表有点类似,列表被用来跟踪FreeRTOS中的任务。与列表相关的全部东西都在文件list.c 和list.h 中。在list.h 中定义了一个叫List_t 的结构体,如下:
& R& a: J( n; h0 ^  b
, _- T, |1 z# v$ v
  1. typedef struct xLIST/ y% ]. t) c; W& V
  2. {
    ) e; T2 z8 B, I0 p( o
  3.      listFIRST_LIST_INTEGRITY_CHECK_VALUE                        /*用于检测列表项数据是否完整*/
    5 R0 D% b2 n3 i: O5 d
  4.      configLIST_VOLATILE UBaseType_t uxNumberOfItems;& C- p9 X+ i4 w8 D) ?* w- j! U, f$ X8 P
  5.      ListItem_t * configLIST_VOLATILE pxIndex;                   /*用于遍历列表*/5 Q. C9 ~/ }) ~" ]( U/ n
  6.      MiniListItem_t xListEnd;                                    /*列表项*/
    6 p* J* Z$ U7 j4 @
  7.      listSECOND_LIST_INTEGRITY_CHECK_VALUE                       /*用于检测列表项数据是否完整*/
    % I7 N; L# G$ J) v/ R
  8. }List_t;
复制代码

& l+ ]. Z4 P9 a; W @SXG_R@99Y5(WI_8J~~@H4Q.png # _- F, R; B) ^: V1 j, n

4 ?  q, V+ b2 p(1) 和(5) 、这两个都是用来检查列表完整性的, 需要将宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES 设置为1,开启以后会向这两个地方分别添加一个变量xListIntegrityValue1 和xListIntegrityValue2,在初始化列表的时候会这两个变量中写入一个特殊的值,默认不开启这个功能。" k) s3 B* {+ D1 U# x( Q
(2)、uxNumberOfItems 用来记录列表中列表项的数量。
+ t0 ~3 \" [& X(3)、pxIndex 用来记录当前列表项索引号,用于遍历列表。
3 ~2 [3 T% v; d* ^: z(4)、列表中最后一个列表项,用来表示列表结束,此变量类型为MiniListItem_t,这是一个迷你列表项。
  ]4 C+ T" ~9 i1 B% Q1 x9 w& A% _, U, y
03. 列表项
& w+ u% J) T; f9 e列表项就是存放在列表中的项目,FreeRTOS 提供了两种列表项:列表项和迷你列表项。这两个都在文件list.h 中有定义。3 @! Y0 Q+ J$ q2 V6 t9 p( ]

6 d4 i  \% W: ?) K1 X7 LxLIST_ITEM列表项, y2 F; y9 v: x6 A6 j
; K& P+ v' ~' K; I+ V8 E% q  R5 o
  1. struct xLIST_ITEM
    6 l0 v, n# T) T
  2. {
    & H: k" R4 U: ?0 M7 A
  3.      listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE           /*用于检测列表项数据是否完整*/
    1 J9 K) Q; I7 X/ v4 e
  4.      configLIST_VOLATILETickType_t xItemValue;           /*列表项值*/; Z4 f7 ?; h- _+ y- h5 U5 I* s
  5.      struct xLIST_ITEM * configLIST_VOLATILE pxNext;      /*指向列表中下一个列表项*/1 ~2 b3 V) d) m* B
  6.      struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;  /*指向列表中上一个列表项*/% A& L2 \' V! Z1 ~* y; B
  7.      void * pvOwner;                                     /*指向一个任务TCB*/
    # g% Q2 P* u* q" O; w
  8.      void * configLIST_VOLATILE pvContainer;             /*指向包含该列表项的列表 */
    ! n+ `6 V2 x8 \6 E( L) i
  9.      listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE          /*用于检测列表项数据是否完整*/
    ; ?3 b3 N0 z9 T5 Z" Y% K3 V
  10. };
    7 ^4 i" t% M/ C+ R- U" V. A6 N
  11. typedef struct xLIST_ITEM ListItem_t;
复制代码

+ H3 w2 D" C6 f0 z9 V5 H9 [6 w N%7G}3){B3XZ1T4SG)FW@VP.png ) Z* k& t0 E0 f7 O+ e  H
3 q! t, p- p9 U7 d. i) G9 J' U
(1)和(7)、用法和列表一样,用来检查列表项完整性的。以后我们在学习列表项的时候不讨论这个功能!
8 q+ D. ^# ~& y2 D2 Q8 B) J(2)、xItemValue 为列表项值。3 b7 d) d6 Q+ T) e% D8 x
(3)、pxNext 指向下一个列表项。
! |6 |# u7 v. h(4)、pxPrevious 指向前一个列表项,和pxNext 配合起来实现类似双向链表的功能。
: Q. x5 d6 x  h) r/ E& V(5)、pvOwner 记录此链表项归谁拥有,通常是任务控制块。
$ U( U. [6 e4 {(6)、pvContainer 用来记录此列表项归哪个列表。注意和pvOwner 的区别,在前面讲解任务控制块TCB_t 的时候说了在TCB_t 中有两个变量xStateListItem 和xEventListItem,这两个变量的类型就是ListItem_t,也就是说这两个成员变量都是列表项。以xStateListItem 为例,当创建一个任务以后xStateListItem 的pvOwner 变量就指向这个任务的任务控制块,表示xSateListItem属于此任务。当任务就绪态以后xStateListItem 的变量pvContainer 就指向就绪列表,表明此列表项在就绪列表中。举个通俗一点的例子:小王在上二年级,他的父亲是老王。如果把小王比作列表项,那么小王的pvOwner 属性值就是老王,小王的pvContainer 属性值就是二年级。
2 E5 w6 t# _- C& Q( E' n6 w$ w4 B0 I4 B: n2 n" L5 E
xMINI_LIST_ITEM列表项
% j5 X* F" _' G) P0 k# S8 o4 k3 R1 `  g1 T) z
  1. struct xMINI_LIST_ITEM+ `& u4 F( `: n! u7 k. O# ^* R, [
  2. {' u6 ^# C( J$ j( _5 H. I9 ]" a
  3.      listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE           /*用于检测列表项数据是否完整*/
    8 e& l/ K3 s8 E
  4.      configLIST_VOLATILE TickType_t xItemValue;
    * K& b- @, m) u5 Y
  5.      struct xLIST_ITEM * configLIST_VOLATILE pxNext;/ U6 ]9 U+ X% _' I9 i0 V
  6.      struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
    ' c- w& m- L! w
  7. };" \8 U: K7 D, r' h4 V5 W5 {
  8. typedef struct xMINI_LIST_ITEM MiniListItem_t;
复制代码

0 d) P6 ]7 u( I/ P; ]2 W- I $A`B7_1A1`S(1@90`[7OHZX.png
8 R$ W. P* O7 L$ F0 N' E) J# n( [6 p
(1)、用于检查迷你列表项的完整性。) R0 Z% x- b5 r. q9 u
, b, R# k. A3 m# v
(2)、xItemValue 记录列表列表项值。
. J) h' b' u0 L  c8 g(3)、pxNext 指向下一个列表项。
3 C3 ~8 j9 i2 p(4)、pxPrevious 指向上一个列表项。
+ j  J; {& W- G6 e+ t- }0 x可以看出迷你列表项只是比列表项少了几个成员变量,迷你列表项有的成员变量列表项都有的,没感觉有什么本质区别啊?那为什么要弄个迷你列表项出来呢?那是因为有些情况下我们不需要列表项这么全的功能,可能只需要其中的某几个成员变量,如果此时用列表项的话会造成内存浪费!比如上面列表结构体List_t 中表示最后一个列表项的成员变量xListEnd 就是MiniListItem_t 类型的。
+ o7 c! Z% l" u) R3 `& C( E& @1 s! t$ S0 [9 m
04. 列表相关宏' m( S' B, R! W/ q7 u0 C
  1. /*; }# M! T9 z6 W; Y% ^" ~* r/ B
  2. * Access macro to set the owner of a list item.  The owner of a list item
    2 c- B* G& e) f5 B  I1 _( F9 V" m
  3. * is the object (usually a TCB) that contains the list item.
    3 X' f+ E' H& N  ?! y
  4. *5 ?; s1 j3 C5 M. ~7 \5 t
  5. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER' P1 Q: j1 c- v" n5 L
  6. * \ingroup LinkedList
    6 r, z9 K( t) j9 Z9 U9 {. ]
  7. */
    5 S! p, \$ D2 M1 C4 Q$ o5 M/ r
  8. #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner )    ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )
    * N  D% u+ x3 G5 x" U

  9. 7 Y; Z' n& B5 U1 _
  10. /*, t, ?' C, C0 ?: m# N7 s; S
  11. * Access macro to get the owner of a list item.  The owner of a list item9 ~" ]( Q1 w9 Q, _! @  e
  12. * is the object (usually a TCB) that contains the list item.
    * B" E: Q, ~- q: G
  13. *
    % l9 s# S7 m0 ^# L3 o
  14. * \page listGET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER9 X* o; z4 [9 f5 O/ z: o1 e
  15. * \ingroup LinkedList4 G4 H: |. p/ t2 h
  16. */
    & H/ w  u6 @+ Y+ b& j
  17. #define listGET_LIST_ITEM_OWNER( pxListItem )             ( ( pxListItem )->pvOwner )
    9 m+ `( h* t6 H  R3 ~
  18. . F/ ~8 H/ ^: h! d& [2 ?$ _4 S5 D' [
  19. /*
    6 v' u8 I1 Z8 W+ ^
  20. * Access macro to set the value of the list item.  In most cases the value is4 ^- A( g8 h/ J3 }8 t5 Z# Q) _2 d
  21. * used to sort the list in descending order.
    0 [# e9 Y! e0 G5 U9 X$ L
  22. *
    ( O) v: c4 j1 S# a) N) o3 s6 F3 p
  23. * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
    2 J) N3 ~! h6 l9 Q
  24. * \ingroup LinkedList- v% ~9 S( W/ c5 Y0 o3 X+ Q) o# E
  25. */
    ( W1 R! G3 b: z/ L4 s
  26. #define listSET_LIST_ITEM_VALUE( pxListItem, xValue )     ( ( pxListItem )->xItemValue = ( xValue ) )
    ! |* k7 V9 y4 [$ H% z8 p: e" K

  27. ' B) l( I+ K* D1 y
  28. /*
    0 B5 Q  b" P. n" C; r, U
  29. * Access macro to retrieve the value of the list item.  The value can8 e1 r3 B* I3 P
  30. * represent anything - for example the priority of a task, or the time at6 x5 `  c0 }3 |' [- ~7 S! f& ]8 b3 U6 u
  31. * which a task should be unblocked.
    6 K7 i! O1 m' B) j! Q/ \# \! {
  32. *
    ! a$ W: f0 O" ^5 W, l3 x* L
  33. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
    ; p4 ~" a) a) `' r- b
  34. * \ingroup LinkedList& V: {2 Q7 Z' p
  35. */
    ' J. V, k# w! O
  36. #define listGET_LIST_ITEM_VALUE( pxListItem )             ( ( pxListItem )->xItemValue )- _3 s& d4 d; N, K  T

  37. 3 m+ G! A; s/ j+ D7 k. b. E
  38. /*
    ( B( M& x& d) g
  39. * Access macro to retrieve the value of the list item at the head of a given; a7 j1 ?! [5 R8 m
  40. * list.$ h3 J: [! C4 V( S: t
  41. *
    & Y* ?1 r# Y" J% p+ j
  42. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
    4 F5 U: P8 g. ]! T* v
  43. * \ingroup LinkedList$ K$ T  u  d7 {- z" y( D
  44. */  w! X2 J$ i" g0 Y- `% a% O
  45. #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList )        ( ( ( pxList )->xListEnd ).pxNext->xItemValue )3 g$ ~: [- L( c0 L
  46. 2 |: ~' G$ u( `/ t8 h. K: _& K
  47. /*& K/ z: T2 s0 f8 c8 p# x3 `2 Z
  48. * Return the list item at the head of the list.
    0 N* S* |$ V, D5 o
  49. *$ K& I2 z$ y9 q) Q1 g9 |- a' U
  50. * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
    ) H+ x- p9 [, O( x
  51. * \ingroup LinkedList
    0 O0 O% \% f' V* v5 A
  52. */
    . O: t- I* S) i- q# v% A( r
  53. #define listGET_HEAD_ENTRY( pxList )                      ( ( ( pxList )->xListEnd ).pxNext )
    ! G6 i2 l1 h# Y- s
  54. & ~- B$ d2 e- n% t) |
  55. /*
      l# }& V+ a, d
  56. * Return the next list item.
    ! C$ f# E0 ~3 y, |2 l. g
  57. *% ^% m- ~- n# G0 a3 l8 t2 r
  58. * \page listGET_NEXT listGET_NEXT
    . O; R1 S% }" |' K1 e( g- p
  59. * \ingroup LinkedList
    $ U/ l, y& Y. M, u, q
  60. */! V. l+ y2 y) U3 W
  61. #define listGET_NEXT( pxListItem )                        ( ( pxListItem )->pxNext )" J  k: z7 L* m3 [1 T7 w

  62. ( g& B! S$ b# W/ e/ E
  63. /*
    1 l# K+ u# C; z- `" H( Q+ E% C
  64. * Return the list item that marks the end of the list0 X1 J9 F1 y+ @
  65. *
    / l" U: |4 j7 ~# g
  66. * \page listGET_END_MARKER listGET_END_MARKER
    $ k2 {& E; n/ {& [( k
  67. * \ingroup LinkedList( }4 r: d1 e! f# y" i: r+ ]+ B
  68. */% w; I9 ]& J3 H5 J) P
  69. #define listGET_END_MARKER( pxList )                      ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )! A; t; R% q& W' B8 l5 N# K# V' l

  70. , _. t) x' Y  T/ h* I
  71. /*- [; g- X4 u; {! H
  72. * Access macro to determine if a list contains any items.  The macro will4 r6 P% p- N! @1 K& q
  73. * only have the value true if the list is empty.' D$ T: j. x. M2 ~" u4 a
  74. *
    6 M# v1 C  c. Q: r1 @' K6 t* z% l
  75. * \page listLIST_IS_EMPTY listLIST_IS_EMPTY- F9 K) n( X& I( @2 k3 k, M1 j3 z
  76. * \ingroup LinkedList
    9 E, r1 Y" |6 I
  77. */6 ~, {, M# P% j- }2 F( d
  78. #define listLIST_IS_EMPTY( pxList )                       ( ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) ? pdTRUE : pdFALSE )
    # v5 R& B% n% v( @" d7 Q# r4 L' H
  79. ; G  G! ~4 S/ C5 }* L( D
  80. /*
    8 r5 q% W0 e* Q& Z* F
  81. * Access macro to return the number of items in the list.6 E0 {' _2 K% J/ _# z
  82. */, P/ m6 F$ ~* E: k; I6 x! t
  83. #define listCURRENT_LIST_LENGTH( pxList )                 ( ( pxList )->uxNumberOfItems )
    2 o  @3 C, L! D& D0 j
  84. 1 i* R, @6 J. d* |: T
  85. /*' S! Z  C8 ]0 [; j# C
  86. * Access function to obtain the owner of the next entry in a list.
    ! L4 c* t/ p  i) a9 Z  E! Q
  87. *2 o, J" I+ J8 r1 u
  88. * The list member pxIndex is used to walk through a list.  Calling( r+ R( b8 ]; [/ X' M
  89. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
    " D. q1 R3 N9 h" S% Q. s3 h
  90. * and returns that entry's pxOwner parameter.  Using multiple calls to this
    % r2 B9 w2 w# _8 ~% G! n
  91. * function it is therefore possible to move through every item contained in5 b3 L, t9 D2 l& t- W
  92. * a list.
    * ~" j- F+ ?/ X
  93. *' ?' X, B4 q. M. T
  94. * The pxOwner parameter of a list item is a pointer to the object that owns! E3 J5 w" X) P: l  _: j6 C
  95. * the list item.  In the scheduler this is normally a task control block.
    . n. w. i- F7 Z
  96. * The pxOwner parameter effectively creates a two way link between the list
    ; H' v% e# @8 O! n! U4 x
  97. * item and its owner.( @# Z/ o7 \5 h0 U9 r
  98. *. K+ u8 A! L) t8 G7 p* G
  99. * @param pxTCB pxTCB is set to the address of the owner of the next list item.
    & e. Q/ x+ X+ [8 J5 N
  100. * @param pxList The list from which the next item owner is to be returned., l% d" ~! [# s
  101. *
    & y5 u' |( Z9 W" j) i- I
  102. * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY9 ^& E$ f) ~( q% c
  103. * \ingroup LinkedList
    ' e# l- ]% O: N/ p, j0 e4 ?# I
  104. */3 d9 ?7 r; u9 o9 V" P6 }  f1 [7 z
  105. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )                                           \
    ) C% g9 X" e7 ?/ P: z! f+ ~
  106.     {                                                                                          \0 b4 \/ H2 y; N* e: T$ w% ^
  107.         List_t * const pxConstList = ( pxList );                                               \$ c  ]5 ]2 {; {1 q6 ~4 U
  108.         /* Increment the index to the next item and return the item, ensuring */               \! z# h! R" S: ~  s5 |8 `# k
  109.         /* we don't return the marker used at the end of the list.  */                         \/ [" @9 v; Z- U! f1 s+ |
  110.         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                           \! n1 C. `% t* G# {. z2 E1 E$ [
  111.         if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \& `- k1 n/ h- O
  112.         {                                                                                      \
    # f; I/ k0 H6 t; a& S& `
  113.             ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                       \
    1 e! C' E/ }- v, e
  114.         }                                                                                      \( a/ C/ z) s4 _: [1 a, r8 t
  115.         ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner;                                         \
    # U9 C2 ^) n& c$ \7 o9 \1 L
  116.     }9 V* P7 R9 ]5 L5 w3 ~
  117. # y) S0 a6 j9 t) g  p2 A. s! U; R
  118. 7 W* M6 `. U! k6 H. v
  119. /*8 s* z6 `( K% ?4 H" N" k( O
  120. * Access function to obtain the owner of the first entry in a list.  Lists3 N3 y6 X) w  W% I! i
  121. * are normally sorted in ascending item value order.# a- u/ d( \$ J1 `% b
  122. *
    ; c- r2 d. W3 V
  123. * This function returns the pxOwner member of the first item in the list.. V0 ^7 o" C& x, s
  124. * The pxOwner parameter of a list item is a pointer to the object that owns
    ( `6 s5 |: v+ L# e6 x
  125. * the list item.  In the scheduler this is normally a task control block.1 I# d& Q/ d: o
  126. * The pxOwner parameter effectively creates a two way link between the list% H% F1 l. |$ `# O/ U$ S% H
  127. * item and its owner.
      a3 |3 G2 L& p
  128. *. }, I+ J; k: |3 Z) [+ O
  129. * @param pxList The list from which the owner of the head item is to be, b) Y% J" o0 S+ N. Q  I
  130. * returned.
    9 v# n6 j; N  P2 d0 I$ B7 G' G
  131. *
    5 l% t8 z% K/ R! p& c7 X
  132. * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY2 x# S2 ~6 @  \1 Q3 }
  133. * \ingroup LinkedList3 ^+ B8 S; O* Q. j& B! Q1 c* n$ \9 f
  134. */$ f2 @! |" B5 m/ m4 s/ K
  135. #define listGET_OWNER_OF_HEAD_ENTRY( pxList )            ( ( &( ( pxList )->xListEnd ) )->pxNext->pvOwner )
    & ~& B" }+ h' X
  136. # |+ q0 G2 G; H' A) [
  137. /*0 U4 z/ Z  _+ m+ p7 A3 w
  138. * Check to see if a list item is within a list.  The list item maintains a
    , e) W. L7 c* W
  139. * "container" pointer that points to the list it is in.  All this macro does
    1 J# |" _, k' |) y; a$ k
  140. * is check to see if the container and the list match.
    ! X: f. v6 s- v
  141. *
    9 a; \/ {( V! h, q
  142. * @param pxList The list we want to know if the list item is within.3 Q1 Z2 w7 q2 G6 j0 W
  143. * @param pxListItem The list item we want to know if is in the list.
    " k& X$ M' |3 \
  144. * @return pdTRUE if the list item is in the list, otherwise pdFALSE.8 ~9 t+ O/ }% G: y+ R. ]
  145. */5 }# A& e. X6 Z3 F2 O
  146. #define listIS_CONTAINED_WITHIN( pxList, pxListItem )    ( ( ( pxListItem )->pxContainer == ( pxList ) ) ? ( pdTRUE ) : ( pdFALSE ) )
    . R) W; ?0 \/ P3 R' M" K
  147. $ |2 h! v+ j5 {
  148. /*& \+ J: V, w) Z- L3 H& x
  149. * Return the list a list item is contained within (referenced from).
    % q# k6 x+ O% }" m2 H0 Y1 \
  150. *
    , ^3 a7 o# [+ _  T2 \
  151. * @param pxListItem The list item being queried.
    3 F2 ]' ~) {# J# @
  152. * @return A pointer to the List_t object that references the pxListItem) ]& g6 F* a7 y# l# @
  153. */% l3 ]1 X9 ]$ ^* r
  154. #define listLIST_ITEM_CONTAINER( pxListItem )            ( ( pxListItem )->pxContainer )& ~5 s4 o9 X3 H5 o  y( M+ U% T
  155. 0 X" b" L2 r% }7 E9 ]- L
  156. /*
    6 p& U' b$ a% l  Z. L$ N, S
  157. * This provides a crude means of knowing if a list has been initialised, as
    ) J) Q- U' [$ M, O5 k# K5 p: Q/ q
  158. * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
    8 U; _2 m3 n( O
  159. * function." U( I" N: y0 P( _
  160. */% S6 S; o5 O6 D' k7 }; S9 G6 J
  161. #define listLIST_IS_INITIALISED( pxList )                ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )' C: v- U8 {# x
复制代码
7 x  \8 N+ {2 p5 F
05. 列表相关函数9 \/ o) [6 ~& T1 ?! O7 p; J
5.1 初始化列表
; {# b- U, w% z) R) O" E" X
列表结构体中包含一个列表项成员,主要用于标记列表结束。初始化列表就是把这个列表项插入到列表中。
2 \% J+ v$ S. J  Z8 C- K' }
+ Z# i0 D. w4 b# s
  1. void vListInitialise( List_t * const pxList )& E5 E7 b5 g0 N9 g9 ?+ Z  ]
  2. {
    / Q* |, j5 W8 G% I
  3.      /*列表索引指向列表项*/
    . O9 p  E+ e+ M. j$ y' z& V+ p6 b
  4.      pxList->pxIndex = ( ListItem_t * )&( pxList->xListEnd );                  
    , h1 d; v9 ~3 k# u9 \0 Z. C- J+ h
  5.      /* 设置为最大可能值 *// A: K$ y$ b/ p4 S( F) A
  6.      pxList->xListEnd.xItemValue =portMAX_DELAY;
    7 `" n% Y3 B6 t
  7. . b% E' i6 e$ L' [" ~
  8.      /* 列表项xListEnd的pxNext和pxPrevious指针指向了它自己 */( h/ `! F" v: s+ \7 e; {  v
  9.      pxList->xListEnd.pxNext = (ListItem_t * ) &( pxList->xListEnd );
    3 I6 l/ {; T$ a/ n4 g  I' q
  10.      pxList->xListEnd.pxPrevious= ( ListItem_t * ) &( pxList->xListEnd );
    $ t' g0 m. L0 o$ p3 r# b
  11.      pxList->uxNumberOfItems = ( UBaseType_t) 0U;
    - p" B9 B$ c* U. ~
  12. 9 Y6 O" W, ?9 r8 L
  13.      /* 设置为已知值,用于检测列表数据是否完整*/
    & d/ j1 L1 {% ~' m
  14.      listSET_LIST_INTEGRITY_CHECK_1_VALUE(pxList );
    ) I' I2 n9 u) p
  15.      listSET_LIST_INTEGRITY_CHECK_2_VALUE(pxList );. a* [2 f/ i1 _/ \, l
  16. }7 m/ _8 N! j! p1 z9 {# _
复制代码

+ E- E, x7 Z. Z. \' [: j如果宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES设置为1,则使能列表项数据完整性检查,则宏listSET_LIST_INTEGRITY_CHECK_1_VALUE()和listSET_LIST_INTEGRITY_CHECK_2_VALUE被一个已知值代替,默认为0x5a5a(16位架构)或者0x5a5a5a5a(32位架构)。
  P! R: G* N- T6 S/ Z  v9 i6 c! b
8 X2 F" {+ s1 @4 J PQ7TE`T~5F]2RUTXY97~6%Q.png * {# w2 G9 Z" a. K. `) A5 d

! j5 Q' w9 W2 C7 \+ E8 K5.2 初始化列表项' s0 ]1 ^: {9 b% {) q- t& S
列表项的初始比较简单,只要确保列表项不在任何列表中即可。9 z' a: c  _# \7 g/ D" k
  1. void vListInitialiseItem( ListItem_t * const pxItem )
    $ y" \+ V, A. l2 V- @; z- s) X
  2. {/ Y: Y' x6 @2 L
  3.      pxItem->pvContainer = NULL;! T+ d0 G$ i) t9 f# F5 H

  4. + k# b. y4 Q" }/ {
  5.      /*设置为已知值,用于检测列表项数据是否完整*/
    0 E* U' l$ y- E( G. _
  6.      listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );7 |9 G5 z4 K1 f% E* I: ^4 K1 l
  7.      listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );# Z7 l( \: ]1 v2 X( H! Y
  8. }
复制代码
* H+ ?, W. e$ k5 G/ r% o# _
如果宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES设置为1,则使能列表项数据完整性检查,则宏listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE和listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE会被两个已知的数值代替,默认为0x5a5a(16位架构)或者0x5a5a5a5a(32位架构)。8 i3 ^4 P0 }' l! g
1 i5 ~, W# B$ N; e+ P4 P
5.3 列表项插入函数) @1 G* w! M8 i2 W" A7 }
每个列表项对象都有一个列表项值(xItemValue),通常是一个被跟踪的任务优先级或是一个调度事件的计数器值。调用API函数vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem)可以将pxNewListItem指向的列表项插入到pxList指向的列表中,列表项在列表的位置由pxNewListItem->xItemValue决定,按照升序排列。
, f) E# j. W3 m0 @- M/ i' L2 m, O! H4 Y2 a' k4 Q& X, y
  1. void vListInsert( List_t * const pxList,
    8 C5 X! m$ d6 l" w, T) q
  2.                   ListItem_t * const pxNewListItem )
    ( t9 K. K0 \# Q. ^* D
  3. {/ z6 X5 J1 y9 x7 q9 o
  4.     ListItem_t * pxIterator;
    . _3 `3 N6 X+ ^3 Y* G- M
  5.     const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
    - J. j; q1 ?6 _% c  i% _$ ^

  6. 1 k  }+ N1 m6 L
  7.     /* 检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
    # z  X  ~3 o& ]& ]) k
  8.     /* Only effective when configASSERT() is also defined, these tests may catch
    # @: V& k+ i% n, m5 N1 D5 t2 |% G+ N- p
  9.      * the list data structures being overwritten in memory.  They will not catch
      z. a/ i0 T0 `* A9 P2 ?4 O' F
  10.      * data errors caused by incorrect configuration or use of FreeRTOS. */
    / ^7 v/ B7 `# v9 b8 c
  11.     listTEST_LIST_INTEGRITY( pxList );1 _' m( R+ t( e, q3 p, i0 ?
  12.     listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    . \, x: \2 @* @

  13. . ^: ~' W9 |2 N" I9 x+ G
  14.     /* Insert the new list item into the list, sorted in xItemValue order.$ R9 U8 b) S# o8 M6 p
  15.      *
    - k4 ]) g, b1 d* ]9 g/ O7 ?
  16.      * If the list already contains a list item with the same item value then the, w- p8 ]% S  S, a( G% ~* p7 ?
  17.      * new list item should be placed after it.  This ensures that TCBs which are
    ( Z& ~! k( i- g9 b
  18.      * stored in ready lists (all of which have the same xItemValue value) get a
    ' X2 ~, M& c- z
  19.      * share of the CPU.  However, if the xItemValue is the same as the back marker+ x8 X* k5 K. e* a' F: g
  20.      * the iteration loop below will not end.  Therefore the value is checked0 T/ X) g% X+ |% w* `0 P+ c" ^
  21.      * first, and the algorithm slightly modified if necessary. */
    : ]3 J. _9 _% X, }
  22.     /*将新的列表项插入到列表,根据xItemValue的值升序插入列表。*/
    # a4 s' v2 ]( }# N' a$ f
  23.     if( xValueOfInsertion == portMAX_DELAY )
    9 \1 Q  x5 c" o2 P  ]
  24.     {# ?* `* X" v: L- J$ s# C
  25.         pxIterator = pxList->xListEnd.pxPrevious;
    * L& F$ K7 _9 M( d
  26.     }
    " t! w* K' t0 R# s8 {& ~! ^; [3 y, z
  27.     else" W" a+ i! c2 x, N- G
  28.     {8 R: F' o" w3 z) o( g" |" H
  29.         /* *** NOTE ***********************************************************
    . o" A4 _. G) `6 l) M, {
  30.         *  If you find your application is crashing here then likely causes are
    ; C1 Q2 ?1 Z% [' L0 D3 w# T# O7 K+ `  A
  31.         *  listed below.  In addition see <a href="https://www.FreeRTOS.org/FAQHelp.html" target="_blank">https://www.FreeRTOS.org/FAQHelp.html</a> for
    + X  z; I8 m( p5 u/ t
  32.         *  more tips, and ensure configASSERT() is defined!' a1 v& {+ V8 s$ ]
  33.         *  <a href="https://www.FreeRTOS.org/a00110.html#configASSERT" target="_blank">https://www.FreeRTOS.org/a00110.html#configASSERT</a>
    ) N: o, f1 b! z( o- @
  34.         *, Z  o, Z' z. E( t/ L+ z
  35.         *   1) Stack overflow -
    8 e/ D0 v' w- s% @
  36.         *      see <a href="https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html" target="_blank">https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html</a>
    5 e# z* ~$ j$ q, n% p( j
  37.         *   2) Incorrect interrupt priority assignment, especially on Cortex-M5 B! K8 Y. b+ a; i& d3 [) k6 n5 |, n
  38.         *      parts where numerically high priority values denote low actual
    ) Q6 J! H+ p+ B6 }
  39.         *      interrupt priorities, which can seem counter intuitive.  See
    6 q# X8 I+ ]: [* ?. S/ M
  40.         *      and the definition% w; \( F; ~: N) O6 b6 D, m, U
  41.         *      of configMAX_SYSCALL_INTERRUPT_PRIORITY on" Q8 B  |: B2 R  a, ]' N
  42.         *      - h+ s" N1 ~6 ?% i4 a
  43.         *   3) Calling an API function from within a critical section or when
    ( u; c) p3 R+ P* r: U% e
  44.         *      the scheduler is suspended, or calling an API function that does9 C$ Q1 o" C8 e8 Y) t9 `7 n1 z5 j
  45.         *      not end in "FromISR" from an interrupt.5 @. J6 {" ~' A8 k( r% M3 J& n
  46.         *   4) Using a queue or semaphore before it has been initialised or
    : X" o" v# ?5 G  @; x
  47.         *      before the scheduler has been started (are interrupts firing0 @: R5 k0 j- k  U3 \" L: T8 a1 \
  48.         *      before vTaskStartScheduler() has been called?).
    ' e4 }) X8 F6 J  p$ P7 \  Q
  49.         **********************************************************************/
    + r7 m0 O9 M) p. N7 `$ L

  50. & ?5 h. V+ z* I9 k9 d: i% I
  51.         //xListEnd表示列表的末尾# k' b4 B) G! o! `3 s' m9 B8 S
  52.         for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM.  This is checked and valid. *//*lint !e440 The iterator moves to a different value, not xValueOfInsertion. */3 `# q! C, y$ O" A
  53.         {1 @3 e' D0 X6 u7 y
  54.             /* There is nothing to do here, just iterating to the wanted1 {7 ^) @, O4 a# \  I
  55.              * insertion position. */' A5 A$ W# ~# G* G
  56.         }* v( Q1 p3 p3 m4 h8 N& J6 D
  57.     }
    : p, z, t$ ]: k# O7 O
  58. 5 @& ^- M1 c. R9 K% I2 {, k
  59.     pxNewListItem->pxNext = pxIterator->pxNext;5 N" c$ }- Y  a  i# f1 Q, T* P
  60.     pxNewListItem->pxNext->pxPrevious = pxNewListItem;9 B5 Q; `4 s* n' {$ Y  U$ E
  61.     pxNewListItem->pxPrevious = pxIterator;
    # G! I3 O. Q9 {/ a; m7 W. R
  62.     pxIterator->pxNext = pxNewListItem;5 x! T$ f# W! D2 q9 O( l

  63. 3 i) \& N- V/ Z# b9 W
  64.     /* Remember which list the item is in.  This allows fast removal of the0 r( G+ m, `9 ^! S7 M& L
  65.      * item later. */9 N# ?* i  h4 p' g6 u, s
  66.     pxNewListItem->pxContainer = pxList;
      T! b4 J& x- I5 _! Q) L

  67. % q/ {* O; @; k( q  X; A1 [
  68.     ( pxList->uxNumberOfItems )++;9 M; l2 P" `9 V
  69. }
    8 J5 q5 J0 K( w' e0 _
复制代码

9 o3 z! ^1 S/ @/ Y, b* ^/ D( L根据xItemValue的值将新的列表项插入到列表。如果列表中存在与新列表项xItemValue值相同的列表项,则新插入的列表项位于它之后。如果列表项的xItemValue值等于portMAX_DELAY(列表结束标记,我们在讲列表数据结构时,说到每个列表数据结构体中都有一个列表项成员xListEnd,用于标记列表结束。xListEnd.xItemValue被初始化为一个常数,其值与硬件架构相关,为0xFFFF或者0xFFFFFFFF。这个常数在移植层定义,即宏portMAX_DELAY),则表示到达了列表结束位置。- R( C+ H0 v+ e1 ]/ Y' ~# G0 n2 z

5 N6 X; s+ J8 v8 W# v+ U  j% l5.4 列表项末尾插入
, r: w" C# a( ^/ D/ j+ M# M; T( PAPI函数vListInsertEnd()是简单的将列表项插入到列表的末端。' F% T# m' R8 }  X4 F/ g+ M
8 }* s& D: k5 ^& |& x
  1. void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )1 T8 l* d! h& t/ U7 k9 g# {
  2. {5 }- E) z: G* ^9 p+ ?$ v+ C
  3. ListItem_t* const pxIndex = pxList->pxIndex;) |7 _+ [1 V: y# Z

  4. * g+ {7 d# M$ J5 e5 M
  5.          /*检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
    ; S3 ^* A! J! r& j6 p( N
  6.          listTEST_LIST_INTEGRITY( pxList );
    & ~2 W6 r# V6 O1 H
  7.          listTEST_LIST_ITEM_INTEGRITY(pxNewListItem );! t6 D/ y" c2 D8 V6 F% ^

  8. 6 A1 s) ], `# z5 W* @
  9.          /*向列表中插入新的列表项*/" q, U% A- F, ^4 `2 k* K& o3 X3 R
  10.          pxNewListItem->pxNext = pxIndex;# @9 A. h' Y" s1 m
  11.          pxNewListItem->pxPrevious =pxIndex->pxPrevious;; c1 t3 n3 Z9 N" H" u

  12. $ d/ h# q, B% F5 e3 m
  13.          mtCOVERAGE_TEST_DELAY();* {5 }. s9 [/ e$ S3 v  V* ]

  14. - L; w% o  _: L
  15.          pxIndex->pxPrevious->pxNext =pxNewListItem;
    1 i9 ~' x: I) o' ?+ j" y# w
  16.          pxIndex->pxPrevious = pxNewListItem;
    / }6 }: @0 F2 q8 H6 x) H% H5 X) o

  17. + V9 U, K1 n7 r; g! C2 ^: L7 V
  18.          pxNewListItem->pvContainer = ( void* ) pxList;4 z6 V4 o3 c& d

  19. 6 Q, p: e3 |7 ^4 }# L
  20.          ( pxList->uxNumberOfItems )++;
    ! n, S3 n3 u4 \# F9 p
  21. }
复制代码
& Q6 d4 k' i/ @8 u7 b& z1 Y. K. v
5.5 列表项删除7 ?+ y. C3 u2 w* X
列表项的删除只是将指定的列表项从列表中删除,并不会将这个列表项的内存给释放掉。
, o$ R; D7 N! K, W. @- i5 N& g. t; s
  1. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
    , o  t8 U  y* S' k
  2. {, T8 f3 l- E5 }& }+ N0 l3 O, ?
  3. /* The list item knows which list it is in.  Obtain the list from the list8 A/ z1 S# k/ X
  4. * item. */! g1 |& I9 }: _  U& W
  5.     List_t * const pxList = pxItemToRemove->pxContainer;
    % B* j. f0 Y' K1 l$ d# l# a
  6. 1 J+ \4 R7 D4 b0 m
  7.     pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;2 C# l5 k4 b8 P/ h7 H7 ]# H
  8.     pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;+ N6 ?% `1 X# T, t' y
  9. 8 G2 A5 h/ h, P2 w2 N5 i
  10.     /* Only used during decision coverage testing. */
    # T; B9 ~9 j8 _( B' S, p
  11.     mtCOVERAGE_TEST_DELAY();
    ( e+ o1 q+ s0 j6 A6 D& L

  12. 2 ?  ^/ N7 B, |
  13.     /* Make sure the index is left pointing to a valid item. */
    ; b% q" N. Q% s1 {+ v
  14.     if( pxList->pxIndex == pxItemToRemove )
      X0 u8 `! q, y* g+ e( L" B5 r
  15.     {
    # d# c8 W$ g3 w. b9 w! i' c/ a" C
  16.         pxList->pxIndex = pxItemToRemove->pxPrevious;
    5 N" H9 B6 ]1 v
  17.     }# L0 q5 F$ r+ A& ]; o
  18.     else
    8 o. `* i8 _0 @3 Y$ s
  19.     {1 x/ \. v  r* e, `3 p$ d' ]: R6 l4 N8 j/ @
  20.         mtCOVERAGE_TEST_MARKER();
    : k" B* @7 |$ Q
  21.     }
    7 B" Q- L6 A/ q( d

  22. 3 S2 ?/ h" U- }! J5 U9 D3 H! p
  23.     pxItemToRemove->pxContainer = NULL;
    " a: z. {; o4 {4 C! N
  24.     ( pxList->uxNumberOfItems )--;* q5 ]2 {/ f  M. {- x
  25. 2 D  C+ t* P3 E" q. n" O
  26.     return pxList->uxNumberOfItems;( v( j" Z. _7 @& t& b8 A5 b
  27. }. |9 n: Q: k" I. m) }
复制代码

% Y$ c* W! ?: ^+ s" X/ f1 A5.6 列表遍历
& S/ t( Y1 q+ T/ U' J
  1. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )                                           \
    5 y4 C( [! d9 q# H5 v: K( w
  2.     {                                                                                          \
    5 ]# K+ g( l* P2 h- o
  3.         List_t * const pxConstList = ( pxList );                                               \
    : E0 d/ n- e$ N
  4.         /* Increment the index to the next item and return the item, ensuring */               \3 t, Y! K+ c' G6 [* w
  5.         /* we don't return the marker used at the end of the list.  */                         \  K; b* W5 o5 }# O
  6.         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                           \* j2 q% d5 p) R' s7 s' X# y
  7.         if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
    ( _4 J" x  j, t: h9 s7 J+ V* J
  8.         {                                                                                      \3 i# K3 j% U4 \" k# }$ Z
  9.             ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                       \
    / Z1 g8 N  L6 P% \% j" {- G
  10.         }                                                                                      \
    ) U- T/ A  E8 i# r- s
  11.         ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner;                                         \
    4 {" N# V% [4 a1 E2 r8 L5 T
  12.     }
    9 `) }0 l: A; i; e, `: I( y$ [" B

  13. 5 D& b' |6 ]' S1 j8 l8 P. [* X- a
复制代码
' \4 j. o9 }' r
3 W5 O# B/ {+ c! b; W2 n$ o
赞 收藏 评论0 发布时间:2022-3-26 14:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版