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