]> git.sur5r.net Git - freertos/blob - Source/list.c
Update version numbers to V4.8.0
[freertos] / Source / list.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50 Changes from V1.2.0\r
51 \r
52         + Removed the volatile modifier from the function parameters.  This was\r
53           only ever included to prevent compiler warnings.  Now warnings are\r
54           removed by casting parameters where the calls are made.\r
55 \r
56         + prvListGetOwnerOfNextEntry() and prvListGetOwnerOfHeadEntry() have been\r
57           removed from the c file and added as macros to the h file.\r
58 \r
59         + uxNumberOfItems has been added to the list structure.  This removes the\r
60           need for a pointer comparison when checking if a list is empty, and so\r
61           is slightly faster.\r
62 \r
63         + Removed the NULL check in vListRemove().  This makes the call faster but\r
64           necessitates any application code utilising the list implementation to\r
65           ensure NULL pointers are not passed.\r
66 \r
67 Changes from V2.0.0\r
68 \r
69         + Double linked the lists to allow faster removal item removal.\r
70 \r
71 Changes from V2.6.1\r
72 \r
73         + Make use of the new portBASE_TYPE definition where ever appropriate.\r
74 \r
75 Changes from V3.0.0\r
76 \r
77         + API changes as described on the FreeRTOS.org WEB site.\r
78 \r
79 Changes from V3.2.4\r
80 \r
81         + Removed the pxHead member of the xList structure.  This always pointed\r
82           to the same place so has been removed to free a few bytes of RAM.\r
83 \r
84         + Introduced the xMiniListItem structure that does not include the \r
85           xListItem members that are not required by the xListEnd member of a list.\r
86           Again this was done to reduce RAM usage.\r
87 \r
88         + Changed the volatile definitions of some structure members to clean up\r
89           the code where the list structures are used.\r
90 \r
91 Changes from V4.0.4\r
92 \r
93         + Optimised vListInsert() in the case when the wake time is the maximum \r
94           tick count value.\r
95 */\r
96 \r
97 #include <stdlib.h>\r
98 #include "FreeRTOS.h"\r
99 #include "list.h"\r
100 \r
101 /*-----------------------------------------------------------\r
102  * PUBLIC LIST API documented in list.h\r
103  *----------------------------------------------------------*/\r
104 \r
105 void vListInitialise( xList *pxList )\r
106 {\r
107         /* The list structure contains a list item which is used to mark the\r
108         end of the list.  To initialise the list the list end is inserted\r
109         as the only list entry. */\r
110         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
111 \r
112         /* The list end value is the highest possible value in the list to\r
113         ensure it remains at the end of the list. */\r
114         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
115 \r
116         /* The list end next and previous pointers point to itself so we know\r
117         when the list is empty. */\r
118         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
119         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
120 \r
121         pxList->uxNumberOfItems = 0;\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vListInitialiseItem( xListItem *pxItem )\r
126 {\r
127         /* Make sure the list item is not recorded as being on a list. */\r
128         pxItem->pvContainer = NULL;\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
133 {\r
134 volatile xListItem * pxIndex;\r
135 \r
136         /* Insert a new list item into pxList, but rather than sort the list,\r
137         makes the new list item the last item to be removed by a call to\r
138         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
139         the pxIndex member. */\r
140         pxIndex = pxList->pxIndex;\r
141 \r
142         pxNewListItem->pxNext = pxIndex->pxNext;\r
143         pxNewListItem->pxPrevious = pxList->pxIndex;\r
144         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
145         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
146         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
147 \r
148         /* Remember which list the item is in. */\r
149         pxNewListItem->pvContainer = ( void * ) pxList;\r
150 \r
151         ( pxList->uxNumberOfItems )++;\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
156 {\r
157 volatile xListItem *pxIterator;\r
158 portTickType xValueOfInsertion;\r
159 \r
160         /* Insert the new list item into the list, sorted in ulListItem order. */\r
161         xValueOfInsertion = pxNewListItem->xItemValue;\r
162 \r
163         /* If the list already contains a list item with the same item value then\r
164         the new list item should be placed after it.  This ensures that TCB's which\r
165         are stored in ready lists (all of which have the same ulListItem value)\r
166         get an equal share of the CPU.  However, if the xItemValue is the same as \r
167         the back marker the iteration loop below will not end.  This means we need\r
168         to guard against this by checking the value first and modifying the \r
169         algorithm slightly if necessary. */\r
170         if( xValueOfInsertion == portMAX_DELAY )\r
171         {\r
172                 pxIterator = pxList->xListEnd.pxPrevious;\r
173         }\r
174         else\r
175         {\r
176                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
177                 {\r
178                         /* There is nothing to do here, we are just iterating to the\r
179                         wanted insertion position. */\r
180                 }\r
181         }\r
182 \r
183         pxNewListItem->pxNext = pxIterator->pxNext;\r
184         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
185         pxNewListItem->pxPrevious = pxIterator;\r
186         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
187 \r
188         /* Remember which list the item is in.  This allows fast removal of the\r
189         item later. */\r
190         pxNewListItem->pvContainer = ( void * ) pxList;\r
191 \r
192         ( pxList->uxNumberOfItems )++;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void vListRemove( xListItem *pxItemToRemove )\r
197 {\r
198 xList * pxList;\r
199 \r
200         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
201         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
202         \r
203         /* The list item knows which list it is in.  Obtain the list from the list\r
204         item. */\r
205         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
206 \r
207         /* Make sure the index is left pointing to a valid item. */\r
208         if( pxList->pxIndex == pxItemToRemove )\r
209         {\r
210                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
211         }\r
212 \r
213         pxItemToRemove->pvContainer = NULL;\r
214         ( pxList->uxNumberOfItems )--;\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r