list.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * FreeRTOS Kernel V11.1.0
  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. #include <stdlib.h>
  29. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  30. * all the API functions to use the MPU wrappers. That should only be done when
  31. * task.h is included from an application file. */
  32. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  33. #include "FreeRTOS.h"
  34. #include "list.h"
  35. /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
  36. * defined for the header files above, but not in this file, in order to
  37. * generate the correct privileged Vs unprivileged linkage and placement. */
  38. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  39. /*-----------------------------------------------------------
  40. * PUBLIC LIST API documented in list.h
  41. *----------------------------------------------------------*/
  42. void vListInitialise( List_t * const pxList )
  43. {
  44. traceENTER_vListInitialise( pxList );
  45. /* The list structure contains a list item which is used to mark the
  46. * end of the list. To initialise the list the list end is inserted
  47. * as the only list entry. */
  48. pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );
  49. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
  50. /* The list end value is the highest possible value in the list to
  51. * ensure it remains at the end of the list. */
  52. pxList->xListEnd.xItemValue = portMAX_DELAY;
  53. /* The list end next and previous pointers point to itself so we know
  54. * when the list is empty. */
  55. pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );
  56. pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );
  57. /* Initialize the remaining fields of xListEnd when it is a proper ListItem_t */
  58. #if ( configUSE_MINI_LIST_ITEM == 0 )
  59. {
  60. pxList->xListEnd.pvOwner = NULL;
  61. pxList->xListEnd.pxContainer = NULL;
  62. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
  63. }
  64. #endif
  65. pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
  66. /* Write known values into the list if
  67. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  68. listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
  69. listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
  70. traceRETURN_vListInitialise();
  71. }
  72. /*-----------------------------------------------------------*/
  73. void vListInitialiseItem( ListItem_t * const pxItem )
  74. {
  75. traceENTER_vListInitialiseItem( pxItem );
  76. /* Make sure the list item is not recorded as being on a list. */
  77. pxItem->pxContainer = NULL;
  78. /* Write known values into the list item if
  79. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  80. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  81. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  82. traceRETURN_vListInitialiseItem();
  83. }
  84. /*-----------------------------------------------------------*/
  85. void vListInsertEnd( List_t * const pxList,
  86. ListItem_t * const pxNewListItem )
  87. {
  88. ListItem_t * const pxIndex = pxList->pxIndex;
  89. traceENTER_vListInsertEnd( pxList, pxNewListItem );
  90. /* Only effective when configASSERT() is also defined, these tests may catch
  91. * the list data structures being overwritten in memory. They will not catch
  92. * data errors caused by incorrect configuration or use of FreeRTOS. */
  93. listTEST_LIST_INTEGRITY( pxList );
  94. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  95. /* Insert a new list item into pxList, but rather than sort the list,
  96. * makes the new list item the last item to be removed by a call to
  97. * listGET_OWNER_OF_NEXT_ENTRY(). */
  98. pxNewListItem->pxNext = pxIndex;
  99. pxNewListItem->pxPrevious = pxIndex->pxPrevious;
  100. /* Only used during decision coverage testing. */
  101. mtCOVERAGE_TEST_DELAY();
  102. pxIndex->pxPrevious->pxNext = pxNewListItem;
  103. pxIndex->pxPrevious = pxNewListItem;
  104. /* Remember which list the item is in. */
  105. pxNewListItem->pxContainer = pxList;
  106. ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems + 1U );
  107. traceRETURN_vListInsertEnd();
  108. }
  109. /*-----------------------------------------------------------*/
  110. void vListInsert( List_t * const pxList,
  111. ListItem_t * const pxNewListItem )
  112. {
  113. ListItem_t * pxIterator;
  114. const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
  115. traceENTER_vListInsert( pxList, pxNewListItem );
  116. /* Only effective when configASSERT() is also defined, these tests may catch
  117. * the list data structures being overwritten in memory. They will not catch
  118. * data errors caused by incorrect configuration or use of FreeRTOS. */
  119. listTEST_LIST_INTEGRITY( pxList );
  120. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  121. /* Insert the new list item into the list, sorted in xItemValue order.
  122. *
  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 TCBs 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 https://www.FreeRTOS.org/FAQHelp.html for
  138. * more tips, and ensure configASSERT() is defined!
  139. * https://www.FreeRTOS.org/a00110.html#configASSERT
  140. *
  141. * 1) Stack overflow -
  142. * see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
  143. * 2) Incorrect interrupt priority assignment, especially on Cortex-M
  144. * parts where numerically high priority values denote low actual
  145. * interrupt priorities, which can seem counter intuitive. See
  146. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html and the definition
  147. * of configMAX_SYSCALL_INTERRUPT_PRIORITY on
  148. * https://www.FreeRTOS.org/a00110.html
  149. * 3) Calling an API function from within a critical section or when
  150. * the scheduler is suspended, or calling an API function that does
  151. * not end in "FromISR" from an interrupt.
  152. * 4) Using a queue or semaphore before it has been initialised or
  153. * before the scheduler has been started (are interrupts firing
  154. * before vTaskStartScheduler() has been called?).
  155. * 5) If the FreeRTOS port supports interrupt nesting then ensure that
  156. * the priority of the tick interrupt is at or below
  157. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  158. **********************************************************************/
  159. for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
  160. {
  161. /* There is nothing to do here, just iterating to the wanted
  162. * insertion position. */
  163. }
  164. }
  165. pxNewListItem->pxNext = pxIterator->pxNext;
  166. pxNewListItem->pxNext->pxPrevious = pxNewListItem;
  167. pxNewListItem->pxPrevious = pxIterator;
  168. pxIterator->pxNext = pxNewListItem;
  169. /* Remember which list the item is in. This allows fast removal of the
  170. * item later. */
  171. pxNewListItem->pxContainer = pxList;
  172. ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems + 1U );
  173. traceRETURN_vListInsert();
  174. }
  175. /*-----------------------------------------------------------*/
  176. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
  177. {
  178. /* The list item knows which list it is in. Obtain the list from the list
  179. * item. */
  180. List_t * const pxList = pxItemToRemove->pxContainer;
  181. traceENTER_uxListRemove( pxItemToRemove );
  182. pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
  183. pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
  184. /* Only used during decision coverage testing. */
  185. mtCOVERAGE_TEST_DELAY();
  186. /* Make sure the index is left pointing to a valid item. */
  187. if( pxList->pxIndex == pxItemToRemove )
  188. {
  189. pxList->pxIndex = pxItemToRemove->pxPrevious;
  190. }
  191. else
  192. {
  193. mtCOVERAGE_TEST_MARKER();
  194. }
  195. pxItemToRemove->pxContainer = NULL;
  196. ( pxList->uxNumberOfItems ) = ( UBaseType_t ) ( pxList->uxNumberOfItems - 1U );
  197. traceRETURN_uxListRemove( pxList->uxNumberOfItems );
  198. return pxList->uxNumberOfItems;
  199. }
  200. /*-----------------------------------------------------------*/