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