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
. |$ 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- typedef struct xLIST' P! y+ e/ ]: V( J# O3 I
- {
, ~6 m( ^" p' S6 K6 X7 Y# r% T* N - listFIRST_LIST_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/7 u: {4 g- ?/ S
- configLIST_VOLATILE UBaseType_t uxNumberOfItems;
: f9 R1 I: x9 Z7 F; T* o3 n - ListItem_t * configLIST_VOLATILE pxIndex; /*用于遍历列表*/
$ a: Y f3 h& [5 i5 _; a - MiniListItem_t xListEnd; /*列表项*/
* v9 Z4 q' x5 e' Q G' Q0 K2 G' \ - listSECOND_LIST_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/* q0 _' N6 V' D9 [
- }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- struct xLIST_ITEM
5 p4 w; ]* a0 {0 n - {( A) ~1 f* K$ a
- listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/! |& X/ B7 N) m, A" `# F8 }: K
- configLIST_VOLATILETickType_t xItemValue; /*列表项值*/
# o3 T2 S, \( @9 P - struct xLIST_ITEM * configLIST_VOLATILE pxNext; /*指向列表中下一个列表项*/
3 o0 c9 w: M# Q* u: E - struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /*指向列表中上一个列表项*/- c _. K4 W& ~0 h
- void * pvOwner; /*指向一个任务TCB*/+ a- ~1 C0 F! G
- void * configLIST_VOLATILE pvContainer; /*指向包含该列表项的列表 */
% Z( B* g# G6 D7 j3 | - listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/% \4 ]" J0 {* v7 C" l. C( r( N& d
- };4 S% j) O$ f2 I1 K. E- }1 ^ d
- typedef struct xLIST_ITEM ListItem_t;
复制代码
% y. K6 R' @4 Z% `, H# a
) 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
- struct xMINI_LIST_ITEM, u0 ]6 N$ k b* g y6 }
- {
' c+ D3 P5 U6 k - listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*用于检测列表项数据是否完整*/5 A5 `6 Z: {% D$ M m5 K
- configLIST_VOLATILE TickType_t xItemValue;
1 R, H; F' f7 a - struct xLIST_ITEM * configLIST_VOLATILE pxNext;
5 h1 \( [: T$ H6 V3 ?: L - struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;( u3 F& q9 R7 u
- };, c$ t) [, t. Y3 Q, _# c( A+ T# }
- typedef struct xMINI_LIST_ITEM MiniListItem_t;
复制代码 # ]- k) X8 y2 K: `# V
: 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- /*3 N* s2 R8 P& V0 J
- * Access macro to set the owner of a list item. The owner of a list item# l) a! q+ p% E7 ^5 R$ `
- * is the object (usually a TCB) that contains the list item.5 r, h* G1 }, h* A8 X* d
- *, x) |1 j) p$ X x
- * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER! W/ u+ E5 }0 M; S( U
- * \ingroup LinkedList
' i- C6 O4 R$ m5 N8 W' s - */; o5 |' h. C$ N
- #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )& v% |9 Q6 x* I i. Z3 x9 j
; I4 [! R6 O; V. k- /*
4 @# h6 ]# O1 k& [7 Q8 ^+ C( _' D - * 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
- * is the object (usually a TCB) that contains the list item.
; D% c" S L g - *
/ G3 c# X1 B9 T2 z2 ` - * \page listGET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
& ]& x/ w0 {5 k - * \ingroup LinkedList
" b+ Y% k1 l* f. m4 X - */$ \0 @" I1 a6 l7 _8 q' m# s6 ?3 B
- #define listGET_LIST_ITEM_OWNER( pxListItem ) ( ( pxListItem )->pvOwner )* F0 z8 g" D0 e' X
- M1 t5 D( J+ ?8 `' V( r" T- s- /*& J6 Y7 \6 t, X% C% R9 P- Q
- * 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
- * used to sort the list in descending order.; z0 K1 `- _, o9 a: l3 e5 E/ T' E
- *$ S) E1 \, v( |0 C
- * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
# ^/ n$ d3 Y( U) M% S5 _ - * \ingroup LinkedList
9 `. q7 M1 \9 S8 ^ - */4 H6 q: Q5 w, \% P* L9 q
- #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( ( pxListItem )->xItemValue = ( xValue ) )+ ^0 L. }0 {- M8 W4 s8 O
- $ o G4 \. E$ q$ ?; {- _
- /*7 |9 I: \* S/ H/ S% F
- * Access macro to retrieve the value of the list item. The value can; I4 {% s, Z: Q @1 W* U: k% X- t1 g
- * represent anything - for example the priority of a task, or the time at& |1 M+ I/ i2 }* s
- * which a task should be unblocked.
i. ^6 ~# z/ [. d( q; _ - *
' O; Q+ i9 U S2 p, u - * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
) r0 n4 G* ]* L: \9 r! e - * \ingroup LinkedList
- F m/ l4 V' K. R% k - */% O+ t1 F. n Y" g
- #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )0 [/ V, l0 U" {
- : i; g! H% `" {/ F6 V
- /*
- V! Y: `0 g, M) i; m3 \" R1 h - * Access macro to retrieve the value of the list item at the head of a given
# V$ H9 u+ U8 n+ O: r" m - * list.
+ Q" w s8 M3 E, ^. T - *# O- A! e2 f4 m4 \
- * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE- _5 V! R5 p( C# \1 c
- * \ingroup LinkedList2 ]2 {2 _' }- w* \/ R8 ^, z3 V
- */2 _! x4 g4 E$ l% b1 G4 I0 I
- #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext->xItemValue )- C: N$ ~2 \1 ]1 L6 |. f" Q6 x0 [
- 3 g3 R* n# T0 a7 p/ r$ @- d" r
- /*+ x3 g- ?$ p2 \$ Z+ b. ?
- * Return the list item at the head of the list.2 g& b( v! T# U! I& r0 j" G# r9 l
- *: D: T( l0 P" y, l$ F/ V% @
- * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
7 l( ^3 |. ~) |. n0 t' s3 t - * \ingroup LinkedList
: q; \& `* C. \/ y* _ - */5 p0 M! J* S3 O
- #define listGET_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext )
! h' M7 H& [: I; b% O - / Z8 n }: z7 j4 C2 {
- /*
" @( L) Z- ~! s7 j - * Return the next list item." u7 W$ \3 _" d9 `, w% k
- *
; W) \% _ @ Y; y - * \page listGET_NEXT listGET_NEXT" j1 M+ j2 c0 t: x0 L; s
- * \ingroup LinkedList! Q% e: v$ Q9 ] d( t
- */" V, r O' F* R5 o+ ^
- #define listGET_NEXT( pxListItem ) ( ( pxListItem )->pxNext )
9 T7 _" n, z6 N3 Y, R - # c. c6 B6 U/ I* A7 g
- /*/ L: s* K& F8 t1 G* M. [1 V6 g. ?
- * Return the list item that marks the end of the list
+ [& J: Z/ z- j6 u* X1 n6 B$ \+ G; m - *5 h U4 B( H4 T+ {$ F
- * \page listGET_END_MARKER listGET_END_MARKER
' E& | Z/ p% }, [ - * \ingroup LinkedList
1 }" ? W+ l. B% E# m! }2 U6 [ - */
: a1 z& C- n* y8 H - #define listGET_END_MARKER( pxList ) ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )
, u( \( r# ]& ?# S3 _) ~8 X - ( r3 l& t& |1 G9 D* x- B
- /*
5 \; S. X9 U: O - * Access macro to determine if a list contains any items. The macro will
8 W) J; P( w1 T$ t9 o' g - * only have the value true if the list is empty.; t: b# `) X2 y
- *
8 V$ A7 K s5 p! Z. P/ e* |# n. b - * \page listLIST_IS_EMPTY listLIST_IS_EMPTY4 L$ U( m& P9 c t
- * \ingroup LinkedList8 W0 w' K( K6 P' M# ]+ @# e5 O8 S
- */
1 p# h% U# g# o- M. h8 z - #define listLIST_IS_EMPTY( pxList ) ( ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) ? pdTRUE : pdFALSE )
: h0 V5 m5 c+ u- k
% |( y* `( v- n4 R. M9 X: ?6 n7 n- /*
5 W5 T! G: A0 d9 B. Z - * Access macro to return the number of items in the list./ F; y- m7 @5 _ W$ P# _
- */
$ G$ f, t Y3 ~ - #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )1 T, C6 m) F0 D/ G6 l
' \8 R$ p# V6 O. N- /*
$ J1 @4 }/ M4 d2 C7 z - * Access function to obtain the owner of the next entry in a list.2 [- D& S( C+ @
- *
# c) G6 c- `% B7 O - * The list member pxIndex is used to walk through a list. Calling
h. l% @9 o7 U; r1 @: E+ r - * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
, ^" [9 @1 B1 [5 G( J - * and returns that entry's pxOwner parameter. Using multiple calls to this I l" H7 i |9 h9 K0 y
- * function it is therefore possible to move through every item contained in) N" t1 ]: Z7 p( ?* @+ A& ^
- * a list.
* n' |$ M2 B, r& @( K% a4 e2 z - */ j& c0 [: P* [9 a- l- B
- * The pxOwner parameter of a list item is a pointer to the object that owns
0 q: _' m# F$ J, ~. W% N - * the list item. In the scheduler this is normally a task control block.# N$ S/ U# J! I) U" h
- * The pxOwner parameter effectively creates a two way link between the list0 [% P0 Y% d) N7 Z! C
- * item and its owner.8 f6 W. P3 I8 N( c% ~4 f3 U
- *5 ~& E; A$ d: W3 I
- * @param pxTCB pxTCB is set to the address of the owner of the next list item.
& H. q5 e& Z' G* j6 D - * @param pxList The list from which the next item owner is to be returned.
% O; x1 }, S: i) O5 j - *
4 s9 X$ S: y( R- D4 W - * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY& G5 J8 P& O/ P$ o# p
- * \ingroup LinkedList( k6 P1 F( B. L0 Z
- */0 [9 y. [! m% W+ o! N7 U3 z0 }
- #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \4 ^5 @2 A# g8 [" Z$ ~' t
- { \1 T5 v: t* T/ h7 d3 G( Y- a
- List_t * const pxConstList = ( pxList ); \
& y2 L, u7 a% z - /* Increment the index to the next item and return the item, ensuring */ \
4 k& V% ~2 n0 T9 R5 ^ - /* we don't return the marker used at the end of the list. */ \6 J/ ?5 U% N \. f- g5 `2 c$ p
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \& E0 T) [' I9 R# k8 p7 {* M* E* Q
- if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
/ a1 R7 P4 n( }( s1 ?/ R - { \: d: P, s$ [& l7 u' ?$ {$ h6 S( K/ u
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
, s, Q" w: ]% ]$ R* J - } \
7 L0 i" w0 _) z& B k - ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
6 T. A; c2 ~, g! J: n" u - }
1 T: k+ _* ^' H( {/ p t - 3 L4 F( F% d& K1 y1 B# J9 m
0 y4 d+ {# T% J7 L$ |- /*
* @- [( Y9 ? @$ A - * Access function to obtain the owner of the first entry in a list. Lists- V: t1 \. o6 i' [3 E) o) v# ~
- * are normally sorted in ascending item value order.+ {' J8 W7 c& e: ]. H! P ^
- *
1 s! L A1 j0 O- y9 _2 ] - * This function returns the pxOwner member of the first item in the list.- L8 ~( i5 K+ t& x+ F
- * The pxOwner parameter of a list item is a pointer to the object that owns5 C$ V# i A8 k& h6 r
- * the list item. In the scheduler this is normally a task control block.5 a7 ^4 n% v9 H+ q2 N4 C8 e' Q
- * The pxOwner parameter effectively creates a two way link between the list/ }5 @: j l8 k# G! u5 O
- * item and its owner.
9 q) Q+ f& R5 c1 ] n% r - *
: y' W* v; g" C# e - * @param pxList The list from which the owner of the head item is to be# q1 e' Y0 O: \+ X5 R! O9 X0 ?: O( ]
- * returned.: e; N6 h* E* i! f- c/ O
- *
' E; l( d2 `" i+ p7 o1 w' F& v - * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY3 @6 Z7 E+ T: y
- * \ingroup LinkedList: G; }1 @! u: |1 @+ n
- */
! |- ?- d/ k" U! ~& g6 l# T- y - #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( ( &( ( pxList )->xListEnd ) )->pxNext->pvOwner ); c v$ s/ I: a5 P: R
- 1 y: X8 x1 `% C. v
- /* A& `, L7 s+ l5 q7 r4 l
- * Check to see if a list item is within a list. The list item maintains a
4 V8 y6 K" g5 P) ?! e6 M - * "container" pointer that points to the list it is in. All this macro does5 O& G2 i% G: y. f+ T+ a( Q. n
- * is check to see if the container and the list match.; r7 ^# U- _# b& g
- *
3 Z. Y) o, O1 y! D - * @param pxList The list we want to know if the list item is within.
; ~4 ]4 [ K( k% Y - * @param pxListItem The list item we want to know if is in the list.
; Q. I3 t# [" U% y - * @return pdTRUE if the list item is in the list, otherwise pdFALSE.
/ R* k5 h: d8 x% t - */
! H9 ^0 D8 r/ E: u5 }4 i - #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( ( pxListItem )->pxContainer == ( pxList ) ) ? ( pdTRUE ) : ( pdFALSE ) )3 r& o; I* ]$ r
- 5 }7 Y0 z! A. q+ M: l
- /*
& H; P5 N/ |, K - * Return the list a list item is contained within (referenced from).
5 u9 Q0 l- s( ]4 |! [, i- Z - *
[( k9 c% `9 [% @5 f9 u8 E - * @param pxListItem The list item being queried.8 E( P+ W+ Z# `* j4 r0 b
- * @return A pointer to the List_t object that references the pxListItem( r5 S9 s6 A/ M7 [ t5 Y
- */
# B6 m ~# ~/ I4 h - #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pxContainer )
+ X. o' b) n7 n1 D0 ]: U8 y: m - . E4 _. n7 {% S5 f
- /*. u2 P1 m6 W1 N8 l( L1 E. A
- * This provides a crude means of knowing if a list has been initialised, as
6 A7 M. M/ ] K# N- G - * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()6 W% M5 y; o3 F; S3 t* S
- * function.' e: n9 f7 ?! p" o: Z6 j9 [" |1 n1 U
- */
# ?3 I) g" c5 I7 \; N; ^ - #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
- void vListInitialise( List_t * const pxList )
: h( z/ w2 W& W/ y, I6 q0 r - {7 m; ]4 n, ?/ I) p
- /*列表索引指向列表项*/& ?5 H8 ^4 Q$ t" K
- pxList->pxIndex = ( ListItem_t * )&( pxList->xListEnd ); . A3 A1 ?/ C3 J
- /* 设置为最大可能值 */: Q% {# ^; b- b
- pxList->xListEnd.xItemValue =portMAX_DELAY;6 {; K7 Q& |! w" w* y1 Y4 t
+ `! |, O+ j& n8 u4 Y. t- /* 列表项xListEnd的pxNext和pxPrevious指针指向了它自己 */
- k' J, u( h/ N7 U+ i - pxList->xListEnd.pxNext = (ListItem_t * ) &( pxList->xListEnd );
! f7 h+ B& [. r$ f& H- c) C% ]& ^ - pxList->xListEnd.pxPrevious= ( ListItem_t * ) &( pxList->xListEnd );
' R4 A f, S5 s - pxList->uxNumberOfItems = ( UBaseType_t) 0U;2 T5 `3 i( O: {# D( N4 h* W
* C+ ~6 l. x0 H" Z+ h$ X4 m- /* 设置为已知值,用于检测列表数据是否完整*/; q1 S6 h' A# l. `0 P1 B6 e
- listSET_LIST_INTEGRITY_CHECK_1_VALUE(pxList );
" t/ L1 c7 J, j3 X6 B2 ^! @/ d - listSET_LIST_INTEGRITY_CHECK_2_VALUE(pxList );: c+ [) R! a! ^
- }
复制代码
, 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. }
( 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
- void vListInitialiseItem( ListItem_t * const pxItem ); ~2 R7 l/ V. s/ v/ i3 _
- {
, `' V1 Y, V0 Q% e8 R1 e+ f - pxItem->pvContainer = NULL;
" ^, [( z s" k
# P2 C4 \$ C% J1 H' Q6 y9 U- /*设置为已知值,用于检测列表项数据是否完整*/ g, N$ }+ i! b! J; u, w# ]
- listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );5 f7 F, Z V9 [* ~$ u( v* {
- listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem );
5 R8 b5 q) K3 c# E) e/ V: T; H; I' H - }
复制代码
( 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
- void vListInsert( List_t * const pxList,
9 K5 x" r1 M5 K- e' M( Y/ F2 x - ListItem_t * const pxNewListItem )
8 Y8 l+ K2 O: L" x7 L* F - {
# q! r9 ] }7 U5 c" i6 w: y - ListItem_t * pxIterator;; w S! q1 {6 b! I+ U
- const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;- s) w+ e2 ?7 F& X& m" T4 G
/ J$ u( d) D. Y- /* 检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/0 l& G7 n9 R" Y- A, |9 S
- /* Only effective when configASSERT() is also defined, these tests may catch5 Q1 D5 d2 J) y, |
- * the list data structures being overwritten in memory. They will not catch( M. {) _6 P, j+ v
- * data errors caused by incorrect configuration or use of FreeRTOS. */
/ a) v, a0 q4 V: o) u( [" n. c - listTEST_LIST_INTEGRITY( pxList );3 R9 M, N; k4 M2 V$ z
- listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
& k4 [! c& H+ @7 L2 R: n7 m
& a2 F/ x1 t& \6 A8 P" v, _6 W- /* Insert the new list item into the list, sorted in xItemValue order.
+ N5 D' _1 D' ?& q( @. \ - *
3 |! L* a1 P# u* ~% F- h5 z4 C/ A - * If the list already contains a list item with the same item value then the
4 z; w: i, L2 u! H3 u - * new list item should be placed after it. This ensures that TCBs which are" j0 c$ k; B% E; r, S, T+ s
- * stored in ready lists (all of which have the same xItemValue value) get a) g( J$ U1 @1 I8 X) v
- * 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 - * the iteration loop below will not end. Therefore the value is checked
y, w. ~, W4 W, W - * first, and the algorithm slightly modified if necessary. */0 p7 F: }4 j. h0 K% F- T3 s9 [
- /*将新的列表项插入到列表,根据xItemValue的值升序插入列表。*/
6 O% S! |3 F# u - if( xValueOfInsertion == portMAX_DELAY )( b* {7 ~7 ~; L7 \- c" G# G) |- Z) g
- {
( S- Y9 D0 [/ I. } Z: E/ B$ l2 z - pxIterator = pxList->xListEnd.pxPrevious;
! S' x& z! p) ~1 O8 a' n - }
* ~6 v% U/ n2 o9 g" S9 G2 z; f. e5 n - else* K# M2 t0 ]! W
- {, {, x' b& A! }- ^0 v+ l' g
- /* *** NOTE ***********************************************************( ]6 c# d$ x8 L' a8 z5 B; _
- * If you find your application is crashing here then likely causes are
B8 }% T( U8 Q S9 H" V8 a$ Z - * listed below. In addition see for4 ?7 u. O. J2 w2 V
- * more tips, and ensure configASSERT() is defined!
( T( i. q6 r1 P3 T0 ] - * R/ B. z6 H8 F
- *: \; P3 K1 {0 W t
- * 1) Stack overflow -, e$ \1 W" }% \, B3 f
- * see
$ ?& v0 T0 S* o+ m5 \9 I( w - * 2) Incorrect interrupt priority assignment, especially on Cortex-M. @! R; C: N' s8 j: p4 J' |/ P
- * parts where numerically high priority values denote low actual
: D( U! |8 [/ |2 Q5 e6 M - * interrupt priorities, which can seem counter intuitive. See
" o8 W2 d: A6 e! V1 w8 F0 ^ - * and the definition2 }) G& Q% s% {( f) C, V
- * of configMAX_SYSCALL_INTERRUPT_PRIORITY on
s' h- X) z6 H9 W5 z - *
) i1 q) s; {! i$ I& @( g; D4 a - * 3) Calling an API function from within a critical section or when' {( o& B6 u/ Y8 L( j% k' f! w
- * the scheduler is suspended, or calling an API function that does
8 j `6 V- q7 Z+ D8 R - * not end in "FromISR" from an interrupt.
" E# ?% |2 N$ K6 K, X8 a2 | - * 4) Using a queue or semaphore before it has been initialised or1 p1 B; W6 U8 B+ B- T: O
- * before the scheduler has been started (are interrupts firing# ]2 J8 k" x, J
- * before vTaskStartScheduler() has been called?).
' R( Z* z0 `; X* v% j - **********************************************************************/
* H# t7 P' [: I4 A0 ^- s0 Z - + g' }4 n y, T' D6 O! H
- //xListEnd表示列表的末尾
+ i% f8 L% m* l - 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
- {4 N( h. Y& o6 L: G
- /* There is nothing to do here, just iterating to the wanted. n$ ~8 b% }% G4 J3 z
- * insertion position. */0 u) [- J: f" U6 J7 U# S
- }
9 v6 W. J) i4 \' q+ S - }* e. J0 W. G) ]- w7 B# S
5 {$ ^/ {4 W! b, Y, k8 N# O; N- pxNewListItem->pxNext = pxIterator->pxNext; s t" g6 j) j, z- |
- pxNewListItem->pxNext->pxPrevious = pxNewListItem;
~- G! C5 W2 P' X1 H5 D - pxNewListItem->pxPrevious = pxIterator;
$ I2 Z" O% H2 ^8 g6 X( L - pxIterator->pxNext = pxNewListItem;( {) \( X" _; O) k$ [0 `+ R8 j$ i" q
% x, `& _0 Y; ]1 L+ e- /* Remember which list the item is in. This allows fast removal of the
% R+ @. F2 R/ w) R) y' X6 P - * item later. */
+ g8 K/ M$ {% \6 i% u% p% v9 p - pxNewListItem->pxContainer = pxList;, O# g8 U& W' M7 h
F( A6 ?( ~1 u- {2 G* J* a% f& K4 H- ( pxList->uxNumberOfItems )++;( w$ z' ?) x% j0 x1 X2 L
- }
复制代码
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 _- void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
5 M8 E8 |4 e* \; A - {' [7 e4 Q4 a8 y+ a
- ListItem_t* const pxIndex = pxList->pxIndex;
5 o" I8 e+ A# @7 V6 g
, s4 T/ p0 b4 T3 R4 c+ @8 j3 N- /*检查列表和列表项数据的完整性,仅当configASSERT()定义时有效。*/
0 M J& q; ^7 j* ^) ~/ _% K - listTEST_LIST_INTEGRITY( pxList );
( Q$ o7 W6 ^6 i e- z6 A - listTEST_LIST_ITEM_INTEGRITY(pxNewListItem );- K6 A0 R5 @: C; T; J$ a9 d
- 7 y* r( z+ \/ [6 W- _
- /*向列表中插入新的列表项*/
4 S( x; i& n; c- E* V0 M+ ^) q - pxNewListItem->pxNext = pxIndex;
6 u- u% F+ p" ^/ ?* x" G8 R - pxNewListItem->pxPrevious =pxIndex->pxPrevious;
5 J. P" y3 K' A
2 V2 v: B4 F4 A& W- mtCOVERAGE_TEST_DELAY();
5 Y2 |) w6 x9 G& [* b( J& p
/ }/ b$ { B2 ~+ A# w( W. e$ ~- pxIndex->pxPrevious->pxNext =pxNewListItem;7 b- } Z9 g* i5 d5 }; l
- pxIndex->pxPrevious = pxNewListItem;
6 _; _ z% ?$ x1 b+ p& k - 2 S+ N: f. v8 J
- pxNewListItem->pvContainer = ( void* ) pxList;
q; Y' M9 [; J - $ [! d. \7 b0 d2 W0 `7 x( G
- ( pxList->uxNumberOfItems )++;1 ]! ~2 D! I; C4 }- N* i: o
- }
复制代码
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- UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )6 X& c" e* L( f" [* K5 N3 v6 C
- {
3 O& U. g# K, b- w$ a$ q+ W: p. d* t - /* The list item knows which list it is in. Obtain the list from the list0 |* w* g. ?6 V, W5 f7 @
- * item. */% J$ \# {' q5 q& t: ^- \
- List_t * const pxList = pxItemToRemove->pxContainer;7 |" x, i: m7 O. e7 e6 i
- ! `4 z6 [! C2 \8 ] p
- pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;2 b5 G5 r, T1 B* F9 m4 Q
- pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
9 n, Z- O+ N9 Y7 v - $ ^4 ?6 \, g5 F" z1 v1 a/ b5 [
- /* Only used during decision coverage testing. */
/ g+ c w3 `# V8 m$ Q; d2 u- v4 f - mtCOVERAGE_TEST_DELAY();, x4 N( T2 N) B9 ?. Z7 y+ i6 d2 d
- 8 {4 B' |3 E8 S
- /* Make sure the index is left pointing to a valid item. */. N0 n) N9 h9 M
- if( pxList->pxIndex == pxItemToRemove )' ^4 l- G% r1 N3 }: O
- {
# C: r# _0 f+ v. Y) n - pxList->pxIndex = pxItemToRemove->pxPrevious;+ ?7 R6 I3 }+ z" L
- }3 T: g, V4 v. s6 v+ F$ J
- else
0 U i" z( m) |$ G; ~0 h, T% o- D - {
; ?3 }; H3 A6 w9 ^) j# m1 ?- _ - mtCOVERAGE_TEST_MARKER();$ `7 R" ?; q1 C
- }
+ g8 F$ G& z; _2 }+ n. T
' [( ?; u" u7 O- pxItemToRemove->pxContainer = NULL;
( ~. Y" v* N( w$ b5 ? - ( pxList->uxNumberOfItems )--;- C0 ~2 H3 c6 R8 k9 I% F
- # M4 Q& V) b1 m4 D& D5 v1 W
- return pxList->uxNumberOfItems;0 _8 h1 V! O) X1 K5 q4 v
- }
复制代码
' l3 R. N$ J$ t" {: {5.6 列表遍历
& |! X1 Y# _ n- u0 s. o- #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \. K' H" `0 [. Z) E; l: I
- { \
9 A2 [; e) S- R; H - List_t * const pxConstList = ( pxList ); \. `4 K! ~, n6 i
- /* Increment the index to the next item and return the item, ensuring */ \
; R; P. w5 k: \; K" K) L - /* we don't return the marker used at the end of the list. */ \# x; b) E' o2 N" I$ V1 [( \
- ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \1 V# M4 r' j2 k6 _$ t6 P
- if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
' Q) Z8 _% L+ V2 f - { \
$ D) r7 V4 J; r - ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \; g; U/ C6 z, D$ `& b/ F' e# r
- } \
3 R( }1 Z& G/ B5 p1 c2 {1 y, f - ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
* m9 A" _; l& U+ B: | } y - }
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 |