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