]> git.sur5r.net Git - freertos/blob - Source/list.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Source / list.c
1 /*\r
2     FreeRTOS V6.0.0 - 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     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 \r
50 #include <stdlib.h>\r
51 #include "FreeRTOS.h"\r
52 #include "list.h"\r
53 \r
54 /*-----------------------------------------------------------\r
55  * PUBLIC LIST API documented in list.h\r
56  *----------------------------------------------------------*/\r
57 \r
58 void vListInitialise( xList *pxList )\r
59 {\r
60         /* The list structure contains a list item which is used to mark the\r
61         end of the list.  To initialise the list the list end is inserted\r
62         as the only list entry. */\r
63         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
64 \r
65         /* The list end value is the highest possible value in the list to\r
66         ensure it remains at the end of the list. */\r
67         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
68 \r
69         /* The list end next and previous pointers point to itself so we know\r
70         when the list is empty. */\r
71         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
72         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
73 \r
74         pxList->uxNumberOfItems = 0;\r
75 }\r
76 /*-----------------------------------------------------------*/\r
77 \r
78 void vListInitialiseItem( xListItem *pxItem )\r
79 {\r
80         /* Make sure the list item is not recorded as being on a list. */\r
81         pxItem->pvContainer = NULL;\r
82 }\r
83 /*-----------------------------------------------------------*/\r
84 \r
85 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
86 {\r
87 volatile xListItem * pxIndex;\r
88 \r
89         /* Insert a new list item into pxList, but rather than sort the list,\r
90         makes the new list item the last item to be removed by a call to\r
91         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
92         the pxIndex member. */\r
93         pxIndex = pxList->pxIndex;\r
94 \r
95         pxNewListItem->pxNext = pxIndex->pxNext;\r
96         pxNewListItem->pxPrevious = pxList->pxIndex;\r
97         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
98         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
99         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
100 \r
101         /* Remember which list the item is in. */\r
102         pxNewListItem->pvContainer = ( void * ) pxList;\r
103 \r
104         ( pxList->uxNumberOfItems )++;\r
105 }\r
106 /*-----------------------------------------------------------*/\r
107 \r
108 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
109 {\r
110 volatile xListItem *pxIterator;\r
111 portTickType xValueOfInsertion;\r
112 \r
113         /* Insert the new list item into the list, sorted in ulListItem order. */\r
114         xValueOfInsertion = pxNewListItem->xItemValue;\r
115 \r
116         /* If the list already contains a list item with the same item value then\r
117         the new list item should be placed after it.  This ensures that TCB's which\r
118         are stored in ready lists (all of which have the same ulListItem value)\r
119         get an equal share of the CPU.  However, if the xItemValue is the same as \r
120         the back marker the iteration loop below will not end.  This means we need\r
121         to guard against this by checking the value first and modifying the \r
122         algorithm slightly if necessary. */\r
123         if( xValueOfInsertion == portMAX_DELAY )\r
124         {\r
125                 pxIterator = pxList->xListEnd.pxPrevious;\r
126         }\r
127         else\r
128         {\r
129                 /* *** NOTE ***********************************************************\r
130                 If you find your application is crashing here then likely causes are:\r
131                         1) Stack overflow - \r
132                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
133                         2) Incorrect interrupt priority assignment, especially on Cortex M3 \r
134                            parts where numerically high priority values denote low actual \r
135                            interrupt priories, which can seem counter intuitive.  See \r
136                            configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html\r
137                         3) Calling an API function from within a critical section or when\r
138                            the scheduler is suspended.\r
139                         4) Using a queue or semaphore before it has been initialised or\r
140                            before the scheduler has been started (are interrupts firing\r
141                            before vTaskStartScheduler() has been called?).\r
142                 See http://www.freertos.org/FAQHelp.html for more tips. \r
143                 **********************************************************************/\r
144                 \r
145                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
146                 {\r
147                         /* There is nothing to do here, we are just iterating to the\r
148                         wanted insertion position. */\r
149                 }\r
150         }\r
151 \r
152         pxNewListItem->pxNext = pxIterator->pxNext;\r
153         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
154         pxNewListItem->pxPrevious = pxIterator;\r
155         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
156 \r
157         /* Remember which list the item is in.  This allows fast removal of the\r
158         item later. */\r
159         pxNewListItem->pvContainer = ( void * ) pxList;\r
160 \r
161         ( pxList->uxNumberOfItems )++;\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 void vListRemove( xListItem *pxItemToRemove )\r
166 {\r
167 xList * pxList;\r
168 \r
169         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
170         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
171         \r
172         /* The list item knows which list it is in.  Obtain the list from the list\r
173         item. */\r
174         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
175 \r
176         /* Make sure the index is left pointing to a valid item. */\r
177         if( pxList->pxIndex == pxItemToRemove )\r
178         {\r
179                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
180         }\r
181 \r
182         pxItemToRemove->pvContainer = NULL;\r
183         ( pxList->uxNumberOfItems )--;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r