list.c 11 KB

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