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