]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/list.c
293b1edf352ffaf5695e34afac90d742ca762886
[freertos] / FreeRTOS / Source / list.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 #include <stdlib.h>\r
77 #include "FreeRTOS.h"\r
78 #include "list.h"\r
79 \r
80 /*-----------------------------------------------------------\r
81  * PUBLIC LIST API documented in list.h\r
82  *----------------------------------------------------------*/\r
83 \r
84 void vListInitialise( xList *pxList )\r
85 {\r
86         /* The list structure contains a list item which is used to mark the\r
87         end of the list.  To initialise the list the list end is inserted\r
88         as the only list entry. */\r
89         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
90 \r
91         /* The list end value is the highest possible value in the list to\r
92         ensure it remains at the end of the list. */\r
93         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
94 \r
95         /* The list end next and previous pointers point to itself so we know\r
96         when the list is empty. */\r
97         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
98         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
99 \r
100         pxList->uxNumberOfItems = ( unsigned portBASE_TYPE ) 0U;\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vListInitialiseItem( xListItem *pxItem )\r
105 {\r
106         /* Make sure the list item is not recorded as being on a list. */\r
107         pxItem->pvContainer = NULL;\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
112 {\r
113 volatile xListItem * pxIndex;\r
114 \r
115         /* Insert a new list item into pxList, but rather than sort the list,\r
116         makes the new list item the last item to be removed by a call to\r
117         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
118         the pxIndex member. */\r
119         pxIndex = pxList->pxIndex;\r
120 \r
121         pxNewListItem->pxNext = pxIndex->pxNext;\r
122         pxNewListItem->pxPrevious = pxList->pxIndex;\r
123         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
124         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
125         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
126 \r
127         /* Remember which list the item is in. */\r
128         pxNewListItem->pvContainer = ( void * ) pxList;\r
129 \r
130         ( pxList->uxNumberOfItems )++;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
135 {\r
136 volatile xListItem *pxIterator;\r
137 portTickType xValueOfInsertion;\r
138 \r
139         /* Insert the new list item into the list, sorted in ulListItem order. */\r
140         xValueOfInsertion = pxNewListItem->xItemValue;\r
141 \r
142         /* If the list already contains a list item with the same item value then\r
143         the new list item should be placed after it.  This ensures that TCB's which\r
144         are stored in ready lists (all of which have the same ulListItem value)\r
145         get an equal share of the CPU.  However, if the xItemValue is the same as\r
146         the back marker the iteration loop below will not end.  This means we need\r
147         to guard against this by checking the value first and modifying the\r
148         algorithm slightly if necessary. */\r
149         if( xValueOfInsertion == portMAX_DELAY )\r
150         {\r
151                 pxIterator = pxList->xListEnd.pxPrevious;\r
152         }\r
153         else\r
154         {\r
155                 /* *** NOTE ***********************************************************\r
156                 If you find your application is crashing here then likely causes are:\r
157                         1) Stack overflow -\r
158                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
159                         2) Incorrect interrupt priority assignment, especially on Cortex-M3\r
160                            parts where numerically high priority values denote low actual\r
161                            interrupt priories, which can seem counter intuitive.  See\r
162                            configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html\r
163                         3) Calling an API function from within a critical section or when\r
164                            the scheduler is suspended.\r
165                         4) Using a queue or semaphore before it has been initialised or\r
166                            before the scheduler has been started (are interrupts firing\r
167                            before vTaskStartScheduler() has been called?).\r
168                 See http://www.freertos.org/FAQHelp.html for more tips.\r
169                 **********************************************************************/\r
170 \r
171                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
172                 {\r
173                         /* There is nothing to do here, we are just iterating to the\r
174                         wanted insertion position. */\r
175                 }\r
176         }\r
177 \r
178         pxNewListItem->pxNext = pxIterator->pxNext;\r
179         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
180         pxNewListItem->pxPrevious = pxIterator;\r
181         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
182 \r
183         /* Remember which list the item is in.  This allows fast removal of the\r
184         item later. */\r
185         pxNewListItem->pvContainer = ( void * ) pxList;\r
186 \r
187         ( pxList->uxNumberOfItems )++;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 unsigned portBASE_TYPE uxListRemove( xListItem *pxItemToRemove )\r
192 {\r
193 xList * pxList;\r
194 \r
195         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
196         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
197 \r
198         /* The list item knows which list it is in.  Obtain the list from the list\r
199         item. */\r
200         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
201 \r
202         /* Make sure the index is left pointing to a valid item. */\r
203         if( pxList->pxIndex == pxItemToRemove )\r
204         {\r
205                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
206         }\r
207 \r
208         pxItemToRemove->pvContainer = NULL;\r
209         ( pxList->uxNumberOfItems )--;\r
210 \r
211         return pxList->uxNumberOfItems;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r