]> git.sur5r.net Git - freertos/blob - Source/list.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Source / list.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 \r
49 #include <stdlib.h>\r
50 #include "FreeRTOS.h"\r
51 #include "list.h"\r
52 \r
53 /*-----------------------------------------------------------\r
54  * PUBLIC LIST API documented in list.h\r
55  *----------------------------------------------------------*/\r
56 \r
57 void vListInitialise( xList *pxList )\r
58 {\r
59         /* The list structure contains a list item which is used to mark the\r
60         end of the list.  To initialise the list the list end is inserted\r
61         as the only list entry. */\r
62         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
63 \r
64         /* The list end value is the highest possible value in the list to\r
65         ensure it remains at the end of the list. */\r
66         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
67 \r
68         /* The list end next and previous pointers point to itself so we know\r
69         when the list is empty. */\r
70         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
71         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
72 \r
73         pxList->uxNumberOfItems = 0;\r
74 }\r
75 /*-----------------------------------------------------------*/\r
76 \r
77 void vListInitialiseItem( xListItem *pxItem )\r
78 {\r
79         /* Make sure the list item is not recorded as being on a list. */\r
80         pxItem->pvContainer = NULL;\r
81 }\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
85 {\r
86 volatile xListItem * pxIndex;\r
87 \r
88         /* Insert a new list item into pxList, but rather than sort the list,\r
89         makes the new list item the last item to be removed by a call to\r
90         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
91         the pxIndex member. */\r
92         pxIndex = pxList->pxIndex;\r
93 \r
94         pxNewListItem->pxNext = pxIndex->pxNext;\r
95         pxNewListItem->pxPrevious = pxList->pxIndex;\r
96         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
97         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
98         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
99 \r
100         /* Remember which list the item is in. */\r
101         pxNewListItem->pvContainer = ( void * ) pxList;\r
102 \r
103         ( pxList->uxNumberOfItems )++;\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
108 {\r
109 volatile xListItem *pxIterator;\r
110 portTickType xValueOfInsertion;\r
111 \r
112         /* Insert the new list item into the list, sorted in ulListItem order. */\r
113         xValueOfInsertion = pxNewListItem->xItemValue;\r
114 \r
115         /* If the list already contains a list item with the same item value then\r
116         the new list item should be placed after it.  This ensures that TCB's which\r
117         are stored in ready lists (all of which have the same ulListItem value)\r
118         get an equal share of the CPU.  However, if the xItemValue is the same as \r
119         the back marker the iteration loop below will not end.  This means we need\r
120         to guard against this by checking the value first and modifying the \r
121         algorithm slightly if necessary. */\r
122         if( xValueOfInsertion == portMAX_DELAY )\r
123         {\r
124                 pxIterator = pxList->xListEnd.pxPrevious;\r
125         }\r
126         else\r
127         {\r
128                 /* *** NOTE ***********************************************************\r
129                 If you find your application is crashing here then likely causes are:\r
130                         1) Stack overflow - \r
131                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
132                         2) Incorrect interrupt priority assignment, especially on Cortex M3 \r
133                            parts where numerically high priority values denote low actual \r
134                            interrupt priories, which can seem counter intuitive.  See \r
135                            configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html\r
136                         3) Calling an API function from within a critical section or when\r
137                            the scheduler is suspended.\r
138                         4) Using a queue or semaphore before it has been initialised or\r
139                            before the scheduler has been started (are interrupts firing\r
140                            before vTaskStartScheduler() has been called?).\r
141                 See http://www.freertos.org/FAQHelp.html for more tips. \r
142                 **********************************************************************/\r
143                 \r
144                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
145                 {\r
146                         /* There is nothing to do here, we are just iterating to the\r
147                         wanted insertion position. */\r
148                 }\r
149         }\r
150 \r
151         pxNewListItem->pxNext = pxIterator->pxNext;\r
152         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
153         pxNewListItem->pxPrevious = pxIterator;\r
154         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
155 \r
156         /* Remember which list the item is in.  This allows fast removal of the\r
157         item later. */\r
158         pxNewListItem->pvContainer = ( void * ) pxList;\r
159 \r
160         ( pxList->uxNumberOfItems )++;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vListRemove( xListItem *pxItemToRemove )\r
165 {\r
166 xList * pxList;\r
167 \r
168         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
169         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
170         \r
171         /* The list item knows which list it is in.  Obtain the list from the list\r
172         item. */\r
173         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
174 \r
175         /* Make sure the index is left pointing to a valid item. */\r
176         if( pxList->pxIndex == pxItemToRemove )\r
177         {\r
178                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
179         }\r
180 \r
181         pxItemToRemove->pvContainer = NULL;\r
182         ( pxList->uxNumberOfItems )--;\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r