]> begriffs open source - cmsis-freertos/blob - Source/list.c
Initial commit
[cmsis-freertos] / Source / list.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 #include <stdlib.h>
72 #include "FreeRTOS.h"
73 #include "list.h"
74
75 /*-----------------------------------------------------------
76  * PUBLIC LIST API documented in list.h
77  *----------------------------------------------------------*/
78
79 void vListInitialise( List_t * const pxList )
80 {
81         /* The list structure contains a list item which is used to mark the
82         end of the list.  To initialise the list the list end is inserted
83         as the only list entry. */
84         pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );                       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
85
86         /* The list end value is the highest possible value in the list to
87         ensure it remains at the end of the list. */
88         pxList->xListEnd.xItemValue = portMAX_DELAY;
89
90         /* The list end next and previous pointers point to itself so we know
91         when the list is empty. */
92         pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );       /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
93         pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );/*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
94
95         pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
96
97         /* Write known values into the list if
98         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
99         listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
100         listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
101 }
102 /*-----------------------------------------------------------*/
103
104 void vListInitialiseItem( ListItem_t * const pxItem )
105 {
106         /* Make sure the list item is not recorded as being on a list. */
107         pxItem->pvContainer = NULL;
108
109         /* Write known values into the list item if
110         configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
111         listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
112         listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
113 }
114 /*-----------------------------------------------------------*/
115
116 void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
117 {
118 ListItem_t * const pxIndex = pxList->pxIndex;
119
120         /* Only effective when configASSERT() is also defined, these tests may catch
121         the list data structures being overwritten in memory.  They will not catch
122         data errors caused by incorrect configuration or use of FreeRTOS. */
123         listTEST_LIST_INTEGRITY( pxList );
124         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
125
126         /* Insert a new list item into pxList, but rather than sort the list,
127         makes the new list item the last item to be removed by a call to
128         listGET_OWNER_OF_NEXT_ENTRY(). */
129         pxNewListItem->pxNext = pxIndex;
130         pxNewListItem->pxPrevious = pxIndex->pxPrevious;
131
132         /* Only used during decision coverage testing. */
133         mtCOVERAGE_TEST_DELAY();
134
135         pxIndex->pxPrevious->pxNext = pxNewListItem;
136         pxIndex->pxPrevious = pxNewListItem;
137
138         /* Remember which list the item is in. */
139         pxNewListItem->pvContainer = ( void * ) pxList;
140
141         ( pxList->uxNumberOfItems )++;
142 }
143 /*-----------------------------------------------------------*/
144
145 void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )
146 {
147 ListItem_t *pxIterator;
148 const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
149
150         /* Only effective when configASSERT() is also defined, these tests may catch
151         the list data structures being overwritten in memory.  They will not catch
152         data errors caused by incorrect configuration or use of FreeRTOS. */
153         listTEST_LIST_INTEGRITY( pxList );
154         listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
155
156         /* Insert the new list item into the list, sorted in xItemValue order.
157
158         If the list already contains a list item with the same item value then the
159         new list item should be placed after it.  This ensures that TCB's which are
160         stored in ready lists (all of which have the same xItemValue value) get a
161         share of the CPU.  However, if the xItemValue is the same as the back marker
162         the iteration loop below will not end.  Therefore the value is checked
163         first, and the algorithm slightly modified if necessary. */
164         if( xValueOfInsertion == portMAX_DELAY )
165         {
166                 pxIterator = pxList->xListEnd.pxPrevious;
167         }
168         else
169         {
170                 /* *** NOTE ***********************************************************
171                 If you find your application is crashing here then likely causes are
172                 listed below.  In addition see http://www.freertos.org/FAQHelp.html for
173                 more tips, and ensure configASSERT() is defined!
174                 http://www.freertos.org/a00110.html#configASSERT
175
176                         1) Stack overflow -
177                            see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
178                         2) Incorrect interrupt priority assignment, especially on Cortex-M
179                            parts where numerically high priority values denote low actual
180                            interrupt priorities, which can seem counter intuitive.  See
181                            http://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition
182                            of configMAX_SYSCALL_INTERRUPT_PRIORITY on
183                            http://www.freertos.org/a00110.html
184                         3) Calling an API function from within a critical section or when
185                            the scheduler is suspended, or calling an API function that does
186                            not end in "FromISR" from an interrupt.
187                         4) Using a queue or semaphore before it has been initialised or
188                            before the scheduler has been started (are interrupts firing
189                            before vTaskStartScheduler() has been called?).
190                 **********************************************************************/
191
192                 for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */
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->pvContainer = ( void * ) pxList;
207
208         ( pxList->uxNumberOfItems )++;
209 }
210 /*-----------------------------------------------------------*/
211
212 UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
213 {
214 /* The list item knows which list it is in.  Obtain the list from the list
215 item. */
216 List_t * const pxList = ( List_t * ) pxItemToRemove->pvContainer;
217
218         pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
219         pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
220
221         /* Only used during decision coverage testing. */
222         mtCOVERAGE_TEST_DELAY();
223
224         /* Make sure the index is left pointing to a valid item. */
225         if( pxList->pxIndex == pxItemToRemove )
226         {
227                 pxList->pxIndex = pxItemToRemove->pxPrevious;
228         }
229         else
230         {
231                 mtCOVERAGE_TEST_MARKER();
232         }
233
234         pxItemToRemove->pvContainer = NULL;
235         ( pxList->uxNumberOfItems )--;
236
237         return pxList->uxNumberOfItems;
238 }
239 /*-----------------------------------------------------------*/
240