2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
55 #include "FreeRTOS.h"
\r
58 /*-----------------------------------------------------------
\r
59 * PUBLIC LIST API documented in list.h
\r
60 *----------------------------------------------------------*/
\r
62 void vListInitialise( xList *pxList )
\r
64 /* The list structure contains a list item which is used to mark the
\r
65 end of the list. To initialise the list the list end is inserted
\r
66 as the only list entry. */
\r
67 pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );
\r
69 /* The list end value is the highest possible value in the list to
\r
70 ensure it remains at the end of the list. */
\r
71 pxList->xListEnd.xItemValue = portMAX_DELAY;
\r
73 /* The list end next and previous pointers point to itself so we know
\r
74 when the list is empty. */
\r
75 pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );
\r
76 pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );
\r
78 pxList->uxNumberOfItems = 0;
\r
80 /*-----------------------------------------------------------*/
\r
82 void vListInitialiseItem( xListItem *pxItem )
\r
84 /* Make sure the list item is not recorded as being on a list. */
\r
85 pxItem->pvContainer = NULL;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )
\r
91 volatile xListItem * pxIndex;
\r
93 /* Insert a new list item into pxList, but rather than sort the list,
\r
94 makes the new list item the last item to be removed by a call to
\r
95 pvListGetOwnerOfNextEntry. This means it has to be the item pointed to by
\r
96 the pxIndex member. */
\r
97 pxIndex = pxList->pxIndex;
\r
99 pxNewListItem->pxNext = pxIndex->pxNext;
\r
100 pxNewListItem->pxPrevious = pxList->pxIndex;
\r
101 pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;
\r
102 pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;
\r
103 pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;
\r
105 /* Remember which list the item is in. */
\r
106 pxNewListItem->pvContainer = ( void * ) pxList;
\r
108 ( pxList->uxNumberOfItems )++;
\r
110 /*-----------------------------------------------------------*/
\r
112 void vListInsert( xList *pxList, xListItem *pxNewListItem )
\r
114 volatile xListItem *pxIterator;
\r
115 portTickType xValueOfInsertion;
\r
117 /* Insert the new list item into the list, sorted in ulListItem order. */
\r
118 xValueOfInsertion = pxNewListItem->xItemValue;
\r
120 /* If the list already contains a list item with the same item value then
\r
121 the new list item should be placed after it. This ensures that TCB's which
\r
122 are stored in ready lists (all of which have the same ulListItem value)
\r
123 get an equal share of the CPU. However, if the xItemValue is the same as
\r
124 the back marker the iteration loop below will not end. This means we need
\r
125 to guard against this by checking the value first and modifying the
\r
126 algorithm slightly if necessary. */
\r
127 if( xValueOfInsertion == portMAX_DELAY )
\r
129 pxIterator = pxList->xListEnd.pxPrevious;
\r
133 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
\r
135 /* There is nothing to do here, we are just iterating to the
\r
136 wanted insertion position. */
\r
140 pxNewListItem->pxNext = pxIterator->pxNext;
\r
141 pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;
\r
142 pxNewListItem->pxPrevious = pxIterator;
\r
143 pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;
\r
145 /* Remember which list the item is in. This allows fast removal of the
\r
147 pxNewListItem->pvContainer = ( void * ) pxList;
\r
149 ( pxList->uxNumberOfItems )++;
\r
151 /*-----------------------------------------------------------*/
\r
153 void vListRemove( xListItem *pxItemToRemove )
\r
157 pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
\r
158 pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
\r
160 /* The list item knows which list it is in. Obtain the list from the list
\r
162 pxList = ( xList * ) pxItemToRemove->pvContainer;
\r
164 /* Make sure the index is left pointing to a valid item. */
\r
165 if( pxList->pxIndex == pxItemToRemove )
\r
167 pxList->pxIndex = pxItemToRemove->pxPrevious;
\r
170 pxItemToRemove->pvContainer = NULL;
\r
171 ( pxList->uxNumberOfItems )--;
\r
173 /*-----------------------------------------------------------*/
\r