]> git.sur5r.net Git - freertos/blob - Source/list.c
Update ready for V5.1.0 release.
[freertos] / Source / list.c
1 /*\r
2         FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 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     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 \r
51 #include <stdlib.h>\r
52 #include "FreeRTOS.h"\r
53 #include "list.h"\r
54 \r
55 /*-----------------------------------------------------------\r
56  * PUBLIC LIST API documented in list.h\r
57  *----------------------------------------------------------*/\r
58 \r
59 void vListInitialise( xList *pxList )\r
60 {\r
61         /* The list structure contains a list item which is used to mark the\r
62         end of the list.  To initialise the list the list end is inserted\r
63         as the only list entry. */\r
64         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
65 \r
66         /* The list end value is the highest possible value in the list to\r
67         ensure it remains at the end of the list. */\r
68         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
69 \r
70         /* The list end next and previous pointers point to itself so we know\r
71         when the list is empty. */\r
72         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
73         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
74 \r
75         pxList->uxNumberOfItems = 0;\r
76 }\r
77 /*-----------------------------------------------------------*/\r
78 \r
79 void vListInitialiseItem( xListItem *pxItem )\r
80 {\r
81         /* Make sure the list item is not recorded as being on a list. */\r
82         pxItem->pvContainer = NULL;\r
83 }\r
84 /*-----------------------------------------------------------*/\r
85 \r
86 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
87 {\r
88 volatile xListItem * pxIndex;\r
89 \r
90         /* Insert a new list item into pxList, but rather than sort the list,\r
91         makes the new list item the last item to be removed by a call to\r
92         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
93         the pxIndex member. */\r
94         pxIndex = pxList->pxIndex;\r
95 \r
96         pxNewListItem->pxNext = pxIndex->pxNext;\r
97         pxNewListItem->pxPrevious = pxList->pxIndex;\r
98         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
99         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
100         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
101 \r
102         /* Remember which list the item is in. */\r
103         pxNewListItem->pvContainer = ( void * ) pxList;\r
104 \r
105         ( pxList->uxNumberOfItems )++;\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
110 {\r
111 volatile xListItem *pxIterator;\r
112 portTickType xValueOfInsertion;\r
113 \r
114         /* Insert the new list item into the list, sorted in ulListItem order. */\r
115         xValueOfInsertion = pxNewListItem->xItemValue;\r
116 \r
117         /* If the list already contains a list item with the same item value then\r
118         the new list item should be placed after it.  This ensures that TCB's which\r
119         are stored in ready lists (all of which have the same ulListItem value)\r
120         get an equal share of the CPU.  However, if the xItemValue is the same as \r
121         the back marker the iteration loop below will not end.  This means we need\r
122         to guard against this by checking the value first and modifying the \r
123         algorithm slightly if necessary. */\r
124         if( xValueOfInsertion == portMAX_DELAY )\r
125         {\r
126                 pxIterator = pxList->xListEnd.pxPrevious;\r
127         }\r
128         else\r
129         {\r
130                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
131                 {\r
132                         /* There is nothing to do here, we are just iterating to the\r
133                         wanted insertion position. */\r
134                 }\r
135         }\r
136 \r
137         pxNewListItem->pxNext = pxIterator->pxNext;\r
138         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
139         pxNewListItem->pxPrevious = pxIterator;\r
140         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
141 \r
142         /* Remember which list the item is in.  This allows fast removal of the\r
143         item later. */\r
144         pxNewListItem->pvContainer = ( void * ) pxList;\r
145 \r
146         ( pxList->uxNumberOfItems )++;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 void vListRemove( xListItem *pxItemToRemove )\r
151 {\r
152 xList * pxList;\r
153 \r
154         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
155         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
156         \r
157         /* The list item knows which list it is in.  Obtain the list from the list\r
158         item. */\r
159         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
160 \r
161         /* Make sure the index is left pointing to a valid item. */\r
162         if( pxList->pxIndex == pxItemToRemove )\r
163         {\r
164                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
165         }\r
166 \r
167         pxItemToRemove->pvContainer = NULL;\r
168         ( pxList->uxNumberOfItems )--;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r