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