]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/list.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / list.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 #include <stdlib.h>\r
72 #include "FreeRTOS.h"\r
73 #include "list.h"\r
74 \r
75 /*-----------------------------------------------------------\r
76  * PUBLIC LIST API documented in list.h\r
77  *----------------------------------------------------------*/\r
78 \r
79 void vListInitialise( List_t * const pxList )\r
80 {\r
81         /* The list structure contains a list item which is used to mark the\r
82         end of the list.  To initialise the list the list end is inserted\r
83         as the only list entry. */\r
84         pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );                       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
85 \r
86         /* The list end value is the highest possible value in the list to\r
87         ensure it remains at the end of the list. */\r
88         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
89 \r
90         /* The list end next and previous pointers point to itself so we know\r
91         when the list is empty. */\r
92         pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
93         pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );/*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
94 \r
95         pxList->uxNumberOfItems = ( UBaseType_t ) 0U;\r
96 \r
97         /* Write known values into the list if\r
98         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */\r
99         listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );\r
100         listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vListInitialiseItem( ListItem_t * const pxItem )\r
105 {\r
106         /* Make sure the list item is not recorded as being on a list. */\r
107         pxItem->pvContainer = NULL;\r
108 \r
109         /* Write known values into the list item if\r
110         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */\r
111         listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );\r
112         listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )\r
117 {\r
118 ListItem_t * const pxIndex = pxList->pxIndex;\r
119 \r
120         /* Only effective when configASSERT() is also defined, these tests may catch\r
121         the list data structures being overwritten in memory.  They will not catch\r
122         data errors caused by incorrect configuration or use of FreeRTOS. */\r
123         listTEST_LIST_INTEGRITY( pxList );\r
124         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );\r
125 \r
126         /* Insert a new list item into pxList, but rather than sort the list,\r
127         makes the new list item the last item to be removed by a call to\r
128         listGET_OWNER_OF_NEXT_ENTRY(). */\r
129         pxNewListItem->pxNext = pxIndex;\r
130         pxNewListItem->pxPrevious = pxIndex->pxPrevious;\r
131 \r
132         /* Only used during decision coverage testing. */\r
133         mtCOVERAGE_TEST_DELAY();\r
134 \r
135         pxIndex->pxPrevious->pxNext = pxNewListItem;\r
136         pxIndex->pxPrevious = pxNewListItem;\r
137 \r
138         /* Remember which list the item is in. */\r
139         pxNewListItem->pvContainer = ( void * ) pxList;\r
140 \r
141         ( pxList->uxNumberOfItems )++;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )\r
146 {\r
147 ListItem_t *pxIterator;\r
148 const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;\r
149 \r
150         /* Only effective when configASSERT() is also defined, these tests may catch\r
151         the list data structures being overwritten in memory.  They will not catch\r
152         data errors caused by incorrect configuration or use of FreeRTOS. */\r
153         listTEST_LIST_INTEGRITY( pxList );\r
154         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );\r
155 \r
156         /* Insert the new list item into the list, sorted in xItemValue order.\r
157 \r
158         If the list already contains a list item with the same item value then the\r
159         new list item should be placed after it.  This ensures that TCB's which are\r
160         stored in ready lists (all of which have the same xItemValue value) get a\r
161         share of the CPU.  However, if the xItemValue is the same as the back marker\r
162         the iteration loop below will not end.  Therefore the value is checked\r
163         first, and the algorithm slightly modified if necessary. */\r
164         if( xValueOfInsertion == portMAX_DELAY )\r
165         {\r
166                 pxIterator = pxList->xListEnd.pxPrevious;\r
167         }\r
168         else\r
169         {\r
170                 /* *** NOTE ***********************************************************\r
171                 If you find your application is crashing here then likely causes are\r
172                 listed below.  In addition see http://www.freertos.org/FAQHelp.html for\r
173                 more tips, and ensure configASSERT() is defined!\r
174                 http://www.freertos.org/a00110.html#configASSERT\r
175 \r
176                         1) Stack overflow -\r
177                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
178                         2) Incorrect interrupt priority assignment, especially on Cortex-M\r
179                            parts where numerically high priority values denote low actual\r
180                            interrupt priorities, which can seem counter intuitive.  See\r
181                            http://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition\r
182                            of configMAX_SYSCALL_INTERRUPT_PRIORITY on\r
183                            http://www.freertos.org/a00110.html\r
184                         3) Calling an API function from within a critical section or when\r
185                            the scheduler is suspended, or calling an API function that does\r
186                            not end in "FromISR" from an interrupt.\r
187                         4) Using a queue or semaphore before it has been initialised or\r
188                            before the scheduler has been started (are interrupts firing\r
189                            before vTaskStartScheduler() has been called?).\r
190                 **********************************************************************/\r
191 \r
192                 for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
193                 {\r
194                         /* There is nothing to do here, just iterating to the wanted\r
195                         insertion position. */\r
196                 }\r
197         }\r
198 \r
199         pxNewListItem->pxNext = pxIterator->pxNext;\r
200         pxNewListItem->pxNext->pxPrevious = pxNewListItem;\r
201         pxNewListItem->pxPrevious = pxIterator;\r
202         pxIterator->pxNext = pxNewListItem;\r
203 \r
204         /* Remember which list the item is in.  This allows fast removal of the\r
205         item later. */\r
206         pxNewListItem->pvContainer = ( void * ) pxList;\r
207 \r
208         ( pxList->uxNumberOfItems )++;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )\r
213 {\r
214 /* The list item knows which list it is in.  Obtain the list from the list\r
215 item. */\r
216 List_t * const pxList = ( List_t * ) pxItemToRemove->pvContainer;\r
217 \r
218         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
219         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
220 \r
221         /* Only used during decision coverage testing. */\r
222         mtCOVERAGE_TEST_DELAY();\r
223 \r
224         /* Make sure the index is left pointing to a valid item. */\r
225         if( pxList->pxIndex == pxItemToRemove )\r
226         {\r
227                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
228         }\r
229         else\r
230         {\r
231                 mtCOVERAGE_TEST_MARKER();\r
232         }\r
233 \r
234         pxItemToRemove->pvContainer = NULL;\r
235         ( pxList->uxNumberOfItems )--;\r
236 \r
237         return pxList->uxNumberOfItems;\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r