]> begriffs open source - freertos/blob - FreeRTOS/Source/list.c
Update PIC32 demo application to remove reliance on PLIB functions.
[freertos] / FreeRTOS / Source / list.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 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     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 \r
70 #include <stdlib.h>\r
71 #include "FreeRTOS.h"\r
72 #include "list.h"\r
73 \r
74 /*-----------------------------------------------------------\r
75  * PUBLIC LIST API documented in list.h\r
76  *----------------------------------------------------------*/\r
77 \r
78 void vListInitialise( xList *pxList )\r
79 {\r
80         /* The list structure contains a list item which is used to mark the\r
81         end of the list.  To initialise the list the list end is inserted\r
82         as the only list entry. */\r
83         pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd );\r
84 \r
85         /* The list end value is the highest possible value in the list to\r
86         ensure it remains at the end of the list. */\r
87         pxList->xListEnd.xItemValue = portMAX_DELAY;\r
88 \r
89         /* The list end next and previous pointers point to itself so we know\r
90         when the list is empty. */\r
91         pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd );\r
92         pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );\r
93 \r
94         pxList->uxNumberOfItems = ( unsigned portBASE_TYPE ) 0U;\r
95 }\r
96 /*-----------------------------------------------------------*/\r
97 \r
98 void vListInitialiseItem( xListItem *pxItem )\r
99 {\r
100         /* Make sure the list item is not recorded as being on a list. */\r
101         pxItem->pvContainer = NULL;\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void vListInsertEnd( xList *pxList, xListItem *pxNewListItem )\r
106 {\r
107 volatile xListItem * pxIndex;\r
108 \r
109         /* Insert a new list item into pxList, but rather than sort the list,\r
110         makes the new list item the last item to be removed by a call to\r
111         pvListGetOwnerOfNextEntry.  This means it has to be the item pointed to by\r
112         the pxIndex member. */\r
113         pxIndex = pxList->pxIndex;\r
114 \r
115         pxNewListItem->pxNext = pxIndex->pxNext;\r
116         pxNewListItem->pxPrevious = pxList->pxIndex;\r
117         pxIndex->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
118         pxIndex->pxNext = ( volatile xListItem * ) pxNewListItem;\r
119         pxList->pxIndex = ( volatile xListItem * ) pxNewListItem;\r
120 \r
121         /* Remember which list the item is in. */\r
122         pxNewListItem->pvContainer = ( void * ) pxList;\r
123 \r
124         ( pxList->uxNumberOfItems )++;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vListInsert( xList *pxList, xListItem *pxNewListItem )\r
129 {\r
130 volatile xListItem *pxIterator;\r
131 portTickType xValueOfInsertion;\r
132 \r
133         /* Insert the new list item into the list, sorted in ulListItem order. */\r
134         xValueOfInsertion = pxNewListItem->xItemValue;\r
135 \r
136         /* If the list already contains a list item with the same item value then\r
137         the new list item should be placed after it.  This ensures that TCB's which\r
138         are stored in ready lists (all of which have the same ulListItem value)\r
139         get an equal share of the CPU.  However, if the xItemValue is the same as\r
140         the back marker the iteration loop below will not end.  This means we need\r
141         to guard against this by checking the value first and modifying the\r
142         algorithm slightly if necessary. */\r
143         if( xValueOfInsertion == portMAX_DELAY )\r
144         {\r
145                 pxIterator = pxList->xListEnd.pxPrevious;\r
146         }\r
147         else\r
148         {\r
149                 /* *** NOTE ***********************************************************\r
150                 If you find your application is crashing here then likely causes are:\r
151                         1) Stack overflow -\r
152                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html\r
153                         2) Incorrect interrupt priority assignment, especially on Cortex-M3\r
154                            parts where numerically high priority values denote low actual\r
155                            interrupt priories, which can seem counter intuitive.  See\r
156                            configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html\r
157                         3) Calling an API function from within a critical section or when\r
158                            the scheduler is suspended.\r
159                         4) Using a queue or semaphore before it has been initialised or\r
160                            before the scheduler has been started (are interrupts firing\r
161                            before vTaskStartScheduler() has been called?).\r
162                 See http://www.freertos.org/FAQHelp.html for more tips.\r
163                 **********************************************************************/\r
164 \r
165                 for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )\r
166                 {\r
167                         /* There is nothing to do here, we are just iterating to the\r
168                         wanted insertion position. */\r
169                 }\r
170         }\r
171 \r
172         pxNewListItem->pxNext = pxIterator->pxNext;\r
173         pxNewListItem->pxNext->pxPrevious = ( volatile xListItem * ) pxNewListItem;\r
174         pxNewListItem->pxPrevious = pxIterator;\r
175         pxIterator->pxNext = ( volatile xListItem * ) pxNewListItem;\r
176 \r
177         /* Remember which list the item is in.  This allows fast removal of the\r
178         item later. */\r
179         pxNewListItem->pvContainer = ( void * ) pxList;\r
180 \r
181         ( pxList->uxNumberOfItems )++;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 unsigned portBASE_TYPE uxListRemove( xListItem *pxItemToRemove )\r
186 {\r
187 xList * pxList;\r
188 \r
189         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;\r
190         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;\r
191 \r
192         /* The list item knows which list it is in.  Obtain the list from the list\r
193         item. */\r
194         pxList = ( xList * ) pxItemToRemove->pvContainer;\r
195 \r
196         /* Make sure the index is left pointing to a valid item. */\r
197         if( pxList->pxIndex == pxItemToRemove )\r
198         {\r
199                 pxList->pxIndex = pxItemToRemove->pxPrevious;\r
200         }\r
201 \r
202         pxItemToRemove->pvContainer = NULL;\r
203         ( pxList->uxNumberOfItems )--;\r
204 \r
205         return pxList->uxNumberOfItems;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r