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