]> begriffs open source - cmsis-freertos/blob - Source/list.c
Merge branch 'develop'
[cmsis-freertos] / Source / list.c
1 /*
2  * FreeRTOS Kernel V10.4.6
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 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
41  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
42  * defined for the header files above, but not in this file, in order to
43  * generate the correct privileged Vs unprivileged linkage and placement. */
44 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
45
46 /*-----------------------------------------------------------
47 * PUBLIC LIST API documented in list.h
48 *----------------------------------------------------------*/
49
50 void vListInitialise( List_t * const pxList )
51 {
52     /* The list structure contains a list item which is used to mark the
53      * end of the list.  To initialise the list the list end is inserted
54      * as the only list entry. */
55     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. */
56
57     /* The list end value is the highest possible value in the list to
58      * ensure it remains at the end of the list. */
59     pxList->xListEnd.xItemValue = portMAX_DELAY;
60
61     /* The list end next and previous pointers point to itself so we know
62      * when the list is empty. */
63     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. */
64     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. */
65
66     pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
67
68     /* Write known values into the list if
69      * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
70     listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
71     listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
72 }
73 /*-----------------------------------------------------------*/
74
75 void vListInitialiseItem( ListItem_t * const pxItem )
76 {
77     /* Make sure the list item is not recorded as being on a list. */
78     pxItem->pxContainer = NULL;
79
80     /* Write known values into the list item if
81      * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
82     listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
83     listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
84 }
85 /*-----------------------------------------------------------*/
86
87 void vListInsertEnd( List_t * const pxList,
88                      ListItem_t * const pxNewListItem )
89 {
90     ListItem_t * const pxIndex = pxList->pxIndex;
91
92     /* Only effective when configASSERT() is also defined, these tests may catch
93      * the list data structures being overwritten in memory.  They will not catch
94      * data errors caused by incorrect configuration or use of FreeRTOS. */
95     listTEST_LIST_INTEGRITY( pxList );
96     listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
97
98     /* Insert a new list item into pxList, but rather than sort the list,
99      * makes the new list item the last item to be removed by a call to
100      * listGET_OWNER_OF_NEXT_ENTRY(). */
101     pxNewListItem->pxNext = pxIndex;
102     pxNewListItem->pxPrevious = pxIndex->pxPrevious;
103
104     /* Only used during decision coverage testing. */
105     mtCOVERAGE_TEST_DELAY();
106
107     pxIndex->pxPrevious->pxNext = pxNewListItem;
108     pxIndex->pxPrevious = pxNewListItem;
109
110     /* Remember which list the item is in. */
111     pxNewListItem->pxContainer = pxList;
112
113     ( pxList->uxNumberOfItems )++;
114 }
115 /*-----------------------------------------------------------*/
116
117 void vListInsert( List_t * const pxList,
118                   ListItem_t * const pxNewListItem )
119 {
120     ListItem_t * pxIterator;
121     const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
122
123     /* Only effective when configASSERT() is also defined, these tests may catch
124      * the list data structures being overwritten in memory.  They will not catch
125      * data errors caused by incorrect configuration or use of FreeRTOS. */
126     listTEST_LIST_INTEGRITY( pxList );
127     listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
128
129     /* Insert the new list item into the list, sorted in xItemValue order.
130      *
131      * If the list already contains a list item with the same item value then the
132      * new list item should be placed after it.  This ensures that TCBs which are
133      * stored in ready lists (all of which have the same xItemValue value) get a
134      * share of the CPU.  However, if the xItemValue is the same as the back marker
135      * the iteration loop below will not end.  Therefore the value is checked
136      * first, and the algorithm slightly modified if necessary. */
137     if( xValueOfInsertion == portMAX_DELAY )
138     {
139         pxIterator = pxList->xListEnd.pxPrevious;
140     }
141     else
142     {
143         /* *** NOTE ***********************************************************
144         *  If you find your application is crashing here then likely causes are
145         *  listed below.  In addition see https://www.FreeRTOS.org/FAQHelp.html for
146         *  more tips, and ensure configASSERT() is defined!
147         *  https://www.FreeRTOS.org/a00110.html#configASSERT
148         *
149         *   1) Stack overflow -
150         *      see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
151         *   2) Incorrect interrupt priority assignment, especially on Cortex-M
152         *      parts where numerically high priority values denote low actual
153         *      interrupt priorities, which can seem counter intuitive.  See
154         *      https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html and the definition
155         *      of configMAX_SYSCALL_INTERRUPT_PRIORITY on
156         *      https://www.FreeRTOS.org/a00110.html
157         *   3) Calling an API function from within a critical section or when
158         *      the scheduler is suspended, or calling an API function that does
159         *      not end in "FromISR" from an interrupt.
160         *   4) Using a queue or semaphore before it has been initialised or
161         *      before the scheduler has been started (are interrupts firing
162         *      before vTaskStartScheduler() has been called?).
163         *   5) If the FreeRTOS port supports interrupt nesting then ensure that
164         *      the priority of the tick interrupt is at or below
165         *      configMAX_SYSCALL_INTERRUPT_PRIORITY.
166         **********************************************************************/
167
168         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. */
169         {
170             /* There is nothing to do here, just iterating to the wanted
171              * insertion position. */
172         }
173     }
174
175     pxNewListItem->pxNext = pxIterator->pxNext;
176     pxNewListItem->pxNext->pxPrevious = pxNewListItem;
177     pxNewListItem->pxPrevious = pxIterator;
178     pxIterator->pxNext = pxNewListItem;
179
180     /* Remember which list the item is in.  This allows fast removal of the
181      * item later. */
182     pxNewListItem->pxContainer = pxList;
183
184     ( pxList->uxNumberOfItems )++;
185 }
186 /*-----------------------------------------------------------*/
187
188 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
189 {
190 /* The list item knows which list it is in.  Obtain the list from the list
191  * item. */
192     List_t * const pxList = pxItemToRemove->pxContainer;
193
194     pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
195     pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
196
197     /* Only used during decision coverage testing. */
198     mtCOVERAGE_TEST_DELAY();
199
200     /* Make sure the index is left pointing to a valid item. */
201     if( pxList->pxIndex == pxItemToRemove )
202     {
203         pxList->pxIndex = pxItemToRemove->pxPrevious;
204     }
205     else
206     {
207         mtCOVERAGE_TEST_MARKER();
208     }
209
210     pxItemToRemove->pxContainer = NULL;
211     ( pxList->uxNumberOfItems )--;
212
213     return pxList->uxNumberOfItems;
214 }
215 /*-----------------------------------------------------------*/