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