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