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

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

[复制链接]
STMCU小助手 发布时间:2022-6-18 17:00
01. 概述9 n# j3 N/ ^- m& ]
要想看懂FreeRTOS 源码并学习其原理,有一个东西绝对跑不了,那就是FreeRTOS 的列表和列表项。列表和列表项是FreeRTOS 的一个数据结构,FreeRTOS 大量使用到了列表和列表项,它是FreeRTOS 的基石。要想深入学习并理解FreeRTOS,那么列表和列表项就必须首先掌握,否则后面根本就没法进行。  t0 D4 M: W6 L; {0 l! l3 d
6 R% [# t4 h, W7 g: b$ G, _* g0 d
列表被FreeRTOS调度器使用,用于跟踪任务,处于就绪、挂起、延时的任务,都会被挂接到各自的列表中。用户程序如果有需要,也可以使用列表。
: Y: d# c( @" f0 C- F, ~1 H  H. ?6 P" s: ?) X# |) J
FreeRTOS列表使用指针指向列表项。一个列表(list)下面可能有很多个列表项(list item),每个列表项都有一个指针指向列表。如下图所示。
: x) [4 ^1 s* G3 s8 c7 e
3 L$ M" E* k% n7 Q% q: t3 F) z3 k I2O2RI0XDTX)A[0Q6EU]KRE.png
. |$ I- P9 a. z2 Z8 q; m& @. o( h' e
02. 列表
7 s( D: P' x6 D! U# {4 m+ n2 Z列表是FreeRTOS 中的一个数据结构,概念上和链表有点类似,列表被用来跟踪FreeRTOS中的任务。与列表相关的全部东西都在文件list.c 和list.h 中。在list.h 中定义了一个叫List_t 的结构体,如下:# _& f) i9 V' t' ^

2 K/ h) Q& I& [7 J0 r1 |; d
  1. typedef struct xLIST' P! y+ e/ ]: V( J# O3 I
  2. {
    , ~6 m( ^" p' S6 K6 X7 Y# r% T* N
  3.      listFIRST_LIST_INTEGRITY_CHECK_VALUE                        /*用于检测列表项数据是否完整*/7 u: {4 g- ?/ S
  4.      configLIST_VOLATILE UBaseType_t uxNumberOfItems;
    : f9 R1 I: x9 Z7 F; T* o3 n
  5.      ListItem_t * configLIST_VOLATILE pxIndex;                   /*用于遍历列表*/
    $ a: Y  f3 h& [5 i5 _; a
  6.      MiniListItem_t xListEnd;                                    /*列表项*/
    * v9 Z4 q' x5 e' Q  G' Q0 K2 G' \
  7.      listSECOND_LIST_INTEGRITY_CHECK_VALUE                       /*用于检测列表项数据是否完整*/* q0 _' N6 V' D9 [
  8. }List_t;
复制代码

  b$ M/ @3 K, x- [+ u- u
' @+ U9 h3 q" G1 \& t" ^# {, y
; |2 {3 J" ?5 y1 Y(1) 和(5) 、这两个都是用来检查列表完整性的, 需要将宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES 设置为1,开启以后会向这两个地方分别添加一个变量xListIntegrityValue1 和xListIntegrityValue2,在初始化列表的时候会这两个变量中写入一个特殊的值,默认不开启这个功能。, K* R4 q" R9 y# t
(2)、uxNumberOfItems 用来记录列表中列表项的数量。1 S% q1 U1 o- d3 V: |
(3)、pxIndex 用来记录当前列表项索引号,用于遍历列表。! K$ Z2 K3 S, f& H; K9 S! c6 z3 U
(4)、列表中最后一个列表项,用来表示列表结束,此变量类型为MiniListItem_t,这是一个迷你列表项。- ?& J6 i5 o, ^( v7 `5 ?4 L

  E: K8 x! T" t$ h- n, D03. 列表项
, v# l* ]7 L" a% b( V
列表项就是存放在列表中的项目,FreeRTOS 提供了两种列表项:列表项和迷你列表项。这两个都在文件list.h 中有定义。
  R0 ~+ W  ?+ U9 F4 ^3 `$ M; }. ]. I6 F' u
xLIST_ITEM列表项/ N; i, }* M; Y

1 _; c' Z- m( P: x& a
  1. struct xLIST_ITEM
    5 p4 w; ]* a0 {0 n
  2. {( A) ~1 f* K$ a
  3.      listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE           /*用于检测列表项数据是否完整*/! |& X/ B7 N) m, A" `# F8 }: K
  4.      configLIST_VOLATILETickType_t xItemValue;           /*列表项值*/
    # o3 T2 S, \( @9 P
  5.      struct xLIST_ITEM * configLIST_VOLATILE pxNext;      /*指向列表中下一个列表项*/
    3 o0 c9 w: M# Q* u: E
  6.      struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;  /*指向列表中上一个列表项*/- c  _. K4 W& ~0 h
  7.      void * pvOwner;                                     /*指向一个任务TCB*/+ a- ~1 C0 F! G
  8.      void * configLIST_VOLATILE pvContainer;             /*指向包含该列表项的列表 */
    % Z( B* g# G6 D7 j3 |
  9.      listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE          /*用于检测列表项数据是否完整*/% \4 ]" J0 {* v7 C" l. C( r( N& d
  10. };4 S% j) O$ f2 I1 K. E- }1 ^  d
  11. typedef struct xLIST_ITEM ListItem_t;
复制代码

% y. K6 R' @4 Z% `, H# a 29{HXM74_0D3OH75ZXAV50N.png ) V+ a1 V! P- n$ E: u5 \& T
* p: s2 c2 {/ v* [
(1)和(7)、用法和列表一样,用来检查列表项完整性的。以后我们在学习列表项的时候不讨论这个功能!
9 L" }- K0 F) R3 d" T3 k- w(2)、xItemValue 为列表项值。
0 F! g* p" M- L6 r6 J(3)、pxNext 指向下一个列表项。3 D/ e* N5 @2 a2 `) J6 I
(4)、pxPrevious 指向前一个列表项,和pxNext 配合起来实现类似双向链表的功能。5 }; `  n3 M0 H7 {
(5)、pvOwner 记录此链表项归谁拥有,通常是任务控制块。
' l( [% M- U% g$ a9 D(6)、pvContainer 用来记录此列表项归哪个列表。注意和pvOwner 的区别,在前面讲解任务控制块TCB_t 的时候说了在TCB_t 中有两个变量xStateListItem 和xEventListItem,这两个变量的类型就是ListItem_t,也就是说这两个成员变量都是列表项。以xStateListItem 为例,当创建一个任务以后xStateListItem 的pvOwner 变量就指向这个任务的任务控制块,表示xSateListItem属于此任务。当任务就绪态以后xStateListItem 的变量pvContainer 就指向就绪列表,表明此列表项在就绪列表中。举个通俗一点的例子:小王在上二年级,他的父亲是老王。如果把小王比作列表项,那么小王的pvOwner 属性值就是老王,小王的pvContainer 属性值就是二年级。
& j1 M5 h4 I1 L1 L* z; `2 X' z, W7 O9 A6 G: ~0 k6 f
xMINI_LIST_ITEM列表项; q1 e7 n- m" [! Q
( R# O6 W5 K( _- J) e3 U% ]( A
  1. struct xMINI_LIST_ITEM, u0 ]6 N$ k  b* g  y6 }
  2. {
    ' c+ D3 P5 U6 k
  3.      listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE           /*用于检测列表项数据是否完整*/5 A5 `6 Z: {% D$ M  m5 K
  4.      configLIST_VOLATILE TickType_t xItemValue;
    1 R, H; F' f7 a
  5.      struct xLIST_ITEM * configLIST_VOLATILE pxNext;
    5 h1 \( [: T$ H6 V3 ?: L
  6.      struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;( u3 F& q9 R7 u
  7. };, c$ t) [, t. Y3 Q, _# c( A+ T# }
  8. typedef struct xMINI_LIST_ITEM MiniListItem_t;
复制代码
# ]- k) X8 y2 K: `# V
ME~S{{H3IK71Q$H9_3D26{9.png : Z: e6 Z6 L- ~# \; G$ e

1 Q# z* [! t- M: K: }) v! C% B(1)、用于检查迷你列表项的完整性。, c( y9 J" \' K1 _1 c+ T

/ p$ N; @, [  [: i, U- I  R+ ]" n(2)、xItemValue 记录列表列表项值。
& p# i. A: f1 k+ j( t% I(3)、pxNext 指向下一个列表项。$ e/ v' S6 z. V9 `
(4)、pxPrevious 指向上一个列表项。0 Z. P4 [8 M. x$ x" r
可以看出迷你列表项只是比列表项少了几个成员变量,迷你列表项有的成员变量列表项都有的,没感觉有什么本质区别啊?那为什么要弄个迷你列表项出来呢?那是因为有些情况下我们不需要列表项这么全的功能,可能只需要其中的某几个成员变量,如果此时用列表项的话会造成内存浪费!比如上面列表结构体List_t 中表示最后一个列表项的成员变量xListEnd 就是MiniListItem_t 类型的。
1 a+ U6 I' U9 u# E7 r8 U. k0 d' ^% ?. ?: P2 T5 z( y
04. 列表相关宏

; e4 B8 O& c  T
  1. /*3 N* s2 R8 P& V0 J
  2. * Access macro to set the owner of a list item.  The owner of a list item# l) a! q+ p% E7 ^5 R$ `
  3. * is the object (usually a TCB) that contains the list item.5 r, h* G1 }, h* A8 X* d
  4. *, x) |1 j) p$ X  x
  5. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER! W/ u+ E5 }0 M; S( U
  6. * \ingroup LinkedList
    ' i- C6 O4 R$ m5 N8 W' s
  7. */; o5 |' h. C$ N
  8. #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner )    ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )& v% |9 Q6 x* I  i. Z3 x9 j

  9. ; I4 [! R6 O; V. k
  10. /*
    4 @# h6 ]# O1 k& [7 Q8 ^+ C( _' D
  11. * Access macro to get the owner of a list item.  The owner of a list item7 O# C: d! M4 y# v1 ~7 U- Q
  12. * is the object (usually a TCB) that contains the list item.
    ; D% c" S  L  g
  13. *
    / G3 c# X1 B9 T2 z2 `
  14. * \page listGET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
    & ]& x/ w0 {5 k
  15. * \ingroup LinkedList
    " b+ Y% k1 l* f. m4 X
  16. */$ \0 @" I1 a6 l7 _8 q' m# s6 ?3 B
  17. #define listGET_LIST_ITEM_OWNER( pxListItem )             ( ( pxListItem )->pvOwner )* F0 z8 g" D0 e' X

  18. - M1 t5 D( J+ ?8 `' V( r" T- s
  19. /*& J6 Y7 \6 t, X% C% R9 P- Q
  20. * Access macro to set the value of the list item.  In most cases the value is. r% I: U0 G( h7 J# B% @2 q
  21. * used to sort the list in descending order.; z0 K1 `- _, o9 a: l3 e5 E/ T' E
  22. *$ S) E1 \, v( |0 C
  23. * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
    # ^/ n$ d3 Y( U) M% S5 _
  24. * \ingroup LinkedList
    9 `. q7 M1 \9 S8 ^
  25. */4 H6 q: Q5 w, \% P* L9 q
  26. #define listSET_LIST_ITEM_VALUE( pxListItem, xValue )     ( ( pxListItem )->xItemValue = ( xValue ) )+ ^0 L. }0 {- M8 W4 s8 O
  27. $ o  G4 \. E$ q$ ?; {- _
  28. /*7 |9 I: \* S/ H/ S% F
  29. * Access macro to retrieve the value of the list item.  The value can; I4 {% s, Z: Q  @1 W* U: k% X- t1 g
  30. * represent anything - for example the priority of a task, or the time at& |1 M+ I/ i2 }* s
  31. * which a task should be unblocked.
      i. ^6 ~# z/ [. d( q; _
  32. *
    ' O; Q+ i9 U  S2 p, u
  33. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
    ) r0 n4 G* ]* L: \9 r! e
  34. * \ingroup LinkedList
    - F  m/ l4 V' K. R% k
  35. */% O+ t1 F. n  Y" g
  36. #define listGET_LIST_ITEM_VALUE( pxListItem )             ( ( pxListItem )->xItemValue )0 [/ V, l0 U" {
  37. : i; g! H% `" {/ F6 V
  38. /*
    - V! Y: `0 g, M) i; m3 \" R1 h
  39. * Access macro to retrieve the value of the list item at the head of a given
    # V$ H9 u+ U8 n+ O: r" m
  40. * list.
    + Q" w  s8 M3 E, ^. T
  41. *# O- A! e2 f4 m4 \
  42. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE- _5 V! R5 p( C# \1 c
  43. * \ingroup LinkedList2 ]2 {2 _' }- w* \/ R8 ^, z3 V
  44. */2 _! x4 g4 E$ l% b1 G4 I0 I
  45. #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList )        ( ( ( pxList )->xListEnd ).pxNext->xItemValue )- C: N$ ~2 \1 ]1 L6 |. f" Q6 x0 [
  46. 3 g3 R* n# T0 a7 p/ r$ @- d" r
  47. /*+ x3 g- ?$ p2 \$ Z+ b. ?
  48. * Return the list item at the head of the list.2 g& b( v! T# U! I& r0 j" G# r9 l
  49. *: D: T( l0 P" y, l$ F/ V% @
  50. * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
    7 l( ^3 |. ~) |. n0 t' s3 t
  51. * \ingroup LinkedList
    : q; \& `* C. \/ y* _
  52. */5 p0 M! J* S3 O
  53. #define listGET_HEAD_ENTRY( pxList )                      ( ( ( pxList )->xListEnd ).pxNext )
    ! h' M7 H& [: I; b% O
  54. / Z8 n  }: z7 j4 C2 {
  55. /*
    " @( L) Z- ~! s7 j
  56. * Return the next list item." u7 W$ \3 _" d9 `, w% k
  57. *
    ; W) \% _  @  Y; y
  58. * \page listGET_NEXT listGET_NEXT" j1 M+ j2 c0 t: x0 L; s
  59. * \ingroup LinkedList! Q% e: v$ Q9 ]  d( t
  60. */" V, r  O' F* R5 o+ ^
  61. #define listGET_NEXT( pxListItem )                        ( ( pxListItem )->pxNext )
    9 T7 _" n, z6 N3 Y, R
  62. # c. c6 B6 U/ I* A7 g
  63. /*/ L: s* K& F8 t1 G* M. [1 V6 g. ?
  64. * Return the list item that marks the end of the list
    + [& J: Z/ z- j6 u* X1 n6 B$ \+ G; m
  65. *5 h  U4 B( H4 T+ {$ F
  66. * \page listGET_END_MARKER listGET_END_MARKER
    ' E& |  Z/ p% }, [
  67. * \ingroup LinkedList
    1 }" ?  W+ l. B% E# m! }2 U6 [
  68. */
    : a1 z& C- n* y8 H
  69. #define listGET_END_MARKER( pxList )                      ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )
    , u( \( r# ]& ?# S3 _) ~8 X
  70. ( r3 l& t& |1 G9 D* x- B
  71. /*
    5 \; S. X9 U: O
  72. * Access macro to determine if a list contains any items.  The macro will
    8 W) J; P( w1 T$ t9 o' g
  73. * only have the value true if the list is empty.; t: b# `) X2 y
  74. *
    8 V$ A7 K  s5 p! Z. P/ e* |# n. b
  75. * \page listLIST_IS_EMPTY listLIST_IS_EMPTY4 L$ U( m& P9 c  t
  76. * \ingroup LinkedList8 W0 w' K( K6 P' M# ]+ @# e5 O8 S
  77. */
    1 p# h% U# g# o- M. h8 z
  78. #define listLIST_IS_EMPTY( pxList )                       ( ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) ? pdTRUE : pdFALSE )
    : h0 V5 m5 c+ u- k

  79. % |( y* `( v- n4 R. M9 X: ?6 n7 n
  80. /*
    5 W5 T! G: A0 d9 B. Z
  81. * Access macro to return the number of items in the list./ F; y- m7 @5 _  W$ P# _
  82. */
    $ G$ f, t  Y3 ~
  83. #define listCURRENT_LIST_LENGTH( pxList )                 ( ( pxList )->uxNumberOfItems )1 T, C6 m) F0 D/ G6 l

  84. ' \8 R$ p# V6 O. N
  85. /*
    $ J1 @4 }/ M4 d2 C7 z
  86. * Access function to obtain the owner of the next entry in a list.2 [- D& S( C+ @
  87. *
    # c) G6 c- `% B7 O
  88. * The list member pxIndex is used to walk through a list.  Calling
      h. l% @9 o7 U; r1 @: E+ r
  89. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
    , ^" [9 @1 B1 [5 G( J
  90. * and returns that entry's pxOwner parameter.  Using multiple calls to this  I  l" H7 i  |9 h9 K0 y
  91. * function it is therefore possible to move through every item contained in) N" t1 ]: Z7 p( ?* @+ A& ^
  92. * a list.
    * n' |$ M2 B, r& @( K% a4 e2 z
  93. */ j& c0 [: P* [9 a- l- B
  94. * The pxOwner parameter of a list item is a pointer to the object that owns
    0 q: _' m# F$ J, ~. W% N
  95. * the list item.  In the scheduler this is normally a task control block.# N$ S/ U# J! I) U" h
  96. * The pxOwner parameter effectively creates a two way link between the list0 [% P0 Y% d) N7 Z! C
  97. * item and its owner.8 f6 W. P3 I8 N( c% ~4 f3 U
  98. *5 ~& E; A$ d: W3 I
  99. * @param pxTCB pxTCB is set to the address of the owner of the next list item.
    & H. q5 e& Z' G* j6 D
  100. * @param pxList The list from which the next item owner is to be returned.
    % O; x1 }, S: i) O5 j
  101. *
    4 s9 X$ S: y( R- D4 W
  102. * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY& G5 J8 P& O/ P$ o# p
  103. * \ingroup LinkedList( k6 P1 F( B. L0 Z
  104. */0 [9 y. [! m% W+ o! N7 U3 z0 }
  105. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )                                           \4 ^5 @2 A# g8 [" Z$ ~' t
  106.     {                                                                                          \1 T5 v: t* T/ h7 d3 G( Y- a
  107.         List_t * const pxConstList = ( pxList );                                               \
    & y2 L, u7 a% z
  108.         /* Increment the index to the next item and return the item, ensuring */               \
    4 k& V% ~2 n0 T9 R5 ^
  109.         /* we don't return the marker used at the end of the list.  */                         \6 J/ ?5 U% N  \. f- g5 `2 c$ p
  110.         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                           \& E0 T) [' I9 R# k8 p7 {* M* E* Q
  111.         if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
    / a1 R7 P4 n( }( s1 ?/ R
  112.         {                                                                                      \: d: P, s$ [& l7 u' ?$ {$ h6 S( K/ u
  113.             ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                       \
    , s, Q" w: ]% ]$ R* J
  114.         }                                                                                      \
    7 L0 i" w0 _) z& B  k
  115.         ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner;                                         \
    6 T. A; c2 ~, g! J: n" u
  116.     }
    1 T: k+ _* ^' H( {/ p  t
  117. 3 L4 F( F% d& K1 y1 B# J9 m

  118. 0 y4 d+ {# T% J7 L$ |
  119. /*
    * @- [( Y9 ?  @$ A
  120. * Access function to obtain the owner of the first entry in a list.  Lists- V: t1 \. o6 i' [3 E) o) v# ~
  121. * are normally sorted in ascending item value order.+ {' J8 W7 c& e: ]. H! P  ^
  122. *
    1 s! L  A1 j0 O- y9 _2 ]
  123. * This function returns the pxOwner member of the first item in the list.- L8 ~( i5 K+ t& x+ F
  124. * The pxOwner parameter of a list item is a pointer to the object that owns5 C$ V# i  A8 k& h6 r
  125. * the list item.  In the scheduler this is normally a task control block.5 a7 ^4 n% v9 H+ q2 N4 C8 e' Q
  126. * The pxOwner parameter effectively creates a two way link between the list/ }5 @: j  l8 k# G! u5 O
  127. * item and its owner.
    9 q) Q+ f& R5 c1 ]  n% r
  128. *
    : y' W* v; g" C# e
  129. * @param pxList The list from which the owner of the head item is to be# q1 e' Y0 O: \+ X5 R! O9 X0 ?: O( ]
  130. * returned.: e; N6 h* E* i! f- c/ O
  131. *
    ' E; l( d2 `" i+ p7 o1 w' F& v
  132. * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY3 @6 Z7 E+ T: y
  133. * \ingroup LinkedList: G; }1 @! u: |1 @+ n
  134. */
    ! |- ?- d/ k" U! ~& g6 l# T- y
  135. #define listGET_OWNER_OF_HEAD_ENTRY( pxList )            ( ( &( ( pxList )->xListEnd ) )->pxNext->pvOwner ); c  v$ s/ I: a5 P: R
  136. 1 y: X8 x1 `% C. v
  137. /*  A& `, L7 s+ l5 q7 r4 l
  138. * Check to see if a list item is within a list.  The list item maintains a
    4 V8 y6 K" g5 P) ?! e6 M
  139. * "container" pointer that points to the list it is in.  All this macro does5 O& G2 i% G: y. f+ T+ a( Q. n
  140. * is check to see if the container and the list match.; r7 ^# U- _# b& g
  141. *
    3 Z. Y) o, O1 y! D
  142. * @param pxList The list we want to know if the list item is within.
    ; ~4 ]4 [  K( k% Y
  143. * @param pxListItem The list item we want to know if is in the list.
    ; Q. I3 t# [" U% y
  144. * @return pdTRUE if the list item is in the list, otherwise pdFALSE.
    / R* k5 h: d8 x% t
  145. */
    ! H9 ^0 D8 r/ E: u5 }4 i
  146. #define listIS_CONTAINED_WITHIN( pxList, pxListItem )    ( ( ( pxListItem )->pxContainer == ( pxList ) ) ? ( pdTRUE ) : ( pdFALSE ) )3 r& o; I* ]$ r
  147. 5 }7 Y0 z! A. q+ M: l
  148. /*
    & H; P5 N/ |, K
  149. * Return the list a list item is contained within (referenced from).
    5 u9 Q0 l- s( ]4 |! [, i- Z
  150. *
      [( k9 c% `9 [% @5 f9 u8 E
  151. * @param pxListItem The list item being queried.8 E( P+ W+ Z# `* j4 r0 b
  152. * @return A pointer to the List_t object that references the pxListItem( r5 S9 s6 A/ M7 [  t5 Y
  153. */
    # B6 m  ~# ~/ I4 h
  154. #define listLIST_ITEM_CONTAINER( pxListItem )            ( ( pxListItem )->pxContainer )
    + X. o' b) n7 n1 D0 ]: U8 y: m
  155. . E4 _. n7 {% S5 f
  156. /*. u2 P1 m6 W1 N8 l( L1 E. A
  157. * This provides a crude means of knowing if a list has been initialised, as
    6 A7 M. M/ ]  K# N- G
  158. * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()6 W% M5 y; o3 F; S3 t* S
  159. * function.' e: n9 f7 ?! p" o: Z6 j9 [" |1 n1 U
  160. */
    # ?3 I) g" c5 I7 \; N; ^
  161. #define listLIST_IS_INITIALISED( pxList )                ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )
复制代码
0 m8 N! c: S& B# N1 Z0 \! w
05. 列表相关函数# ~+ k" m6 Q# r8 O# D* Y
5.1 初始化列表

; m+ c/ {+ l: f  v列表结构体中包含一个列表项成员,主要用于标记列表结束。初始化列表就是把这个列表项插入到列表中。
, p5 p' D( b7 |$ U" X" x5 w) B5 Z7 R4 `  S. V* m: B8 W7 I
  1. void vListInitialise( List_t * const pxList )
    : h( z/ w2 W& W/ y, I6 q0 r
  2. {7 m; ]4 n, ?/ I) p
  3.      /*列表索引指向列表项*/& ?5 H8 ^4 Q$ t" K
  4.      pxList->pxIndex = ( ListItem_t * )&( pxList->xListEnd );                  . A3 A1 ?/ C3 J
  5.      /* 设置为最大可能值 */: Q% {# ^; b- b
  6.      pxList->xListEnd.xItemValue =portMAX_DELAY;6 {; K7 Q& |! w" w* y1 Y4 t

  7. + `! |, O+ j& n8 u4 Y. t
  8.      /* 列表项xListEnd的pxNext和pxPrevious指针指向了它自己 */
    - k' J, u( h/ N7 U+ i
  9.      pxList->xListEnd.pxNext = (ListItem_t * ) &( pxList->xListEnd );
    ! f7 h+ B& [. r$ f& H- c) C% ]& ^
  10.      pxList->xListEnd.pxPrevious= ( ListItem_t * ) &( pxList->xListEnd );
    ' R4 A  f, S5 s
  11.      pxList->uxNumberOfItems = ( UBaseType_t) 0U;2 T5 `3 i( O: {# D( N4 h* W

  12. * C+ ~6 l. x0 H" Z+ h$ X4 m
  13.      /* 设置为已知值,用于检测列表数据是否完整*/; q1 S6 h' A# l. `0 P1 B6 e
  14.      listSET_LIST_INTEGRITY_CHECK_1_VALUE(pxList );
    " t/ L1 c7 J, j3 X6 B2 ^! @/ d
  15.      listSET_LIST_INTEGRITY_CHECK_2_VALUE(pxList );: c+ [) R! a! ^
  16. }
复制代码

, w8 V- \8 E) a' y  n如果宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES设置为1,则使能列表项数据完整性检查,则宏listSET_LIST_INTEGRITY_CHECK_1_VALUE()和listSET_LIST_INTEGRITY_CHECK_2_VALUE被一个已知值代替,默认为0x5a5a(16位架构)或者0x5a5a5a5a(32位架构)。
5 G+ f! e$ n* J% H' p3 N4 z# g! H$ e5 Z: k5 y. }
P27PILXP(KBZ1I~E4YVFB2W.png ( z- p2 b, \9 x* O0 z
0 Z# U8 W( a2 h
5.2 初始化列表项* ^7 }3 R* o4 G0 y) a; j
列表项的初始比较简单,只要确保列表项不在任何列表中即可。4 v8 r, a9 o# @. N# `9 H5 K! Z! O
8 Y- O. e' w0 `9 w6 e  v
  1. void vListInitialiseItem( ListItem_t * const pxItem ); ~2 R7 l/ V. s/ v/ i3 _
  2. {
    , `' V1 Y, V0 Q% e8 R1 e+ f
  3.      pxItem->pvContainer = NULL;
    " ^, [( z  s" k

  4. # P2 C4 \$ C% J1 H' Q6 y9 U
  5.      /*设置为已知值,用于检测列表项数据是否完整*/  g, N$ }+ i! b! J; u, w# ]
  6.      listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );5 f7 F, Z  V9 [* ~$ u( v* {
  7.      listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );
    5 R8 b5 q) K3 c# E) e/ V: T; H; I' H
  8. }
复制代码

( N6 _2 }$ U( i% c+ N6 s+ S" }如果宏configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES设置为1,则使能列表项数据完整性检查,则宏listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE和listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE会被两个已知的数值代替,默认为0x5a5a(16位架构)或者0x5a5a5a5a(32位架构)。
5 g3 d, l' ?5 k8 Q6 Q- |0 y( _# ~2 i5 b
5.3 列表项插入函数3 Y  a9 G+ P7 z* E5 b* v$ c. P
每个列表项对象都有一个列表项值(xItemValue),通常是一个被跟踪的任务优先级或是一个调度事件的计数器值。调用API函数vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem)可以将pxNewListItem指向的列表项插入到pxList指向的列表中,列表项在列表的位置由pxNewListItem->xItemValue决定,按照升序排列。
$ S% y9 o1 b* W% O3 Q' Q" s' _8 c. p. ^% V' ]2 l
  1. void vListInsert( List_t * const pxList,
    9 K5 x" r1 M5 K- e' M( Y/ F2 x
  2.                   ListItem_t * const pxNewListItem )
    8 Y8 l+ K2 O: L" x7 L* F
  3. {
    # q! r9 ]  }7 U5 c" i6 w: y
  4.     ListItem_t * pxIterator;; w  S! q1 {6 b! I+ U
  5.     const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;- s) w+ e2 ?7 F& X& m" T4 G

  6. / J$ u( d) D. Y
  7.     /* 检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/0 l& G7 n9 R" Y- A, |9 S
  8.     /* Only effective when configASSERT() is also defined, these tests may catch5 Q1 D5 d2 J) y, |
  9.      * the list data structures being overwritten in memory.  They will not catch( M. {) _6 P, j+ v
  10.      * data errors caused by incorrect configuration or use of FreeRTOS. */
    / a) v, a0 q4 V: o) u( [" n. c
  11.     listTEST_LIST_INTEGRITY( pxList );3 R9 M, N; k4 M2 V$ z
  12.     listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    & k4 [! c& H+ @7 L2 R: n7 m

  13. & a2 F/ x1 t& \6 A8 P" v, _6 W
  14.     /* Insert the new list item into the list, sorted in xItemValue order.
    + N5 D' _1 D' ?& q( @. \
  15.      *
    3 |! L* a1 P# u* ~% F- h5 z4 C/ A
  16.      * If the list already contains a list item with the same item value then the
    4 z; w: i, L2 u! H3 u
  17.      * new list item should be placed after it.  This ensures that TCBs which are" j0 c$ k; B% E; r, S, T+ s
  18.      * stored in ready lists (all of which have the same xItemValue value) get a) g( J$ U1 @1 I8 X) v
  19.      * share of the CPU.  However, if the xItemValue is the same as the back marker
    : a+ S) v: b0 f$ P3 M5 u  m6 Y
  20.      * the iteration loop below will not end.  Therefore the value is checked
      y, w. ~, W4 W, W
  21.      * first, and the algorithm slightly modified if necessary. */0 p7 F: }4 j. h0 K% F- T3 s9 [
  22.     /*将新的列表项插入到列表,根据xItemValue的值升序插入列表。*/
    6 O% S! |3 F# u
  23.     if( xValueOfInsertion == portMAX_DELAY )( b* {7 ~7 ~; L7 \- c" G# G) |- Z) g
  24.     {
    ( S- Y9 D0 [/ I. }  Z: E/ B$ l2 z
  25.         pxIterator = pxList->xListEnd.pxPrevious;
    ! S' x& z! p) ~1 O8 a' n
  26.     }
    * ~6 v% U/ n2 o9 g" S9 G2 z; f. e5 n
  27.     else* K# M2 t0 ]! W
  28.     {, {, x' b& A! }- ^0 v+ l' g
  29.         /* *** NOTE ***********************************************************( ]6 c# d$ x8 L' a8 z5 B; _
  30.         *  If you find your application is crashing here then likely causes are
      B8 }% T( U8 Q  S9 H" V8 a$ Z
  31.         *  listed below.  In addition see  for4 ?7 u. O. J2 w2 V
  32.         *  more tips, and ensure configASSERT() is defined!
    ( T( i. q6 r1 P3 T0 ]
  33.         *   R/ B. z6 H8 F
  34.         *: \; P3 K1 {0 W  t
  35.         *   1) Stack overflow -, e$ \1 W" }% \, B3 f
  36.         *      see
    $ ?& v0 T0 S* o+ m5 \9 I( w
  37.         *   2) Incorrect interrupt priority assignment, especially on Cortex-M. @! R; C: N' s8 j: p4 J' |/ P
  38.         *      parts where numerically high priority values denote low actual
    : D( U! |8 [/ |2 Q5 e6 M
  39.         *      interrupt priorities, which can seem counter intuitive.  See
    " o8 W2 d: A6 e! V1 w8 F0 ^
  40.         *     and the definition2 }) G& Q% s% {( f) C, V
  41.         *      of configMAX_SYSCALL_INTERRUPT_PRIORITY on
      s' h- X) z6 H9 W5 z
  42.         *   
    ) i1 q) s; {! i$ I& @( g; D4 a
  43.         *   3) Calling an API function from within a critical section or when' {( o& B6 u/ Y8 L( j% k' f! w
  44.         *      the scheduler is suspended, or calling an API function that does
    8 j  `6 V- q7 Z+ D8 R
  45.         *      not end in "FromISR" from an interrupt.
    " E# ?% |2 N$ K6 K, X8 a2 |
  46.         *   4) Using a queue or semaphore before it has been initialised or1 p1 B; W6 U8 B+ B- T: O
  47.         *      before the scheduler has been started (are interrupts firing# ]2 J8 k" x, J
  48.         *      before vTaskStartScheduler() has been called?).
    ' R( Z* z0 `; X* v% j
  49.         **********************************************************************/
    * H# t7 P' [: I4 A0 ^- s0 Z
  50. + g' }4 n  y, T' D6 O! H
  51.         //xListEnd表示列表的末尾
    + i% f8 L% m* l
  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. */. g7 G2 F1 C9 j- J/ {- F3 T
  53.         {4 N( h. Y& o6 L: G
  54.             /* There is nothing to do here, just iterating to the wanted. n$ ~8 b% }% G4 J3 z
  55.              * insertion position. */0 u) [- J: f" U6 J7 U# S
  56.         }
    9 v6 W. J) i4 \' q+ S
  57.     }* e. J0 W. G) ]- w7 B# S

  58. 5 {$ ^/ {4 W! b, Y, k8 N# O; N
  59.     pxNewListItem->pxNext = pxIterator->pxNext;  s  t" g6 j) j, z- |
  60.     pxNewListItem->pxNext->pxPrevious = pxNewListItem;
      ~- G! C5 W2 P' X1 H5 D
  61.     pxNewListItem->pxPrevious = pxIterator;
    $ I2 Z" O% H2 ^8 g6 X( L
  62.     pxIterator->pxNext = pxNewListItem;( {) \( X" _; O) k$ [0 `+ R8 j$ i" q

  63. % x, `& _0 Y; ]1 L+ e
  64.     /* Remember which list the item is in.  This allows fast removal of the
    % R+ @. F2 R/ w) R) y' X6 P
  65.      * item later. */
    + g8 K/ M$ {% \6 i% u% p% v9 p
  66.     pxNewListItem->pxContainer = pxList;, O# g8 U& W' M7 h

  67.   F( A6 ?( ~1 u- {2 G* J* a% f& K4 H
  68.     ( pxList->uxNumberOfItems )++;( w$ z' ?) x% j0 x1 X2 L
  69. }
复制代码

2 S. C! p* P4 n$ ]1 ?8 X8 k根据xItemValue的值将新的列表项插入到列表。如果列表中存在与新列表项xItemValue值相同的列表项,则新插入的列表项位于它之后。如果列表项的xItemValue值等于portMAX_DELAY(列表结束标记,我们在讲列表数据结构时,说到每个列表数据结构体中都有一个列表项成员xListEnd,用于标记列表结束。xListEnd.xItemValue被初始化为一个常数,其值与硬件架构相关,为0xFFFF或者0xFFFFFFFF。这个常数在移植层定义,即宏portMAX_DELAY),则表示到达了列表结束位置。
& F3 z6 |- F5 V+ U- v9 M, c. E7 P
9 d: v2 ~1 s. N. @& u1 P5.4 列表项末尾插入
' t: j. m7 x/ W1 r: d/ [
API函数vListInsertEnd()是简单的将列表项插入到列表的末端。/ J3 I& y- W8 w: q

/ g' c  B& P) I  s6 }6 _
  1. void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
    5 M8 E8 |4 e* \; A
  2. {' [7 e4 Q4 a8 y+ a
  3. ListItem_t* const pxIndex = pxList->pxIndex;
    5 o" I8 e+ A# @7 V6 g

  4. , s4 T/ p0 b4 T3 R4 c+ @8 j3 N
  5.          /*检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
    0 M  J& q; ^7 j* ^) ~/ _% K
  6.          listTEST_LIST_INTEGRITY( pxList );
    ( Q$ o7 W6 ^6 i  e- z6 A
  7.          listTEST_LIST_ITEM_INTEGRITY(pxNewListItem );- K6 A0 R5 @: C; T; J$ a9 d
  8. 7 y* r( z+ \/ [6 W- _
  9.          /*向列表中插入新的列表项*/
    4 S( x; i& n; c- E* V0 M+ ^) q
  10.          pxNewListItem->pxNext = pxIndex;
    6 u- u% F+ p" ^/ ?* x" G8 R
  11.          pxNewListItem->pxPrevious =pxIndex->pxPrevious;
    5 J. P" y3 K' A

  12. 2 V2 v: B4 F4 A& W
  13.          mtCOVERAGE_TEST_DELAY();
    5 Y2 |) w6 x9 G& [* b( J& p

  14. / }/ b$ {  B2 ~+ A# w( W. e$ ~
  15.          pxIndex->pxPrevious->pxNext =pxNewListItem;7 b- }  Z9 g* i5 d5 }; l
  16.          pxIndex->pxPrevious = pxNewListItem;
    6 _; _  z% ?$ x1 b+ p& k
  17. 2 S+ N: f. v8 J
  18.          pxNewListItem->pvContainer = ( void* ) pxList;
      q; Y' M9 [; J
  19. $ [! d. \7 b0 d2 W0 `7 x( G
  20.          ( pxList->uxNumberOfItems )++;1 ]! ~2 D! I; C4 }- N* i: o
  21. }
复制代码

7 X+ i2 w, k/ P# U+ A% _5 e: ^" ?5.5 列表项删除
% k6 l7 P6 g0 z; G$ l9 _9 c列表项的删除只是将指定的列表项从列表中删除,并不会将这个列表项的内存给释放掉。
: w1 H( Z2 n; n! N# J7 F
$ }% S: j0 ]! Y: U3 ~0 G5 H0 ~" T
  1. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )6 X& c" e* L( f" [* K5 N3 v6 C
  2. {
    3 O& U. g# K, b- w$ a$ q+ W: p. d* t
  3. /* The list item knows which list it is in.  Obtain the list from the list0 |* w* g. ?6 V, W5 f7 @
  4. * item. */% J$ \# {' q5 q& t: ^- \
  5.     List_t * const pxList = pxItemToRemove->pxContainer;7 |" x, i: m7 O. e7 e6 i
  6. ! `4 z6 [! C2 \8 ]  p
  7.     pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;2 b5 G5 r, T1 B* F9 m4 Q
  8.     pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
    9 n, Z- O+ N9 Y7 v
  9. $ ^4 ?6 \, g5 F" z1 v1 a/ b5 [
  10.     /* Only used during decision coverage testing. */
    / g+ c  w3 `# V8 m$ Q; d2 u- v4 f
  11.     mtCOVERAGE_TEST_DELAY();, x4 N( T2 N) B9 ?. Z7 y+ i6 d2 d
  12. 8 {4 B' |3 E8 S
  13.     /* Make sure the index is left pointing to a valid item. */. N0 n) N9 h9 M
  14.     if( pxList->pxIndex == pxItemToRemove )' ^4 l- G% r1 N3 }: O
  15.     {
    # C: r# _0 f+ v. Y) n
  16.         pxList->pxIndex = pxItemToRemove->pxPrevious;+ ?7 R6 I3 }+ z" L
  17.     }3 T: g, V4 v. s6 v+ F$ J
  18.     else
    0 U  i" z( m) |$ G; ~0 h, T% o- D
  19.     {
    ; ?3 }; H3 A6 w9 ^) j# m1 ?- _
  20.         mtCOVERAGE_TEST_MARKER();$ `7 R" ?; q1 C
  21.     }
    + g8 F$ G& z; _2 }+ n. T

  22. ' [( ?; u" u7 O
  23.     pxItemToRemove->pxContainer = NULL;
    ( ~. Y" v* N( w$ b5 ?
  24.     ( pxList->uxNumberOfItems )--;- C0 ~2 H3 c6 R8 k9 I% F
  25. # M4 Q& V) b1 m4 D& D5 v1 W
  26.     return pxList->uxNumberOfItems;0 _8 h1 V! O) X1 K5 q4 v
  27. }
复制代码

' l3 R. N$ J$ t" {: {5.6 列表遍历
& |! X1 Y# _  n- u0 s. o
  1. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )                                           \. K' H" `0 [. Z) E; l: I
  2.     {                                                                                          \
    9 A2 [; e) S- R; H
  3.         List_t * const pxConstList = ( pxList );                                               \. `4 K! ~, n6 i
  4.         /* Increment the index to the next item and return the item, ensuring */               \
    ; R; P. w5 k: \; K" K) L
  5.         /* we don't return the marker used at the end of the list.  */                         \# x; b) E' o2 N" I$ V1 [( \
  6.         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                           \1 V# M4 r' j2 k6 _$ t6 P
  7.         if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
    ' Q) Z8 _% L+ V2 f
  8.         {                                                                                      \
    $ D) r7 V4 J; r
  9.             ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                       \; g; U/ C6 z, D$ `& b/ F' e# r
  10.         }                                                                                      \
    3 R( }1 Z& G/ B5 p1 c2 {1 y, f
  11.         ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner;                                         \
    * m9 A" _; l& U+ B: |  }  y
  12.     }
    5 E" }. r. Z3 f0 l& X( J
复制代码

: F4 b  z8 G! h( s) j( G6 l5 [8 C3 t: x9 g/ f# W

/ |! G9 F+ w8 F2 F# R: Y
赞 收藏 评论0 发布时间:2022-6-18 17:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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