1 /*
2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29
30 #include <stdlib.h>
31
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
33 * all the API functions to use the MPU wrappers. That should only be done when
34 * task.h is included from an application file. */
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36
37 #include "FreeRTOS.h"
38 #include "list.h"
39
40 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
41 * defined for the header files above, but not in this file, in order to
42 * generate the correct privileged Vs unprivileged linkage and placement. */
43 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
44
45 /*-----------------------------------------------------------
46 * PUBLIC LIST API documented in list.h
47 *----------------------------------------------------------*/
48
vListInitialise(List_t * const pxList)49 void vListInitialise( List_t * const pxList )
50 {
51 traceENTER_vListInitialise( pxList );
52
53 /* The list structure contains a list item which is used to mark the
54 * end of the list. To initialise the list the list end is inserted
55 * as the only list entry. */
56 pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );
57
58 listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
59
60 /* The list end value is the highest possible value in the list to
61 * ensure it remains at the end of the list. */
62 pxList->xListEnd.xItemValue = portMAX_DELAY;
63
64 /* The list end next and previous pointers point to itself so we know
65 * when the list is empty. */
66 pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );
67 pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );
68
69 /* Initialize the remaining fields of xListEnd when it is a proper ListItem_t */
70 #if ( configUSE_MINI_LIST_ITEM == 0 )
71 {
72 pxList->xListEnd.pvOwner = NULL;
73 pxList->xListEnd.pxContainer = NULL;
74 listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
75 }
76 #endif
77
78 pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
79
80 /* Write known values into the list if
81 * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
82 listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
83 listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
84
85 traceRETURN_vListInitialise();
86 }
87 /*-----------------------------------------------------------*/
88
vListInitialiseItem(ListItem_t * const pxItem)89 void vListInitialiseItem( ListItem_t * const pxItem )
90 {
91 traceENTER_vListInitialiseItem( pxItem );
92
93 /* Make sure the list item is not recorded as being on a list. */
94 pxItem->pxContainer = NULL;
95
96 /* Write known values into the list item if
97 * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
98 listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
99 listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
100
101 traceRETURN_vListInitialiseItem();
102 }
103 /*-----------------------------------------------------------*/
104
vListInsertEnd(List_t * const pxList,ListItem_t * const pxNewListItem)105 void vListInsertEnd( List_t * const pxList,
106 ListItem_t * const pxNewListItem )
107 {
108 ListItem_t * const pxIndex = pxList->pxIndex;
109
110 traceENTER_vListInsertEnd( pxList, pxNewListItem );
111
112 /* Only effective when configASSERT() is also defined, these tests may catch
113 * the list data structures being overwritten in memory. They will not catch
114 * data errors caused by incorrect configuration or use of FreeRTOS. */
115 listTEST_LIST_INTEGRITY( pxList );
116 listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
117
118 /* Insert a new list item into pxList, but rather than sort the list,
119 * makes the new list item the last item to be removed by a call to
120 * listGET_OWNER_OF_NEXT_ENTRY(). */
121 pxNewListItem->pxNext = pxIndex;
122 pxNewListItem->pxPrevious = pxIndex->pxPrevious;
123
124 /* Only used during decision coverage testing. */
125 mtCOVERAGE_TEST_DELAY();
126
127 pxIndex->pxPrevious->pxNext = pxNewListItem;
128 pxIndex->pxPrevious = pxNewListItem;
129
130 /* Remember which list the item is in. */
131 pxNewListItem->pxContainer = pxList;
132
133 ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems + 1U );
134
135 traceRETURN_vListInsertEnd();
136 }
137 /*-----------------------------------------------------------*/
138
vListInsert(List_t * const pxList,ListItem_t * const pxNewListItem)139 void vListInsert( List_t * const pxList,
140 ListItem_t * const pxNewListItem )
141 {
142 ListItem_t * pxIterator;
143 const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
144
145 traceENTER_vListInsert( pxList, pxNewListItem );
146
147 /* Only effective when configASSERT() is also defined, these tests may catch
148 * the list data structures being overwritten in memory. They will not catch
149 * data errors caused by incorrect configuration or use of FreeRTOS. */
150 listTEST_LIST_INTEGRITY( pxList );
151 listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
152
153 /* Insert the new list item into the list, sorted in xItemValue order.
154 *
155 * If the list already contains a list item with the same item value then the
156 * new list item should be placed after it. This ensures that TCBs which are
157 * stored in ready lists (all of which have the same xItemValue value) get a
158 * share of the CPU. However, if the xItemValue is the same as the back marker
159 * the iteration loop below will not end. Therefore the value is checked
160 * first, and the algorithm slightly modified if necessary. */
161 if( xValueOfInsertion == portMAX_DELAY )
162 {
163 pxIterator = pxList->xListEnd.pxPrevious;
164 }
165 else
166 {
167 /* *** NOTE ***********************************************************
168 * If you find your application is crashing here then likely causes are
169 * listed below. In addition see https://www.FreeRTOS.org/FAQHelp.html for
170 * more tips, and ensure configASSERT() is defined!
171 * https://www.FreeRTOS.org/a00110.html#configASSERT
172 *
173 * 1) Stack overflow -
174 * see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
175 * 2) Incorrect interrupt priority assignment, especially on Cortex-M
176 * parts where numerically high priority values denote low actual
177 * interrupt priorities, which can seem counter intuitive. See
178 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html and the definition
179 * of configMAX_SYSCALL_INTERRUPT_PRIORITY on
180 * https://www.FreeRTOS.org/a00110.html
181 * 3) Calling an API function from within a critical section or when
182 * the scheduler is suspended, or calling an API function that does
183 * not end in "FromISR" from an interrupt.
184 * 4) Using a queue or semaphore before it has been initialised or
185 * before the scheduler has been started (are interrupts firing
186 * before vTaskStartScheduler() has been called?).
187 * 5) If the FreeRTOS port supports interrupt nesting then ensure that
188 * the priority of the tick interrupt is at or below
189 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
190 **********************************************************************/
191
192 for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
193 {
194 /* There is nothing to do here, just iterating to the wanted
195 * insertion position. */
196 }
197 }
198
199 pxNewListItem->pxNext = pxIterator->pxNext;
200 pxNewListItem->pxNext->pxPrevious = pxNewListItem;
201 pxNewListItem->pxPrevious = pxIterator;
202 pxIterator->pxNext = pxNewListItem;
203
204 /* Remember which list the item is in. This allows fast removal of the
205 * item later. */
206 pxNewListItem->pxContainer = pxList;
207
208 ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems + 1U );
209
210 traceRETURN_vListInsert();
211 }
212 /*-----------------------------------------------------------*/
213
214
uxListRemove(ListItem_t * const pxItemToRemove)215 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
216 {
217 /* The list item knows which list it is in. Obtain the list from the list
218 * item. */
219 List_t * const pxList = pxItemToRemove->pxContainer;
220
221 traceENTER_uxListRemove( pxItemToRemove );
222
223 pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
224 pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
225
226 /* Only used during decision coverage testing. */
227 mtCOVERAGE_TEST_DELAY();
228
229 /* Make sure the index is left pointing to a valid item. */
230 if( pxList->pxIndex == pxItemToRemove )
231 {
232 pxList->pxIndex = pxItemToRemove->pxPrevious;
233 }
234 else
235 {
236 mtCOVERAGE_TEST_MARKER();
237 }
238
239 pxItemToRemove->pxContainer = NULL;
240 ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems - 1U );
241
242 traceRETURN_uxListRemove( pxList->uxNumberOfItems );
243
244 return pxList->uxNumberOfItems;
245 }
246 /*-----------------------------------------------------------*/
247