]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/list.c
Update version number in preparation for official V8.2.0 release.
[freertos] / FreeRTOS / Source / list.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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         pxIndex->pxPrevious->pxNext = pxNewListItem;\r
132         pxIndex->pxPrevious = pxNewListItem;\r
133 \r
134         /* Remember which list the item is in. */\r
135         pxNewListItem->pvContainer = ( void * ) pxList;\r
136 \r
137         ( pxList->uxNumberOfItems )++;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )\r
142 {\r
143 ListItem_t *pxIterator;\r
144 const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;\r
145 \r
146         /* Only effective when configASSERT() is also defined, these tests may catch\r
147         the list data structures being overwritten in memory.  They will not catch\r
148         data errors caused by incorrect configuration or use of FreeRTOS. */\r
149         listTEST_LIST_INTEGRITY( pxList );\r
150         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );\r
151 \r
152         /* Insert the new list item into the list, sorted in xItemValue order.\r
153 \r
154         If the list already contains a list item with the same item value then the\r
155         new list item should be placed after it.  This ensures that TCB's which are\r
156         stored in ready lists (all of which have the same xItemValue value) get a\r
157         share of the CPU.  However, if the xItemValue is the same as the back marker\r
158         the iteration loop below will not end.  Therefore the value is checked\r
159         first, and the algorithm slightly modified if necessary. */\r
160         if( xValueOfInsertion == portMAX_DELAY )\r
161         {\r
162                 pxIterator = pxList->xListEnd.pxPrevious;\r
163         }\r
164         else\r
165         {\r
166                 /* *** NOTE ***********************************************************\r
167                 If you find your application is crashing here then likely causes are\r
168                 listed below.  In addition see http://www.freertos.org/FAQHelp.html for\r
169                 more tips, and ensure configASSERT() is defined!\r
170                 http://www.freertos.org/a00110.html#configASSERT\r
171 \r
172                         1) Stack overflow -\r
173                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
174                         2) Incorrect interrupt priority assignment, especially on Cortex-M\r
175                            parts where numerically high priority values denote low actual\r
176                            interrupt priorities, which can seem counter intuitive.  See\r
177                            http://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition\r
178                            of configMAX_SYSCALL_INTERRUPT_PRIORITY on\r
179                            http://www.freertos.org/a00110.html\r
180                         3) Calling an API function from within a critical section or when\r
181                            the scheduler is suspended, or calling an API function that does\r
182                            not end in "FromISR" from an interrupt.\r
183                         4) Using a queue or semaphore before it has been initialised or\r
184                            before the scheduler has been started (are interrupts firing\r
185                            before vTaskStartScheduler() has been called?).\r
186                 **********************************************************************/\r
187 \r
188                 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
189                 {\r
190                         /* There is nothing to do here, just iterating to the wanted\r
191                         insertion position. */\r
192                 }\r
193         }\r
194 \r
195         pxNewListItem->pxNext = pxIterator->pxNext;\r
196         pxNewListItem->pxNext->pxPrevious = pxNewListItem;\r
197         pxNewListItem->pxPrevious = pxIterator;\r
198         pxIterator->pxNext = pxNewListItem;\r
199 \r
200         /* Remember which list the item is in.  This allows fast removal of the\r
201         item later. */\r
202         pxNewListItem->pvContainer = ( void * ) pxList;\r
203 \r
204         ( pxList->uxNumberOfItems )++;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )\r
209 {\r
210 /* The list item knows which list it is in.  Obtain the list from the list\r
211 item. */\r
212 List_t * const pxList = ( List_t * ) pxItemToRemove->pvContainer;\r
213 \r
214         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
215         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
216 \r
217         /* Make sure the index is left pointing to a valid item. */\r
218         if( pxList->pxIndex == pxItemToRemove )\r
219         {\r
220                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
221         }\r
222         else\r
223         {\r
224                 mtCOVERAGE_TEST_MARKER();\r
225         }\r
226 \r
227         pxItemToRemove->pvContainer = NULL;\r
228         ( pxList->uxNumberOfItems )--;\r
229 \r
230         return pxList->uxNumberOfItems;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r