]> git.sur5r.net Git - freertos/blob - Source/include/list.h
Ready for V5.2.0 release.
[freertos] / Source / include / list.h
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53  * This is the list implementation used by the scheduler.  While it is tailored\r
54  * heavily for the schedulers needs, it is also available for use by\r
55  * application code.\r
56  *\r
57  * xLists can only store pointers to xListItems.  Each xListItem contains a\r
58  * numeric value (xItemValue).  Most of the time the lists are sorted in\r
59  * descending item value order.\r
60  *\r
61  * Lists are created already containing one list item.  The value of this\r
62  * item is the maximum possible that can be stored, it is therefore always at\r
63  * the end of the list and acts as a marker.  The list member pxHead always\r
64  * points to this marker - even though it is at the tail of the list.  This\r
65  * is because the tail contains a wrap back pointer to the true head of\r
66  * the list.\r
67  *\r
68  * In addition to it's value, each list item contains a pointer to the next\r
69  * item in the list (pxNext), a pointer to the list it is in (pxContainer)\r
70  * and a pointer to back to the object that contains it.  These later two\r
71  * pointers are included for efficiency of list manipulation.  There is\r
72  * effectively a two way link between the object containing the list item and\r
73  * the list item itself.\r
74  *\r
75  *\r
76  * \page ListIntroduction List Implementation\r
77  * \ingroup FreeRTOSIntro\r
78  */\r
79 \r
80 /*\r
81         Changes from V4.3.1\r
82 \r
83         + Included local const within listGET_OWNER_OF_NEXT_ENTRY() to assist\r
84           compiler with optimisation.  Thanks B.R.\r
85 */\r
86 \r
87 #ifndef LIST_H\r
88 #define LIST_H\r
89 \r
90 #ifdef __cplusplus\r
91 extern "C" {\r
92 #endif\r
93 /*\r
94  * Definition of the only type of object that a list can contain.\r
95  */\r
96 struct xLIST_ITEM\r
97 {\r
98         portTickType xItemValue;                                /*< The value being listed.  In most cases this is used to sort the list in descending order. */\r
99         volatile struct xLIST_ITEM * pxNext;    /*< Pointer to the next xListItem in the list. */\r
100         volatile struct xLIST_ITEM * pxPrevious;/*< Pointer to the previous xListItem in the list. */\r
101         void * pvOwner;                                                 /*< Pointer to the object (normally a TCB) that contains the list item.  There is therefore a two way link between the object containing the list item and the list item itself. */\r
102         void * pvContainer;                                             /*< Pointer to the list in which this list item is placed (if any). */\r
103 };\r
104 typedef struct xLIST_ITEM xListItem;            /* For some reason lint wants this as two separate definitions. */\r
105 \r
106 struct xMINI_LIST_ITEM\r
107 {\r
108         portTickType xItemValue;\r
109         volatile struct xLIST_ITEM *pxNext;\r
110         volatile struct xLIST_ITEM *pxPrevious;\r
111 };\r
112 typedef struct xMINI_LIST_ITEM xMiniListItem;\r
113 \r
114 /*\r
115  * Definition of the type of queue used by the scheduler.\r
116  */\r
117 typedef struct xLIST\r
118 {\r
119         volatile unsigned portBASE_TYPE uxNumberOfItems;\r
120         volatile xListItem * pxIndex;                   /*< Used to walk through the list.  Points to the last item returned by a call to pvListGetOwnerOfNextEntry (). */\r
121         volatile xMiniListItem xListEnd;                /*< List item that contains the maximum possible item value meaning it is always at the end of the list and is therefore used as a marker. */\r
122 } xList;\r
123 \r
124 /*\r
125  * Access macro to set the owner of a list item.  The owner of a list item\r
126  * is the object (usually a TCB) that contains the list item.\r
127  *\r
128  * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER\r
129  * \ingroup LinkedList\r
130  */\r
131 #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner )          ( pxListItem )->pvOwner = ( void * ) pxOwner\r
132 \r
133 /*\r
134  * Access macro to set the value of the list item.  In most cases the value is\r
135  * used to sort the list in descending order.\r
136  *\r
137  * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE\r
138  * \ingroup LinkedList\r
139  */\r
140 #define listSET_LIST_ITEM_VALUE( pxListItem, xValue )           ( pxListItem )->xItemValue = xValue\r
141 \r
142 /*\r
143  * Access macro the retrieve the value of the list item.  The value can\r
144  * represent anything - for example a the priority of a task, or the time at\r
145  * which a task should be unblocked.\r
146  *\r
147  * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE\r
148  * \ingroup LinkedList\r
149  */\r
150 #define listGET_LIST_ITEM_VALUE( pxListItem )                           ( ( pxListItem )->xItemValue )\r
151 \r
152 /*\r
153  * Access macro to determine if a list contains any items.  The macro will\r
154  * only have the value true if the list is empty.\r
155  *\r
156  * \page listLIST_IS_EMPTY listLIST_IS_EMPTY\r
157  * \ingroup LinkedList\r
158  */\r
159 #define listLIST_IS_EMPTY( pxList )                             ( ( pxList )->uxNumberOfItems == ( unsigned portBASE_TYPE ) 0 )\r
160 \r
161 /*\r
162  * Access macro to return the number of items in the list.\r
163  */\r
164 #define listCURRENT_LIST_LENGTH( pxList )               ( ( pxList )->uxNumberOfItems )\r
165 \r
166 /*\r
167  * Access function to obtain the owner of the next entry in a list.\r
168  *\r
169  * The list member pxIndex is used to walk through a list.  Calling\r
170  * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list\r
171  * and returns that entries pxOwner parameter.  Using multiple calls to this\r
172  * function it is therefore possible to move through every item contained in\r
173  * a list.\r
174  *\r
175  * The pxOwner parameter of a list item is a pointer to the object that owns\r
176  * the list item.  In the scheduler this is normally a task control block.\r
177  * The pxOwner parameter effectively creates a two way link between the list\r
178  * item and its owner.\r
179  *\r
180  * @param pxList The list from which the next item owner is to be returned.\r
181  *\r
182  * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY\r
183  * \ingroup LinkedList\r
184  */\r
185 #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList )                                                                    \\r
186 {                                                                                                                                                                               \\r
187 xList * const pxConstList = pxList;                                                                                                             \\r
188         /* Increment the index to the next item and return the item, ensuring */                        \\r
189         /* we don't return the marker used at the end of the list.  */                                          \\r
190         ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                                            \\r
191         if( ( pxConstList )->pxIndex == ( xListItem * ) &( ( pxConstList )->xListEnd ) )        \\r
192         {                                                                                                                                                                       \\r
193                 ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext;                                    \\r
194         }                                                                                                                                                                       \\r
195         pxTCB = ( pxConstList )->pxIndex->pvOwner;                                                                                      \\r
196 }\r
197 \r
198 \r
199 /*\r
200  * Access function to obtain the owner of the first entry in a list.  Lists\r
201  * are normally sorted in ascending item value order.\r
202  *\r
203  * This function returns the pxOwner member of the first item in the list.\r
204  * The pxOwner parameter of a list item is a pointer to the object that owns\r
205  * the list item.  In the scheduler this is normally a task control block.\r
206  * The pxOwner parameter effectively creates a two way link between the list\r
207  * item and its owner.\r
208  *\r
209  * @param pxList The list from which the owner of the head item is to be\r
210  * returned.\r
211  *\r
212  * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY\r
213  * \ingroup LinkedList\r
214  */\r
215 #define listGET_OWNER_OF_HEAD_ENTRY( pxList )  ( ( pxList->uxNumberOfItems != ( unsigned portBASE_TYPE ) 0 ) ? ( (&( pxList->xListEnd ))->pxNext->pvOwner ) : ( NULL ) )\r
216 \r
217 /*\r
218  * Check to see if a list item is within a list.  The list item maintains a\r
219  * "container" pointer that points to the list it is in.  All this macro does\r
220  * is check to see if the container and the list match.\r
221  *\r
222  * @param pxList The list we want to know if the list item is within.\r
223  * @param pxListItem The list item we want to know if is in the list.\r
224  * @return pdTRUE is the list item is in the list, otherwise pdFALSE.\r
225  * pointer against\r
226  */\r
227 #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( pxListItem )->pvContainer == ( void * ) pxList )\r
228 \r
229 /*\r
230  * Must be called before a list is used!  This initialises all the members\r
231  * of the list structure and inserts the xListEnd item into the list as a\r
232  * marker to the back of the list.\r
233  *\r
234  * @param pxList Pointer to the list being initialised.\r
235  *\r
236  * \page vListInitialise vListInitialise\r
237  * \ingroup LinkedList\r
238  */\r
239 void vListInitialise( xList *pxList );\r
240 \r
241 /*\r
242  * Must be called before a list item is used.  This sets the list container to\r
243  * null so the item does not think that it is already contained in a list.\r
244  *\r
245  * @param pxItem Pointer to the list item being initialised.\r
246  *\r
247  * \page vListInitialiseItem vListInitialiseItem\r
248  * \ingroup LinkedList\r
249  */\r
250 void vListInitialiseItem( xListItem *pxItem );\r
251 \r
252 /*\r
253  * Insert a list item into a list.  The item will be inserted into the list in\r
254  * a position determined by its item value (descending item value order).\r
255  *\r
256  * @param pxList The list into which the item is to be inserted.\r
257  *\r
258  * @param pxNewListItem The item to that is to be placed in the list.\r
259  *\r
260  * \page vListInsert vListInsert\r
261  * \ingroup LinkedList\r
262  */\r
263 void vListInsert( xList *pxList, xListItem *pxNewListItem );\r
264 \r
265 /*\r
266  * Insert a list item into a list.  The item will be inserted in a position\r
267  * such that it will be the last item within the list returned by multiple\r
268  * calls to listGET_OWNER_OF_NEXT_ENTRY.\r
269  *\r
270  * The list member pvIndex is used to walk through a list.  Calling\r
271  * listGET_OWNER_OF_NEXT_ENTRY increments pvIndex to the next item in the list.\r
272  * Placing an item in a list using vListInsertEnd effectively places the item\r
273  * in the list position pointed to by pvIndex.  This means that every other\r
274  * item within the list will be returned by listGET_OWNER_OF_NEXT_ENTRY before\r
275  * the pvIndex parameter again points to the item being inserted.\r
276  *\r
277  * @param pxList The list into which the item is to be inserted.\r
278  *\r
279  * @param pxNewListItem The list item to be inserted into the list.\r
280  *\r
281  * \page vListInsertEnd vListInsertEnd\r
282  * \ingroup LinkedList\r
283  */\r
284 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem );\r
285 \r
286 /*\r
287  * Remove an item from a list.  The list item has a pointer to the list that\r
288  * it is in, so only the list item need be passed into the function.\r
289  *\r
290  * @param vListRemove The item to be removed.  The item will remove itself from\r
291  * the list pointed to by it's pxContainer parameter.\r
292  *\r
293  * \page vListRemove vListRemove\r
294  * \ingroup LinkedList\r
295  */\r
296 void vListRemove( xListItem *pxItemToRemove );\r
297 \r
298 #ifdef __cplusplus\r
299 }\r
300 #endif\r
301 \r
302 #endif\r
303 \r