uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
list.c
Go to the documentation of this file.
1 /*
2  FreeRTOS V7.5.3 - Copyright (C) 2013 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  ***************************************************************************
8  * *
9  * FreeRTOS provides completely free yet professionally developed, *
10  * robust, strictly quality controlled, supported, and cross *
11  * platform software that has become a de facto standard. *
12  * *
13  * Help yourself get started quickly and support the FreeRTOS *
14  * project by purchasing a FreeRTOS tutorial book, reference *
15  * manual, or both from: http://www.FreeRTOS.org/Documentation *
16  * *
17  * Thank you! *
18  * *
19  ***************************************************************************
20 
21  This file is part of the FreeRTOS distribution.
22 
23  FreeRTOS is free software; you can redistribute it and/or modify it under
24  the terms of the GNU General Public License (version 2) as published by the
25  Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
26 
27  >>! NOTE: The modification to the GPL is included to allow you to distribute
28  >>! a combined work that includes FreeRTOS without being obliged to provide
29  >>! the source code for proprietary components outside of the FreeRTOS
30  >>! kernel.
31 
32  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
33  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34  FOR A PARTICULAR PURPOSE. Full license text is available from the following
35  link: http://www.freertos.org/a00114.html
36 
37  1 tab == 4 spaces!
38 
39  ***************************************************************************
40  * *
41  * Having a problem? Start by reading the FAQ "My application does *
42  * not run, what could be wrong?" *
43  * *
44  * http://www.FreeRTOS.org/FAQHelp.html *
45  * *
46  ***************************************************************************
47 
48  http://www.FreeRTOS.org - Documentation, books, training, latest versions,
49  license and Real Time Engineers Ltd. contact details.
50 
51  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
52  including FreeRTOS+Trace - an indispensable productivity tool, a DOS
53  compatible FAT file system, and our tiny thread aware UDP/IP stack.
54 
55  http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
56  Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
57  licenses offer ticketed support, indemnification and middleware.
58 
59  http://www.SafeRTOS.com - High Integrity Systems also provide a safety
60  engineered and independently SIL3 certified version for use in safety and
61  mission critical applications that require provable dependability.
62 
63  1 tab == 4 spaces!
64 */
65 
66 
67 #include <stdlib.h>
68 #include "FreeRTOS.h"
69 #include "list.h"
70 
71 /*-----------------------------------------------------------
72  * PUBLIC LIST API documented in list.h
73  *----------------------------------------------------------*/
74 
75 void vListInitialise( xList * const pxList )
76 {
77  /* The list structure contains a list item which is used to mark the
78  end of the list. To initialise the list the list end is inserted
79  as the only list entry. */
80  pxList->pxIndex = ( xListItem * ) &( pxList->xListEnd ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
81 
82  /* The list end value is the highest possible value in the list to
83  ensure it remains at the end of the list. */
85 
86  /* The list end next and previous pointers point to itself so we know
87  when the list is empty. */
88  pxList->xListEnd.pxNext = ( xListItem * ) &( pxList->xListEnd ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
89  pxList->xListEnd.pxPrevious = ( xListItem * ) &( pxList->xListEnd );/*lint !e826 !e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
90 
91  pxList->uxNumberOfItems = ( unsigned portBASE_TYPE ) 0U;
92 }
93 /*-----------------------------------------------------------*/
94 
95 void vListInitialiseItem( xListItem * const pxItem )
96 {
97  /* Make sure the list item is not recorded as being on a list. */
98  pxItem->pvContainer = NULL;
99 }
100 /*-----------------------------------------------------------*/
101 
102 void vListInsertEnd( xList * const pxList, xListItem * const pxNewListItem )
103 {
104 xListItem * pxIndex;
105 
106  /* Insert a new list item into pxList, but rather than sort the list,
107  makes the new list item the last item to be removed by a call to
108  pvListGetOwnerOfNextEntry. */
109  pxIndex = pxList->pxIndex;
110 
111  pxNewListItem->pxNext = pxIndex;
112  pxNewListItem->pxPrevious = pxIndex->pxPrevious;
113  pxIndex->pxPrevious->pxNext = pxNewListItem;
114  pxIndex->pxPrevious = pxNewListItem;
115 
116  /* Remember which list the item is in. */
117  pxNewListItem->pvContainer = ( void * ) pxList;
118 
119  ( pxList->uxNumberOfItems )++;
120 }
121 /*-----------------------------------------------------------*/
122 
123 void vListInsert( xList * const pxList, xListItem * const pxNewListItem )
124 {
125 xListItem *pxIterator;
126 portTickType xValueOfInsertion;
127 
128  /* Insert the new list item into the list, sorted in ulListItem order. */
129  xValueOfInsertion = pxNewListItem->xItemValue;
130 
131  /* If the list already contains a list item with the same item value then
132  the new list item should be placed after it. This ensures that TCB's which
133  are stored in ready lists (all of which have the same ulListItem value)
134  get an equal share of the CPU. However, if the xItemValue is the same as
135  the back marker the iteration loop below will not end. This means we need
136  to guard against this by checking the value first and modifying the
137  algorithm slightly if necessary. */
138  if( xValueOfInsertion == portMAX_DELAY )
139  {
140  pxIterator = pxList->xListEnd.pxPrevious;
141  }
142  else
143  {
144  /* *** NOTE ***********************************************************
145  If you find your application is crashing here then likely causes are:
146  1) Stack overflow -
147  see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
148  2) Incorrect interrupt priority assignment, especially on Cortex-M3
149  parts where numerically high priority values denote low actual
150  interrupt priories, which can seem counter intuitive. See
151  configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html
152  3) Calling an API function from within a critical section or when
153  the scheduler is suspended, or calling an API function that does
154  not end in "FromISR" from an interrupt.
155  4) Using a queue or semaphore before it has been initialised or
156  before the scheduler has been started (are interrupts firing
157  before vTaskStartScheduler() has been called?).
158  See http://www.freertos.org/FAQHelp.html for more tips.
159  **********************************************************************/
160 
161  for( pxIterator = ( xListItem * ) &( 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. */
162  {
163  /* There is nothing to do here, we are just iterating to the
164  wanted insertion position. */
165  }
166  }
167 
168  pxNewListItem->pxNext = pxIterator->pxNext;
169  pxNewListItem->pxNext->pxPrevious = pxNewListItem;
170  pxNewListItem->pxPrevious = pxIterator;
171  pxIterator->pxNext = pxNewListItem;
172 
173  /* Remember which list the item is in. This allows fast removal of the
174  item later. */
175  pxNewListItem->pvContainer = ( void * ) pxList;
176 
177  ( pxList->uxNumberOfItems )++;
178 }
179 /*-----------------------------------------------------------*/
180 
181 unsigned portBASE_TYPE uxListRemove( xListItem * const pxItemToRemove )
182 {
183 xList * pxList;
184 
185  pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
186  pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
187 
188  /* The list item knows which list it is in. Obtain the list from the list
189  item. */
190  pxList = ( xList * ) pxItemToRemove->pvContainer;
191 
192  /* Make sure the index is left pointing to a valid item. */
193  if( pxList->pxIndex == pxItemToRemove )
194  {
195  pxList->pxIndex = pxItemToRemove->pxPrevious;
196  }
197 
198  pxItemToRemove->pvContainer = NULL;
199  ( pxList->uxNumberOfItems )--;
200 
201  return pxList->uxNumberOfItems;
202 }
203 /*-----------------------------------------------------------*/
204