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
) 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- typedef struct xLIST/ y% ]. t) c; W& V
- {
) e; T2 z8 B, I0 p( o - listFIRST_LIST_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/
5 R0 D% b2 n3 i: O5 d - configLIST_VOLATILE UBaseType_t uxNumberOfItems;& C- p9 X+ i4 w8 D) ?* w- j! U, f$ X8 P
- ListItem_t * configLIST_VOLATILE pxIndex; /*用于遍历列表*/5 Q. C9 ~/ }) ~" ]( U/ n
- MiniListItem_t xListEnd; /*列表项*/
6 p* J* Z$ U7 j4 @ - listSECOND_LIST_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/
% I7 N; L# G$ J) v/ R - }List_t;
复制代码
& l+ ]. Z4 P9 a; W
# _- 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
- struct xLIST_ITEM
6 l0 v, n# T) T - {
& H: k" R4 U: ?0 M7 A - listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/
1 J9 K) Q; I7 X/ v4 e - configLIST_VOLATILETickType_t xItemValue; /*列表项值*/; Z4 f7 ?; h- _+ y- h5 U5 I* s
- struct xLIST_ITEM * configLIST_VOLATILE pxNext; /*指向列表中下一个列表项*/1 ~2 b3 V) d) m* B
- struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /*指向列表中上一个列表项*/% A& L2 \' V! Z1 ~* y; B
- void * pvOwner; /*指向一个任务TCB*/
# g% Q2 P* u* q" O; w - void * configLIST_VOLATILE pvContainer; /*指向包含该列表项的列表 */
! n+ `6 V2 x8 \6 E( L) i - listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/
; ?3 b3 N0 z9 T5 Z" Y% K3 V - };
7 ^4 i" t% M/ C+ R- U" V. A6 N - typedef struct xLIST_ITEM ListItem_t;
复制代码
+ H3 w2 D" C6 f0 z9 V5 H9 [6 w
) 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
- struct xMINI_LIST_ITEM+ `& u4 F( `: n! u7 k. O# ^* R, [
- {' u6 ^# C( J$ j( _5 H. I9 ]" a
- listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/
8 e& l/ K3 s8 E - configLIST_VOLATILE TickType_t xItemValue;
* K& b- @, m) u5 Y - struct xLIST_ITEM * configLIST_VOLATILE pxNext;/ U6 ]9 U+ X% _' I9 i0 V
- struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
' c- w& m- L! w - };" \8 U: K7 D, r' h4 V5 W5 {
- typedef struct xMINI_LIST_ITEM MiniListItem_t;
复制代码
0 d) P6 ]7 u( I/ P; ]2 W- I
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
- /*; }# M! T9 z6 W; Y% ^" ~* r/ B
- * 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 - * is the object (usually a TCB) that contains the list item.
3 X' f+ E' H& N ?! y - *5 ?; s1 j3 C5 M. ~7 \5 t
- * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER' P1 Q: j1 c- v" n5 L
- * \ingroup LinkedList
6 r, z9 K( t) j9 Z9 U9 {. ] - */
5 S! p, \$ D2 M1 C4 Q$ o5 M/ r - #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )
* N D% u+ x3 G5 x" U
7 Y; Z' n& B5 U1 _- /*, t, ?' C, C0 ?: m# N7 s; S
- * Access macro to get the owner of a list item. The owner of a list item9 ~" ]( Q1 w9 Q, _! @ e
- * is the object (usually a TCB) that contains the list item.
* B" E: Q, ~- q: G - *
% l9 s# S7 m0 ^# L3 o - * \page listGET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER9 X* o; z4 [9 f5 O/ z: o1 e
- * \ingroup LinkedList4 G4 H: |. p/ t2 h
- */
& H/ w u6 @+ Y+ b& j - #define listGET_LIST_ITEM_OWNER( pxListItem ) ( ( pxListItem )->pvOwner )
9 m+ `( h* t6 H R3 ~ - . F/ ~8 H/ ^: h! d& [2 ?$ _4 S5 D' [
- /*
6 v' u8 I1 Z8 W+ ^ - * 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
- * used to sort the list in descending order.
0 [# e9 Y! e0 G5 U9 X$ L - *
( O) v: c4 j1 S# a) N) o3 s6 F3 p - * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
2 J) N3 ~! h6 l9 Q - * \ingroup LinkedList- v% ~9 S( W/ c5 Y0 o3 X+ Q) o# E
- */
( W1 R! G3 b: z/ L4 s - #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( ( pxListItem )->xItemValue = ( xValue ) )
! |* k7 V9 y4 [$ H% z8 p: e" K
' B) l( I+ K* D1 y- /*
0 B5 Q b" P. n" C; r, U - * Access macro to retrieve the value of the list item. The value can8 e1 r3 B* I3 P
- * represent anything - for example the priority of a task, or the time at6 x5 ` c0 }3 |' [- ~7 S! f& ]8 b3 U6 u
- * which a task should be unblocked.
6 K7 i! O1 m' B) j! Q/ \# \! { - *
! a$ W: f0 O" ^5 W, l3 x* L - * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
; p4 ~" a) a) `' r- b - * \ingroup LinkedList& V: {2 Q7 Z' p
- */
' J. V, k# w! O - #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )- _3 s& d4 d; N, K T
3 m+ G! A; s/ j+ D7 k. b. E- /*
( B( M& x& d) g - * Access macro to retrieve the value of the list item at the head of a given; a7 j1 ?! [5 R8 m
- * list.$ h3 J: [! C4 V( S: t
- *
& Y* ?1 r# Y" J% p+ j - * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
4 F5 U: P8 g. ]! T* v - * \ingroup LinkedList$ K$ T u d7 {- z" y( D
- */ w! X2 J$ i" g0 Y- `% a% O
- #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext->xItemValue )3 g$ ~: [- L( c0 L
- 2 |: ~' G$ u( `/ t8 h. K: _& K
- /*& K/ z: T2 s0 f8 c8 p# x3 `2 Z
- * Return the list item at the head of the list.
0 N* S* |$ V, D5 o - *$ K& I2 z$ y9 q) Q1 g9 |- a' U
- * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
) H+ x- p9 [, O( x - * \ingroup LinkedList
0 O0 O% \% f' V* v5 A - */
. O: t- I* S) i- q# v% A( r - #define listGET_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext )
! G6 i2 l1 h# Y- s - & ~- B$ d2 e- n% t) |
- /*
l# }& V+ a, d - * Return the next list item.
! C$ f# E0 ~3 y, |2 l. g - *% ^% m- ~- n# G0 a3 l8 t2 r
- * \page listGET_NEXT listGET_NEXT
. O; R1 S% }" |' K1 e( g- p - * \ingroup LinkedList
$ U/ l, y& Y. M, u, q - */! V. l+ y2 y) U3 W
- #define listGET_NEXT( pxListItem ) ( ( pxListItem )->pxNext )" J k: z7 L* m3 [1 T7 w
( g& B! S$ b# W/ e/ E- /*
1 l# K+ u# C; z- `" H( Q+ E% C - * Return the list item that marks the end of the list0 X1 J9 F1 y+ @
- *
/ l" U: |4 j7 ~# g - * \page listGET_END_MARKER listGET_END_MARKER
$ k2 {& E; n/ {& [( k - * \ingroup LinkedList( }4 r: d1 e! f# y" i: r+ ]+ B
- */% w; I9 ]& J3 H5 J) P
- #define listGET_END_MARKER( pxList ) ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )! A; t; R% q& W' B8 l5 N# K# V' l
, _. t) x' Y T/ h* I- /*- [; g- X4 u; {! H
- * Access macro to determine if a list contains any items. The macro will4 r6 P% p- N! @1 K& q
- * only have the value true if the list is empty.' D$ T: j. x. M2 ~" u4 a
- *
6 M# v1 C c. Q: r1 @' K6 t* z% l - * \page listLIST_IS_EMPTY listLIST_IS_EMPTY- F9 K) n( X& I( @2 k3 k, M1 j3 z
- * \ingroup LinkedList
9 E, r1 Y" |6 I - */6 ~, {, M# P% j- }2 F( d
- #define listLIST_IS_EMPTY( pxList ) ( ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) ? pdTRUE : pdFALSE )
# v5 R& B% n% v( @" d7 Q# r4 L' H - ; G G! ~4 S/ C5 }* L( D
- /*
8 r5 q% W0 e* Q& Z* F - * Access macro to return the number of items in the list.6 E0 {' _2 K% J/ _# z
- */, P/ m6 F$ ~* E: k; I6 x! t
- #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )
2 o @3 C, L! D& D0 j - 1 i* R, @6 J. d* |: T
- /*' S! Z C8 ]0 [; j# C
- * Access function to obtain the owner of the next entry in a list.
! L4 c* t/ p i) a9 Z E! Q - *2 o, J" I+ J8 r1 u
- * The list member pxIndex is used to walk through a list. Calling( r+ R( b8 ]; [/ X' M
- * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
" D. q1 R3 N9 h" S% Q. s3 h - * and returns that entry's pxOwner parameter. Using multiple calls to this
% r2 B9 w2 w# _8 ~% G! n - * function it is therefore possible to move through every item contained in5 b3 L, t9 D2 l& t- W
- * a list.
* ~" j- F+ ?/ X - *' ?' X, B4 q. M. T
- * The pxOwner parameter of a list item is a pointer to the object that owns! E3 J5 w" X) P: l _: j6 C
- * the list item. In the scheduler this is normally a task control block.
. n. w. i- F7 Z - * The pxOwner parameter effectively creates a two way link between the list
; H' v% e# @8 O! n! U4 x - * item and its owner.( @# Z/ o7 \5 h0 U9 r
- *. K+ u8 A! L) t8 G7 p* G
- * @param pxTCB pxTCB is set to the address of the owner of the next list item.
& e. Q/ x+ X+ [8 J5 N - * @param pxList The list from which the next item owner is to be returned., l% d" ~! [# s
- *
& y5 u' |( Z9 W" j) i- I - * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY9 ^& E$ f) ~( q% c
- * \ingroup LinkedList
' e# l- ]% O: N/ p, j0 e4 ?# I - */3 d9 ?7 r; u9 o9 V" P6 } f1 [7 z
- #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
) C% g9 X" e7 ?/ P: z! f+ ~ - { \0 b4 \/ H2 y; N* e: T$ w% ^
- List_t * const pxConstList = ( pxList ); \$ c ]5 ]2 {; {1 q6 ~4 U
- /* Increment the index to the next item and return the item, ensuring */ \! z# h! R" S: ~ s5 |8 `# k
- /* we don't return the marker used at the end of the list. */ \/ [" @9 v; Z- U! f1 s+ |
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \! n1 C. `% t* G# {. z2 E1 E$ [
- if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \& `- k1 n/ h- O
- { \
# f; I/ k0 H6 t; a& S& ` - ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
1 e! C' E/ }- v, e - } \( a/ C/ z) s4 _: [1 a, r8 t
- ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
# U9 C2 ^) n& c$ \7 o9 \1 L - }9 V* P7 R9 ]5 L5 w3 ~
- # y) S0 a6 j9 t) g p2 A. s! U; R
- 7 W* M6 `. U! k6 H. v
- /*8 s* z6 `( K% ?4 H" N" k( O
- * Access function to obtain the owner of the first entry in a list. Lists3 N3 y6 X) w W% I! i
- * are normally sorted in ascending item value order.# a- u/ d( \$ J1 `% b
- *
; c- r2 d. W3 V - * This function returns the pxOwner member of the first item in the list.. V0 ^7 o" C& x, s
- * The pxOwner parameter of a list item is a pointer to the object that owns
( `6 s5 |: v+ L# e6 x - * the list item. In the scheduler this is normally a task control block.1 I# d& Q/ d: o
- * The pxOwner parameter effectively creates a two way link between the list% H% F1 l. |$ `# O/ U$ S% H
- * item and its owner.
a3 |3 G2 L& p - *. }, I+ J; k: |3 Z) [+ O
- * @param pxList The list from which the owner of the head item is to be, b) Y% J" o0 S+ N. Q I
- * returned.
9 v# n6 j; N P2 d0 I$ B7 G' G - *
5 l% t8 z% K/ R! p& c7 X - * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY2 x# S2 ~6 @ \1 Q3 }
- * \ingroup LinkedList3 ^+ B8 S; O* Q. j& B! Q1 c* n$ \9 f
- */$ f2 @! |" B5 m/ m4 s/ K
- #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( ( &( ( pxList )->xListEnd ) )->pxNext->pvOwner )
& ~& B" }+ h' X - # |+ q0 G2 G; H' A) [
- /*0 U4 z/ Z _+ m+ p7 A3 w
- * Check to see if a list item is within a list. The list item maintains a
, e) W. L7 c* W - * "container" pointer that points to the list it is in. All this macro does
1 J# |" _, k' |) y; a$ k - * is check to see if the container and the list match.
! X: f. v6 s- v - *
9 a; \/ {( V! h, q - * @param pxList The list we want to know if the list item is within.3 Q1 Z2 w7 q2 G6 j0 W
- * @param pxListItem The list item we want to know if is in the list.
" k& X$ M' |3 \ - * @return pdTRUE if the list item is in the list, otherwise pdFALSE.8 ~9 t+ O/ }% G: y+ R. ]
- */5 }# A& e. X6 Z3 F2 O
- #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( ( pxListItem )->pxContainer == ( pxList ) ) ? ( pdTRUE ) : ( pdFALSE ) )
. R) W; ?0 \/ P3 R' M" K - $ |2 h! v+ j5 {
- /*& \+ J: V, w) Z- L3 H& x
- * Return the list a list item is contained within (referenced from).
% q# k6 x+ O% }" m2 H0 Y1 \ - *
, ^3 a7 o# [+ _ T2 \ - * @param pxListItem The list item being queried.
3 F2 ]' ~) {# J# @ - * @return A pointer to the List_t object that references the pxListItem) ]& g6 F* a7 y# l# @
- */% l3 ]1 X9 ]$ ^* r
- #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pxContainer )& ~5 s4 o9 X3 H5 o y( M+ U% T
- 0 X" b" L2 r% }7 E9 ]- L
- /*
6 p& U' b$ a% l Z. L$ N, S - * This provides a crude means of knowing if a list has been initialised, as
) J) Q- U' [$ M, O5 k# K5 p: Q/ q - * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
8 U; _2 m3 n( O - * function." U( I" N: y0 P( _
- */% S6 S; o5 O6 D' k7 }; S9 G6 J
- #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- void vListInitialise( List_t * const pxList )& E5 E7 b5 g0 N9 g9 ?+ Z ]
- {
/ Q* |, j5 W8 G% I - /*列表索引指向列表项*/
. O9 p E+ e+ M. j$ y' z& V+ p6 b - pxList->pxIndex = ( ListItem_t * )&( pxList->xListEnd );
, h1 d; v9 ~3 k# u9 \0 Z. C- J+ h - /* 设置为最大可能值 *// A: K$ y$ b/ p4 S( F) A
- pxList->xListEnd.xItemValue =portMAX_DELAY;
7 `" n% Y3 B6 t - . b% E' i6 e$ L' [" ~
- /* 列表项xListEnd的pxNext和pxPrevious指针指向了它自己 */( h/ `! F" v: s+ \7 e; { v
- pxList->xListEnd.pxNext = (ListItem_t * ) &( pxList->xListEnd );
3 I6 l/ {; T$ a/ n4 g I' q - pxList->xListEnd.pxPrevious= ( ListItem_t * ) &( pxList->xListEnd );
$ t' g0 m. L0 o$ p3 r# b - pxList->uxNumberOfItems = ( UBaseType_t) 0U;
- p" B9 B$ c* U. ~ - 9 Y6 O" W, ?9 r8 L
- /* 设置为已知值,用于检测列表数据是否完整*/
& d/ j1 L1 {% ~' m - listSET_LIST_INTEGRITY_CHECK_1_VALUE(pxList );
) I' I2 n9 u) p - listSET_LIST_INTEGRITY_CHECK_2_VALUE(pxList );. a* [2 f/ i1 _/ \, l
- }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
* {# 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
- void vListInitialiseItem( ListItem_t * const pxItem )
$ y" \+ V, A. l2 V- @; z- s) X - {/ Y: Y' x6 @2 L
- pxItem->pvContainer = NULL;! T+ d0 G$ i) t9 f# F5 H
+ k# b. y4 Q" }/ {- /*设置为已知值,用于检测列表项数据是否完整*/
0 E* U' l$ y- E( G. _ - listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );7 |9 G5 z4 K1 f% E* I: ^4 K1 l
- listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );# Z7 l( \: ]1 v2 X( H! Y
- }
复制代码 * 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
- void vListInsert( List_t * const pxList,
8 C5 X! m$ d6 l" w, T) q - ListItem_t * const pxNewListItem )
( t9 K. K0 \# Q. ^* D - {/ z6 X5 J1 y9 x7 q9 o
- ListItem_t * pxIterator;
. _3 `3 N6 X+ ^3 Y* G- M - const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
- J. j; q1 ?6 _% c i% _$ ^
1 k }+ N1 m6 L- /* 检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
# z X ~3 o& ]& ]) k - /* Only effective when configASSERT() is also defined, these tests may catch
# @: V& k+ i% n, m5 N1 D5 t2 |% G+ N- p - * the list data structures being overwritten in memory. They will not catch
z. a/ i0 T0 `* A9 P2 ?4 O' F - * data errors caused by incorrect configuration or use of FreeRTOS. */
/ ^7 v/ B7 `# v9 b8 c - listTEST_LIST_INTEGRITY( pxList );1 _' m( R+ t( e, q3 p, i0 ?
- listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
. \, x: \2 @* @
. ^: ~' W9 |2 N" I9 x+ G- /* Insert the new list item into the list, sorted in xItemValue order.$ R9 U8 b) S# o8 M6 p
- *
- k4 ]) g, b1 d* ]9 g/ O7 ? - * If the list already contains a list item with the same item value then the, w- p8 ]% S S, a( G% ~* p7 ?
- * new list item should be placed after it. This ensures that TCBs which are
( Z& ~! k( i- g9 b - * stored in ready lists (all of which have the same xItemValue value) get a
' X2 ~, M& c- z - * share of the CPU. However, if the xItemValue is the same as the back marker+ x8 X* k5 K. e* a' F: g
- * the iteration loop below will not end. Therefore the value is checked0 T/ X) g% X+ |% w* `0 P+ c" ^
- * first, and the algorithm slightly modified if necessary. */
: ]3 J. _9 _% X, } - /*将新的列表项插入到列表,根据xItemValue的值升序插入列表。*/
# a4 s' v2 ]( }# N' a$ f - if( xValueOfInsertion == portMAX_DELAY )
9 \1 Q x5 c" o2 P ] - {# ?* `* X" v: L- J$ s# C
- pxIterator = pxList->xListEnd.pxPrevious;
* L& F$ K7 _9 M( d - }
" t! w* K' t0 R# s8 {& ~! ^; [3 y, z - else" W" a+ i! c2 x, N- G
- {8 R: F' o" w3 z) o( g" |" H
- /* *** NOTE ***********************************************************
. o" A4 _. G) `6 l) M, { - * If you find your application is crashing here then likely causes are
; C1 Q2 ?1 Z% [' L0 D3 w# T# O7 K+ ` A - * 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 - * more tips, and ensure configASSERT() is defined!' a1 v& {+ V8 s$ ]
- * <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- @ - *, Z o, Z' z. E( t/ L+ z
- * 1) Stack overflow -
8 e/ D0 v' w- s% @ - * 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 - * 2) Incorrect interrupt priority assignment, especially on Cortex-M5 B! K8 Y. b+ a; i& d3 [) k6 n5 |, n
- * parts where numerically high priority values denote low actual
) Q6 J! H+ p+ B6 } - * interrupt priorities, which can seem counter intuitive. See
6 q# X8 I+ ]: [* ?. S/ M - * and the definition% w; \( F; ~: N) O6 b6 D, m, U
- * of configMAX_SYSCALL_INTERRUPT_PRIORITY on" Q8 B |: B2 R a, ]' N
- * - h+ s" N1 ~6 ?% i4 a
- * 3) Calling an API function from within a critical section or when
( u; c) p3 R+ P* r: U% e - * the scheduler is suspended, or calling an API function that does9 C$ Q1 o" C8 e8 Y) t9 `7 n1 z5 j
- * not end in "FromISR" from an interrupt.5 @. J6 {" ~' A8 k( r% M3 J& n
- * 4) Using a queue or semaphore before it has been initialised or
: X" o" v# ?5 G @; x - * before the scheduler has been started (are interrupts firing0 @: R5 k0 j- k U3 \" L: T8 a1 \
- * before vTaskStartScheduler() has been called?).
' e4 }) X8 F6 J p$ P7 \ Q - **********************************************************************/
+ r7 m0 O9 M) p. N7 `$ L
& ?5 h. V+ z* I9 k9 d: i% I- //xListEnd表示列表的末尾# k' b4 B) G! o! `3 s' m9 B8 S
- 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
- {1 @3 e' D0 X6 u7 y
- /* There is nothing to do here, just iterating to the wanted1 {7 ^) @, O4 a# \ I
- * insertion position. */' A5 A$ W# ~# G* G
- }* v( Q1 p3 p3 m4 h8 N& J6 D
- }
: p, z, t$ ]: k# O7 O - 5 @& ^- M1 c. R9 K% I2 {, k
- pxNewListItem->pxNext = pxIterator->pxNext;5 N" c$ }- Y a i# f1 Q, T* P
- pxNewListItem->pxNext->pxPrevious = pxNewListItem;9 B5 Q; `4 s* n' {$ Y U$ E
- pxNewListItem->pxPrevious = pxIterator;
# G! I3 O. Q9 {/ a; m7 W. R - pxIterator->pxNext = pxNewListItem;5 x! T$ f# W! D2 q9 O( l
3 i) \& N- V/ Z# b9 W- /* Remember which list the item is in. This allows fast removal of the0 r( G+ m, `9 ^! S7 M& L
- * item later. */9 N# ?* i h4 p' g6 u, s
- pxNewListItem->pxContainer = pxList;
T! b4 J& x- I5 _! Q) L
% q/ {* O; @; k( q X; A1 [- ( pxList->uxNumberOfItems )++;9 M; l2 P" `9 V
- }
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
- void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )1 T8 l* d! h& t/ U7 k9 g# {
- {5 }- E) z: G* ^9 p+ ?$ v+ C
- ListItem_t* const pxIndex = pxList->pxIndex;) |7 _+ [1 V: y# Z
* g+ {7 d# M$ J5 e5 M- /*检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
; S3 ^* A! J! r& j6 p( N - listTEST_LIST_INTEGRITY( pxList );
& ~2 W6 r# V6 O1 H - listTEST_LIST_ITEM_INTEGRITY(pxNewListItem );! t6 D/ y" c2 D8 V6 F% ^
6 A1 s) ], `# z5 W* @- /*向列表中插入新的列表项*/" q, U% A- F, ^4 `2 k* K& o3 X3 R
- pxNewListItem->pxNext = pxIndex;# @9 A. h' Y" s1 m
- pxNewListItem->pxPrevious =pxIndex->pxPrevious;; c1 t3 n3 Z9 N" H" u
$ d/ h# q, B% F5 e3 m- mtCOVERAGE_TEST_DELAY();* {5 }. s9 [/ e$ S3 v V* ]
- L; w% o _: L- pxIndex->pxPrevious->pxNext =pxNewListItem;
1 i9 ~' x: I) o' ?+ j" y# w - pxIndex->pxPrevious = pxNewListItem;
/ }6 }: @0 F2 q8 H6 x) H% H5 X) o
+ V9 U, K1 n7 r; g! C2 ^: L7 V- pxNewListItem->pvContainer = ( void* ) pxList;4 z6 V4 o3 c& d
6 Q, p: e3 |7 ^4 }# L- ( pxList->uxNumberOfItems )++;
! n, S3 n3 u4 \# F9 p - }
复制代码 & 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
- UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
, o t8 U y* S' k - {, T8 f3 l- E5 }& }+ N0 l3 O, ?
- /* The list item knows which list it is in. Obtain the list from the list8 A/ z1 S# k/ X
- * item. */! g1 |& I9 }: _ U& W
- List_t * const pxList = pxItemToRemove->pxContainer;
% B* j. f0 Y' K1 l$ d# l# a - 1 J+ \4 R7 D4 b0 m
- pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;2 C# l5 k4 b8 P/ h7 H7 ]# H
- pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;+ N6 ?% `1 X# T, t' y
- 8 G2 A5 h/ h, P2 w2 N5 i
- /* Only used during decision coverage testing. */
# T; B9 ~9 j8 _( B' S, p - mtCOVERAGE_TEST_DELAY();
( e+ o1 q+ s0 j6 A6 D& L
2 ? ^/ N7 B, |- /* Make sure the index is left pointing to a valid item. */
; b% q" N. Q% s1 {+ v - if( pxList->pxIndex == pxItemToRemove )
X0 u8 `! q, y* g+ e( L" B5 r - {
# d# c8 W$ g3 w. b9 w! i' c/ a" C - pxList->pxIndex = pxItemToRemove->pxPrevious;
5 N" H9 B6 ]1 v - }# L0 q5 F$ r+ A& ]; o
- else
8 o. `* i8 _0 @3 Y$ s - {1 x/ \. v r* e, `3 p$ d' ]: R6 l4 N8 j/ @
- mtCOVERAGE_TEST_MARKER();
: k" B* @7 |$ Q - }
7 B" Q- L6 A/ q( d
3 S2 ?/ h" U- }! J5 U9 D3 H! p- pxItemToRemove->pxContainer = NULL;
" a: z. {; o4 {4 C! N - ( pxList->uxNumberOfItems )--;* q5 ]2 {/ f M. {- x
- 2 D C+ t* P3 E" q. n" O
- return pxList->uxNumberOfItems;( v( j" Z. _7 @& t& b8 A5 b
- }. |9 n: Q: k" I. m) }
复制代码
% Y$ c* W! ?: ^+ s" X/ f1 A5.6 列表遍历
& S/ t( Y1 q+ T/ U' J- #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
5 y4 C( [! d9 q# H5 v: K( w - { \
5 ]# K+ g( l* P2 h- o - List_t * const pxConstList = ( pxList ); \
: E0 d/ n- e$ N - /* Increment the index to the next item and return the item, ensuring */ \3 t, Y! K+ c' G6 [* w
- /* we don't return the marker used at the end of the list. */ \ K; b* W5 o5 }# O
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \* j2 q% d5 p) R' s7 s' X# y
- if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
( _4 J" x j, t: h9 s7 J+ V* J - { \3 i# K3 j% U4 \" k# }$ Z
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
/ Z1 g8 N L6 P% \% j" {- G - } \
) U- T/ A E8 i# r- s - ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
4 {" N# V% [4 a1 E2 r8 L5 T - }
9 `) }0 l: A; i; e, `: I( y$ [" B
5 D& b' |6 ]' S1 j8 l8 P. [* X- a
复制代码 ' \4 j. o9 }' r
3 W5 O# B/ {+ c! b; W2 n$ o
|