]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/list.c
Improve efficiency and behaviour of vListInsertEnd().
[freertos] / FreeRTOS / Source / list.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 #include <stdlib.h>\r
77 #include "FreeRTOS.h"\r
78 #include "list.h"\r
79 \r
80 /*-----------------------------------------------------------\r
81  * PUBLIC LIST API documented in list.h\r
82  *----------------------------------------------------------*/\r
83 \r
84 void vListInitialise( xList *pxList )\r
85 {\r
86         /* The list structure contains a list item which is used to mark the\r
87         end of the list.  To initialise the list the list end is inserted\r
88         as the only list entry. */\r
89         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
90 \r
91         /* The list end value is the highest possible value in the list to\r
92         ensure it remains at the end of the list. */\r
93         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
94 \r
95         /* The list end next and previous pointers point to itself so we know\r
96         when the list is empty. */\r
97         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
98         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
99 \r
100         pxList->uxNumberOfItems = ( unsigned portBASE_TYPE ) 0U;\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vListInitialiseItem( xListItem *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 /*-----------------------------------------------------------*/\r
110 \r
111 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
112 {\r
113 volatile xListItem * pxIndex;\r
114 \r
115         /* Insert a new list item into pxList, but rather than sort the list,\r
116         makes the new list item the last item to be removed by a call to\r
117         pvListGetOwnerOfNextEntry. */\r
118         pxIndex = pxList->pxIndex;\r
119 \r
120         pxNewListItem->pxNext = pxIndex;\r
121         pxNewListItem->pxPrevious = pxIndex->pxPrevious;\r
122         pxIndex->pxPrevious->pxNext = ( volatile xListItem * ) pxNewListItem;\r
123         pxIndex->pxPrevious = ( volatile xListItem * ) pxNewListItem;   \r
124 \r
125         /* Remember which list the item is in. */\r
126         pxNewListItem->pvContainer = ( void * ) pxList;\r
127 \r
128         ( pxList->uxNumberOfItems )++;\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
133 {\r
134 volatile xListItem *pxIterator;\r
135 portTickType xValueOfInsertion;\r
136 \r
137         /* Insert the new list item into the list, sorted in ulListItem order. */\r
138         xValueOfInsertion = pxNewListItem->xItemValue;\r
139 \r
140         /* If the list already contains a list item with the same item value then\r
141         the new list item should be placed after it.  This ensures that TCB's which\r
142         are stored in ready lists (all of which have the same ulListItem value)\r
143         get an equal share of the CPU.  However, if the xItemValue is the same as\r
144         the back marker the iteration loop below will not end.  This means we need\r
145         to guard against this by checking the value first and modifying the\r
146         algorithm slightly if necessary. */\r
147         if( xValueOfInsertion == portMAX_DELAY )\r
148         {\r
149                 pxIterator = pxList->xListEnd.pxPrevious;\r
150         }\r
151         else\r
152         {\r
153                 /* *** NOTE ***********************************************************\r
154                 If you find your application is crashing here then likely causes are:\r
155                         1) Stack overflow -\r
156                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
157                         2) Incorrect interrupt priority assignment, especially on Cortex-M3\r
158                            parts where numerically high priority values denote low actual\r
159                            interrupt priories, which can seem counter intuitive.  See\r
160                            configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html\r
161                         3) Calling an API function from within a critical section or when\r
162                            the scheduler is suspended, or calling an API function that does\r
163                            not end in "FromISR" from an interrupt.\r
164                         4) Using a queue or semaphore before it has been initialised or\r
165                            before the scheduler has been started (are interrupts firing\r
166                            before vTaskStartScheduler() has been called?).\r
167                 See http://www.freertos.org/FAQHelp.html for more tips.\r
168                 **********************************************************************/\r
169 \r
170                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
171                 {\r
172                         /* There is nothing to do here, we are just iterating to the\r
173                         wanted insertion position. */\r
174                 }\r
175         }\r
176 \r
177         pxNewListItem->pxNext = pxIterator->pxNext;\r
178         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
179         pxNewListItem->pxPrevious = pxIterator;\r
180         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
181 \r
182         /* Remember which list the item is in.  This allows fast removal of the\r
183         item later. */\r
184         pxNewListItem->pvContainer = ( void * ) pxList;\r
185 \r
186         ( pxList->uxNumberOfItems )++;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 unsigned portBASE_TYPE uxListRemove( xListItem *pxItemToRemove )\r
191 {\r
192 xList * pxList;\r
193 \r
194         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
195         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
196 \r
197         /* The list item knows which list it is in.  Obtain the list from the list\r
198         item. */\r
199         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
200 \r
201         /* Make sure the index is left pointing to a valid item. */\r
202         if( pxList->pxIndex == pxItemToRemove )\r
203         {\r
204                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
205         }\r
206 \r
207         pxItemToRemove->pvContainer = NULL;\r
208         ( pxList->uxNumberOfItems )--;\r
209 \r
210         return pxList->uxNumberOfItems;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r