list.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * FreeRTOS Kernel V10.5.1
  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. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  36. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
  37. * defined for the header files above, but not in this file, in order to
  38. * generate the correct privileged Vs unprivileged linkage and placement. */
  39. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  40. /*-----------------------------------------------------------
  41. * PUBLIC LIST API documented in list.h
  42. *----------------------------------------------------------*/
  43. void vListInitialise( List_t * const pxList )
  44. {
  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 ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
  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 ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
  56. 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. */
  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. }
  71. /*-----------------------------------------------------------*/
  72. void vListInitialiseItem( ListItem_t * const pxItem )
  73. {
  74. /* Make sure the list item is not recorded as being on a list. */
  75. pxItem->pxContainer = NULL;
  76. /* Write known values into the list item if
  77. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  78. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  79. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  80. }
  81. /*-----------------------------------------------------------*/
  82. void vListInsertEnd( List_t * const pxList,
  83. ListItem_t * const pxNewListItem )
  84. {
  85. ListItem_t * const pxIndex = pxList->pxIndex;
  86. /* Only effective when configASSERT() is also defined, these tests may catch
  87. * the list data structures being overwritten in memory. They will not catch
  88. * data errors caused by incorrect configuration or use of FreeRTOS. */
  89. listTEST_LIST_INTEGRITY( pxList );
  90. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  91. /* Insert a new list item into pxList, but rather than sort the list,
  92. * makes the new list item the last item to be removed by a call to
  93. * listGET_OWNER_OF_NEXT_ENTRY(). */
  94. pxNewListItem->pxNext = pxIndex;
  95. pxNewListItem->pxPrevious = pxIndex->pxPrevious;
  96. /* Only used during decision coverage testing. */
  97. mtCOVERAGE_TEST_DELAY();
  98. pxIndex->pxPrevious->pxNext = pxNewListItem;
  99. pxIndex->pxPrevious = pxNewListItem;
  100. /* Remember which list the item is in. */
  101. pxNewListItem->pxContainer = pxList;
  102. ( pxList->uxNumberOfItems )++;
  103. }
  104. /*-----------------------------------------------------------*/
  105. void vListInsert( List_t * const pxList,
  106. ListItem_t * const pxNewListItem )
  107. {
  108. ListItem_t * pxIterator;
  109. const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
  110. /* Only effective when configASSERT() is also defined, these tests may catch
  111. * the list data structures being overwritten in memory. They will not catch
  112. * data errors caused by incorrect configuration or use of FreeRTOS. */
  113. listTEST_LIST_INTEGRITY( pxList );
  114. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  115. /* Insert the new list item into the list, sorted in xItemValue order.
  116. *
  117. * If the list already contains a list item with the same item value then the
  118. * new list item should be placed after it. This ensures that TCBs which are
  119. * stored in ready lists (all of which have the same xItemValue value) get a
  120. * share of the CPU. However, if the xItemValue is the same as the back marker
  121. * the iteration loop below will not end. Therefore the value is checked
  122. * first, and the algorithm slightly modified if necessary. */
  123. if( xValueOfInsertion == portMAX_DELAY )
  124. {
  125. pxIterator = pxList->xListEnd.pxPrevious;
  126. }
  127. else
  128. {
  129. /* *** NOTE ***********************************************************
  130. * If you find your application is crashing here then likely causes are
  131. * listed below. In addition see https://www.FreeRTOS.org/FAQHelp.html for
  132. * more tips, and ensure configASSERT() is defined!
  133. * https://www.FreeRTOS.org/a00110.html#configASSERT
  134. *
  135. * 1) Stack overflow -
  136. * see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
  137. * 2) Incorrect interrupt priority assignment, especially on Cortex-M
  138. * parts where numerically high priority values denote low actual
  139. * interrupt priorities, which can seem counter intuitive. See
  140. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html and the definition
  141. * of configMAX_SYSCALL_INTERRUPT_PRIORITY on
  142. * https://www.FreeRTOS.org/a00110.html
  143. * 3) Calling an API function from within a critical section or when
  144. * the scheduler is suspended, or calling an API function that does
  145. * not end in "FromISR" from an interrupt.
  146. * 4) Using a queue or semaphore before it has been initialised or
  147. * before the scheduler has been started (are interrupts firing
  148. * before vTaskStartScheduler() has been called?).
  149. * 5) If the FreeRTOS port supports interrupt nesting then ensure that
  150. * the priority of the tick interrupt is at or below
  151. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  152. **********************************************************************/
  153. 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. */
  154. {
  155. /* There is nothing to do here, just iterating to the wanted
  156. * insertion position. */
  157. }
  158. }
  159. pxNewListItem->pxNext = pxIterator->pxNext;
  160. pxNewListItem->pxNext->pxPrevious = pxNewListItem;
  161. pxNewListItem->pxPrevious = pxIterator;
  162. pxIterator->pxNext = pxNewListItem;
  163. /* Remember which list the item is in. This allows fast removal of the
  164. * item later. */
  165. pxNewListItem->pxContainer = pxList;
  166. ( pxList->uxNumberOfItems )++;
  167. }
  168. /*-----------------------------------------------------------*/
  169. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
  170. {
  171. /* The list item knows which list it is in. Obtain the list from the list
  172. * item. */
  173. List_t * const pxList = pxItemToRemove->pxContainer;
  174. pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
  175. pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
  176. /* Only used during decision coverage testing. */
  177. mtCOVERAGE_TEST_DELAY();
  178. /* Make sure the index is left pointing to a valid item. */
  179. if( pxList->pxIndex == pxItemToRemove )
  180. {
  181. pxList->pxIndex = pxItemToRemove->pxPrevious;
  182. }
  183. else
  184. {
  185. mtCOVERAGE_TEST_MARKER();
  186. }
  187. pxItemToRemove->pxContainer = NULL;
  188. ( pxList->uxNumberOfItems )--;
  189. return pxList->uxNumberOfItems;
  190. }
  191. /*-----------------------------------------------------------*/