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