]> git.sur5r.net Git - freertos/blob - Source/list.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / list.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37 Changes from V1.2.0\r
38 \r
39         + Removed the volatile modifier from the function parameters.  This was\r
40           only ever included to prevent compiler warnings.  Now warnings are\r
41           removed by casting parameters where the calls are made.\r
42 \r
43         + prvListGetOwnerOfNextEntry() and prvListGetOwnerOfHeadEntry() have been\r
44           removed from the c file and added as macros to the h file.\r
45 \r
46         + uxNumberOfItems has been added to the list structure.  This removes the\r
47           need for a pointer comparison when checking if a list is empty, and so\r
48           is slightly faster.\r
49 \r
50         + Removed the NULL check in vListRemove().  This makes the call faster but\r
51           necessitates any application code utilising the list implementation to\r
52           ensure NULL pointers are not passed.\r
53 \r
54 Changes from V2.0.0\r
55 \r
56         + Double linked the lists to allow faster removal item removal.\r
57 \r
58 Changes from V2.6.1\r
59 \r
60         + Make use of the new portBASE_TYPE definition where ever appropriate.\r
61 \r
62 Changes from V3.0.0\r
63 \r
64         + API changes as described on the FreeRTOS.org WEB site.\r
65 \r
66 Changes from V3.2.4\r
67 \r
68         + Removed the pxHead member of the xList structure.  This always pointed\r
69           to the same place so has been removed to free a few bytes of RAM.\r
70 \r
71         + Introduced the xMiniListItem structure that does not include the \r
72           xListItem members that are not required by the xListEnd member of a list.\r
73           Again this was done to reduce RAM usage.\r
74 \r
75         + Changed the volatile definitions of some structure members to clean up\r
76           the code where the list structures are used.\r
77 \r
78 Changes from V4.0.4\r
79 \r
80         + Optimised vListInsert() in the case when the wake time is the maximum \r
81           tick count value.\r
82 */\r
83 \r
84 #include <stdlib.h>\r
85 #include "FreeRTOS.h"\r
86 #include "list.h"\r
87 \r
88 /*-----------------------------------------------------------\r
89  * PUBLIC LIST API documented in list.h\r
90  *----------------------------------------------------------*/\r
91 \r
92 void vListInitialise( xList *pxList )\r
93 {\r
94         /* The list structure contains a list item which is used to mark the\r
95         end of the list.  To initialise the list the list end is inserted\r
96         as the only list entry. */\r
97         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
98 \r
99         /* The list end value is the highest possible value in the list to\r
100         ensure it remains at the end of the list. */\r
101         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
102 \r
103         /* The list end next and previous pointers point to itself so we know\r
104         when the list is empty. */\r
105         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
106         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
107 \r
108         pxList->uxNumberOfItems = 0;\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vListInitialiseItem( xListItem *pxItem )\r
113 {\r
114         /* Make sure the list item is not recorded as being on a list. */\r
115         pxItem->pvContainer = NULL;\r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
120 {\r
121 volatile xListItem * pxIndex;\r
122 \r
123         /* Insert a new list item into pxList, but rather than sort the list,\r
124         makes the new list item the last item to be removed by a call to\r
125         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
126         the pxIndex member. */\r
127         pxIndex = pxList->pxIndex;\r
128 \r
129         pxNewListItem->pxNext = pxIndex->pxNext;\r
130         pxNewListItem->pxPrevious = pxList->pxIndex;\r
131         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
132         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
133         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
134 \r
135         /* Remember which list the item is in. */\r
136         pxNewListItem->pvContainer = ( void * ) pxList;\r
137 \r
138         ( pxList->uxNumberOfItems )++;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
143 {\r
144 volatile xListItem *pxIterator;\r
145 portTickType xValueOfInsertion;\r
146 \r
147         /* Insert the new list item into the list, sorted in ulListItem order. */\r
148         xValueOfInsertion = pxNewListItem->xItemValue;\r
149 \r
150         /* If the list already contains a list item with the same item value then\r
151         the new list item should be placed after it.  This ensures that TCB's which\r
152         are stored in ready lists (all of which have the same ulListItem value)\r
153         get an equal share of the CPU.  However, if the xItemValue is the same as \r
154         the back marker the iteration loop below will not end.  This means we need\r
155         to guard against this by checking the value first and modifying the \r
156         algorithm slightly if necessary. */\r
157         if( xValueOfInsertion == portMAX_DELAY )\r
158         {\r
159                 pxIterator = pxList->xListEnd.pxPrevious;\r
160         }\r
161         else\r
162         {\r
163                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
164                 {\r
165                         /* There is nothing to do here, we are just iterating to the\r
166                         wanted insertion position. */\r
167                 }\r
168         }\r
169 \r
170         pxNewListItem->pxNext = pxIterator->pxNext;\r
171         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
172         pxNewListItem->pxPrevious = pxIterator;\r
173         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
174 \r
175         /* Remember which list the item is in.  This allows fast removal of the\r
176         item later. */\r
177         pxNewListItem->pvContainer = ( void * ) pxList;\r
178 \r
179         ( pxList->uxNumberOfItems )++;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vListRemove( xListItem *pxItemToRemove )\r
184 {\r
185 xList * pxList;\r
186 \r
187         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
188         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
189         \r
190         /* The list item knows which list it is in.  Obtain the list from the list\r
191         item. */\r
192         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
193 \r
194         /* Make sure the index is left pointing to a valid item. */\r
195         if( pxList->pxIndex == pxItemToRemove )\r
196         {\r
197                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
198         }\r
199 \r
200         pxItemToRemove->pvContainer = NULL;\r
201         ( pxList->uxNumberOfItems )--;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r