uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
list.h
Go to the documentation of this file.
1 /*
2  FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  ***************************************************************************
8  * *
9  * FreeRTOS provides completely free yet professionally developed, *
10  * robust, strictly quality controlled, supported, and cross *
11  * platform software that has become a de facto standard. *
12  * *
13  * Help yourself get started quickly and support the FreeRTOS *
14  * project by purchasing a FreeRTOS tutorial book, reference *
15  * manual, or both from: http://www.FreeRTOS.org/Documentation *
16  * *
17  * Thank you! *
18  * *
19  ***************************************************************************
20 
21  This file is part of the FreeRTOS distribution.
22 
23  FreeRTOS is free software; you can redistribute it and/or modify it under
24  the terms of the GNU General Public License (version 2) as published by the
25  Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
26 
27  >>! NOTE: The modification to the GPL is included to allow you to distribute
28  >>! a combined work that includes FreeRTOS without being obliged to provide
29  >>! the source code for proprietary components outside of the FreeRTOS
30  >>! kernel.
31 
32  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
33  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34  FOR A PARTICULAR PURPOSE. Full license text is available from the following
35  link: http://www.freertos.org/a00114.html
36 
37  1 tab == 4 spaces!
38 
39  ***************************************************************************
40  * *
41  * Having a problem? Start by reading the FAQ "My application does *
42  * not run, what could be wrong?" *
43  * *
44  * http://www.FreeRTOS.org/FAQHelp.html *
45  * *
46  ***************************************************************************
47 
48  http://www.FreeRTOS.org - Documentation, books, training, latest versions,
49  license and Real Time Engineers Ltd. contact details.
50 
51  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
52  including FreeRTOS+Trace - an indispensable productivity tool, a DOS
53  compatible FAT file system, and our tiny thread aware UDP/IP stack.
54 
55  http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
56  Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
57  licenses offer ticketed support, indemnification and middleware.
58 
59  http://www.SafeRTOS.com - High Integrity Systems also provide a safety
60  engineered and independently SIL3 certified version for use in safety and
61  mission critical applications that require provable dependability.
62 
63  1 tab == 4 spaces!
64 */
65 
66 /*
67  * This is the list implementation used by the scheduler. While it is tailored
68  * heavily for the schedulers needs, it is also available for use by
69  * application code.
70  *
71  * xLists can only store pointers to xListItems. Each xListItem contains a
72  * numeric value (xItemValue). Most of the time the lists are sorted in
73  * descending item value order.
74  *
75  * Lists are created already containing one list item. The value of this
76  * item is the maximum possible that can be stored, it is therefore always at
77  * the end of the list and acts as a marker. The list member pxHead always
78  * points to this marker - even though it is at the tail of the list. This
79  * is because the tail contains a wrap back pointer to the true head of
80  * the list.
81  *
82  * In addition to it's value, each list item contains a pointer to the next
83  * item in the list (pxNext), a pointer to the list it is in (pxContainer)
84  * and a pointer to back to the object that contains it. These later two
85  * pointers are included for efficiency of list manipulation. There is
86  * effectively a two way link between the object containing the list item and
87  * the list item itself.
88  *
89  *
90  * \page ListIntroduction List Implementation
91  * \ingroup FreeRTOSIntro
92  */
93 
94 
95 #ifndef LIST_H
96 #define LIST_H
97 
98 /*
99  * The list structure members are modified from within interrupts, and therefore
100  * by rights should be declared volatile. However, they are only modified in a
101  * functionally atomic way (within critical sections of with the scheduler
102  * suspended) and are either passed by reference into a function or indexed via
103  * a volatile variable. Therefore, in all use cases tested so far, the volatile
104  * qualifier can be omitted in order to provide a moderate performance
105  * improvement without adversely affecting functional behaviour. The assembly
106  * instructions generated by the IAR, ARM and GCC compilers when the respective
107  * compiler's options were set for maximum optimisation has been inspected and
108  * deemed to be as intended. That said, as compiler technology advances, and
109  * especially if aggressive cross module optimisation is used (a use case that
110  * has not been exercised to any great extend) then it is feasible that the
111  * volatile qualifier will be needed for correct optimisation. It is expected
112  * that a compiler removing essential code because, without the volatile
113  * qualifier on the list structure members and with aggressive cross module
114  * optimisation, the compiler deemed the code unnecessary will result in
115  * complete and obvious failure of the scheduler. If this is ever experienced
116  * then the volatile qualifier can be inserted in the relevant places within the
117  * list structures by simply defining configLIST_VOLATILE to volatile in
118  * FreeRTOSConfig.h (as per the example at the bottom of this comment block).
119  * If configLIST_VOLATILE is not defined then the preprocessor directives below
120  * will simply #define configLIST_VOLATILE away completely.
121  *
122  * To use volatile list structure members then add the following line to
123  * FreeRTOSConfig.h (without the quotes):
124  * "#define configLIST_VOLATILE volatile"
125  */
126 #ifndef configLIST_VOLATILE
127  #define configLIST_VOLATILE
128 #endif /* configSUPPORT_CROSS_MODULE_OPTIMISATION */
129 
130 #ifdef __cplusplus
131 extern "C" {
132 #endif
133 /*
134  * Definition of the only type of object that a list can contain.
135  */
137 {
138  configLIST_VOLATILE portTickType xItemValue; /*< The value being listed. In most cases this is used to sort the list in descending order. */
139  struct xLIST_ITEM * configLIST_VOLATILE pxNext; /*< Pointer to the next xListItem in the list. */
140  struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;/*< Pointer to the previous xListItem in the list. */
141  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. */
142  void * configLIST_VOLATILE pvContainer; /*< Pointer to the list in which this list item is placed (if any). */
143 };
144 typedef struct xLIST_ITEM xListItem; /* For some reason lint wants this as two separate definitions. */
145 
147 {
151 };
153 
154 /*
155  * Definition of the type of queue used by the scheduler.
156  */
157 typedef struct xLIST
158 {
160  xListItem * configLIST_VOLATILE pxIndex; /*< Used to walk through the list. Points to the last item returned by a call to pvListGetOwnerOfNextEntry (). */
161  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. */
162 } xList;
163 
164 /*
165  * Access macro to set the owner of a list item. The owner of a list item
166  * is the object (usually a TCB) that contains the list item.
167  *
168  * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
169  * \ingroup LinkedList
170  */
171 #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )
172 
173 /*
174  * Access macro to get the owner of a list item. The owner of a list item
175  * is the object (usually a TCB) that contains the list item.
176  *
177  * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
178  * \ingroup LinkedList
179  */
180 #define listGET_LIST_ITEM_OWNER( pxListItem ) ( pxListItem )->pvOwner
181 
182 /*
183  * Access macro to set the value of the list item. In most cases the value is
184  * used to sort the list in descending order.
185  *
186  * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
187  * \ingroup LinkedList
188  */
189 #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( ( pxListItem )->xItemValue = ( xValue ) )
190 
191 /*
192  * Access macro to retrieve the value of the list item. The value can
193  * represent anything - for example a the priority of a task, or the time at
194  * which a task should be unblocked.
195  *
196  * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
197  * \ingroup LinkedList
198  */
199 #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )
200 
201 /*
202  * Access macro the retrieve the value of the list item at the head of a given
203  * list.
204  *
205  * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
206  * \ingroup LinkedList
207  */
208 #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( (&( ( pxList )->xListEnd ))->pxNext->xItemValue )
209 
210 /*
211  * Access macro to determine if a list contains any items. The macro will
212  * only have the value true if the list is empty.
213  *
214  * \page listLIST_IS_EMPTY listLIST_IS_EMPTY
215  * \ingroup LinkedList
216  */
217 #define listLIST_IS_EMPTY( pxList ) ( ( portBASE_TYPE ) ( ( pxList )->uxNumberOfItems == ( unsigned portBASE_TYPE ) 0 ) )
218 
219 /*
220  * Access macro to return the number of items in the list.
221  */
222 #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )
223 
224 /*
225  * Access function to obtain the owner of the next entry in a list.
226  *
227  * The list member pxIndex is used to walk through a list. Calling
228  * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
229  * and returns that entries pxOwner parameter. Using multiple calls to this
230  * function it is therefore possible to move through every item contained in
231  * a list.
232  *
233  * The pxOwner parameter of a list item is a pointer to the object that owns
234  * the list item. In the scheduler this is normally a task control block.
235  * The pxOwner parameter effectively creates a two way link between the list
236  * item and its owner.
237  *
238  * @param pxList The list from which the next item owner is to be returned.
239  *
240  * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY
241  * \ingroup LinkedList
242  */
243 #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
244 { \
245 xList * const pxConstList = ( pxList ); \
246  /* Increment the index to the next item and return the item, ensuring */ \
247  /* we don't return the marker used at the end of the list. */ \
248  ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
249  if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
250  { \
251  ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
252  } \
253  ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
254 }
255 
256 
257 /*
258  * Access function to obtain the owner of the first entry in a list. Lists
259  * are normally sorted in ascending item value order.
260  *
261  * This function returns the pxOwner member of the first item in the list.
262  * The pxOwner parameter of a list item is a pointer to the object that owns
263  * the list item. In the scheduler this is normally a task control block.
264  * The pxOwner parameter effectively creates a two way link between the list
265  * item and its owner.
266  *
267  * @param pxList The list from which the owner of the head item is to be
268  * returned.
269  *
270  * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY
271  * \ingroup LinkedList
272  */
273 #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( (&( ( pxList )->xListEnd ))->pxNext->pvOwner )
274 
275 /*
276  * Check to see if a list item is within a list. The list item maintains a
277  * "container" pointer that points to the list it is in. All this macro does
278  * is check to see if the container and the list match.
279  *
280  * @param pxList The list we want to know if the list item is within.
281  * @param pxListItem The list item we want to know if is in the list.
282  * @return pdTRUE is the list item is in the list, otherwise pdFALSE.
283  * pointer against
284  */
285 #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( portBASE_TYPE ) ( ( pxListItem )->pvContainer == ( void * ) ( pxList ) ) )
286 
287 /*
288  * Return the list a list item is contained within (referenced from).
289  *
290  * @param pxListItem The list item being queried.
291  * @return A pointer to the xList object that references the pxListItem
292  */
293 #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pvContainer )
294 
295 /*
296  * This provides a crude means of knowing if a list has been initialised, as
297  * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
298  * function.
299  */
300 #define listLIST_IS_INITIALISED( pxList ) ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )
301 
302 /*
303  * Must be called before a list is used! This initialises all the members
304  * of the list structure and inserts the xListEnd item into the list as a
305  * marker to the back of the list.
306  *
307  * @param pxList Pointer to the list being initialised.
308  *
309  * \page vListInitialise vListInitialise
310  * \ingroup LinkedList
311  */
312 void vListInitialise( xList * const pxList );
313 
314 /*
315  * Must be called before a list item is used. This sets the list container to
316  * null so the item does not think that it is already contained in a list.
317  *
318  * @param pxItem Pointer to the list item being initialised.
319  *
320  * \page vListInitialiseItem vListInitialiseItem
321  * \ingroup LinkedList
322  */
323 void vListInitialiseItem( xListItem * const pxItem );
324 
325 /*
326  * Insert a list item into a list. The item will be inserted into the list in
327  * a position determined by its item value (descending item value order).
328  *
329  * @param pxList The list into which the item is to be inserted.
330  *
331  * @param pxNewListItem The item to that is to be placed in the list.
332  *
333  * \page vListInsert vListInsert
334  * \ingroup LinkedList
335  */
336 void vListInsert( xList * const pxList, xListItem * const pxNewListItem );
337 
338 /*
339  * Insert a list item into a list. The item will be inserted in a position
340  * such that it will be the last item within the list returned by multiple
341  * calls to listGET_OWNER_OF_NEXT_ENTRY.
342  *
343  * The list member pvIndex is used to walk through a list. Calling
344  * listGET_OWNER_OF_NEXT_ENTRY increments pvIndex to the next item in the list.
345  * Placing an item in a list using vListInsertEnd effectively places the item
346  * in the list position pointed to by pvIndex. This means that every other
347  * item within the list will be returned by listGET_OWNER_OF_NEXT_ENTRY before
348  * the pvIndex parameter again points to the item being inserted.
349  *
350  * @param pxList The list into which the item is to be inserted.
351  *
352  * @param pxNewListItem The list item to be inserted into the list.
353  *
354  * \page vListInsertEnd vListInsertEnd
355  * \ingroup LinkedList
356  */
357 void vListInsertEnd( xList * const pxList, xListItem * const pxNewListItem );
358 
359 /*
360  * Remove an item from a list. The list item has a pointer to the list that
361  * it is in, so only the list item need be passed into the function.
362  *
363  * @param uxListRemove The item to be removed. The item will remove itself from
364  * the list pointed to by it's pxContainer parameter.
365  *
366  * @return The number of items that remain in the list after the list item has
367  * been removed.
368  *
369  * \page uxListRemove uxListRemove
370  * \ingroup LinkedList
371  */
372 unsigned portBASE_TYPE uxListRemove( xListItem * const pxItemToRemove );
373 
374 #ifdef __cplusplus
375 }
376 #endif
377 
378 #endif
379