list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * FreeRTOS Kernel V10.5.1 (ESP-IDF SMP modified)
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-FileCopyrightText: 2021 Amazon.com, Inc. or its affiliates
  6. *
  7. * SPDX-License-Identifier: MIT
  8. *
  9. * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * https://www.FreeRTOS.org
  29. * https://github.com/FreeRTOS
  30. *
  31. */
  32. #include <stdlib.h>
  33. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  34. * all the API functions to use the MPU wrappers. That should only be done when
  35. * task.h is included from an application file. */
  36. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  37. #include "FreeRTOS.h"
  38. #include "list.h"
  39. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  40. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
  41. * defined for the header files above, but not in this file, in order to
  42. * generate the correct privileged Vs unprivileged linkage and placement. */
  43. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  44. /*-----------------------------------------------------------
  45. * PUBLIC LIST API documented in list.h
  46. *----------------------------------------------------------*/
  47. void vListInitialise( List_t * const pxList )
  48. {
  49. /* The list structure contains a list item which is used to mark the
  50. * end of the list. To initialise the list the list end is inserted
  51. * as the only list entry. */
  52. 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. */
  53. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
  54. /* The list end value is the highest possible value in the list to
  55. * ensure it remains at the end of the list. */
  56. pxList->xListEnd.xItemValue = portMAX_DELAY;
  57. /* The list end next and previous pointers point to itself so we know
  58. * when the list is empty. */
  59. 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. */
  60. 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. */
  61. /* Initialize the remaining fields of xListEnd when it is a proper ListItem_t */
  62. #if ( configUSE_MINI_LIST_ITEM == 0 )
  63. {
  64. pxList->xListEnd.pvOwner = NULL;
  65. pxList->xListEnd.pxContainer = NULL;
  66. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
  67. }
  68. #endif
  69. pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
  70. /* Write known values into the list if
  71. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  72. listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
  73. listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
  74. }
  75. /*-----------------------------------------------------------*/
  76. void vListInitialiseItem( ListItem_t * const pxItem )
  77. {
  78. /* Make sure the list item is not recorded as being on a list. */
  79. pxItem->pxContainer = NULL;
  80. /* Write known values into the list item if
  81. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  82. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  83. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  84. }
  85. /*-----------------------------------------------------------*/
  86. void vListInsertEnd( List_t * const pxList,
  87. ListItem_t * const pxNewListItem )
  88. {
  89. ListItem_t * const pxIndex = pxList->pxIndex;
  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 )++;
  107. }
  108. /*-----------------------------------------------------------*/
  109. void vListInsert( List_t * const pxList,
  110. ListItem_t * const pxNewListItem )
  111. {
  112. ListItem_t * pxIterator;
  113. const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
  114. /* Only effective when configASSERT() is also defined, these tests may catch
  115. * the list data structures being overwritten in memory. They will not catch
  116. * data errors caused by incorrect configuration or use of FreeRTOS. */
  117. listTEST_LIST_INTEGRITY( pxList );
  118. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  119. /* Insert the new list item into the list, sorted in xItemValue order.
  120. *
  121. * If the list already contains a list item with the same item value then the
  122. * new list item should be placed after it. This ensures that TCBs which are
  123. * stored in ready lists (all of which have the same xItemValue value) get a
  124. * share of the CPU. However, if the xItemValue is the same as the back marker
  125. * the iteration loop below will not end. Therefore the value is checked
  126. * first, and the algorithm slightly modified if necessary. */
  127. if( xValueOfInsertion == portMAX_DELAY )
  128. {
  129. pxIterator = pxList->xListEnd.pxPrevious;
  130. }
  131. else
  132. {
  133. /* *** NOTE ***********************************************************
  134. * If you find your application is crashing here then likely causes are
  135. * listed below. In addition see https://www.FreeRTOS.org/FAQHelp.html for
  136. * more tips, and ensure configASSERT() is defined!
  137. * https://www.FreeRTOS.org/a00110.html#configASSERT
  138. *
  139. * 1) Stack overflow -
  140. * see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
  141. * 2) Incorrect interrupt priority assignment, especially on Cortex-M
  142. * parts where numerically high priority values denote low actual
  143. * interrupt priorities, which can seem counter intuitive. See
  144. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html and the definition
  145. * of configMAX_SYSCALL_INTERRUPT_PRIORITY on
  146. * https://www.FreeRTOS.org/a00110.html
  147. * 3) Calling an API function from within a critical section or when
  148. * the scheduler is suspended, or calling an API function that does
  149. * not end in "FromISR" from an interrupt.
  150. * 4) Using a queue or semaphore before it has been initialised or
  151. * before the scheduler has been started (are interrupts firing
  152. * before vTaskStartScheduler() has been called?).
  153. * 5) If the FreeRTOS port supports interrupt nesting then ensure that
  154. * the priority of the tick interrupt is at or below
  155. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  156. **********************************************************************/
  157. 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. */
  158. {
  159. /* There is nothing to do here, just iterating to the wanted
  160. * insertion position. */
  161. }
  162. }
  163. pxNewListItem->pxNext = pxIterator->pxNext;
  164. pxNewListItem->pxNext->pxPrevious = pxNewListItem;
  165. pxNewListItem->pxPrevious = pxIterator;
  166. pxIterator->pxNext = pxNewListItem;
  167. /* Remember which list the item is in. This allows fast removal of the
  168. * item later. */
  169. pxNewListItem->pxContainer = pxList;
  170. ( pxList->uxNumberOfItems )++;
  171. }
  172. /*-----------------------------------------------------------*/
  173. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
  174. {
  175. /* The list item knows which list it is in. Obtain the list from the list
  176. * item. */
  177. List_t * const pxList = pxItemToRemove->pxContainer;
  178. pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
  179. pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
  180. /* Only used during decision coverage testing. */
  181. mtCOVERAGE_TEST_DELAY();
  182. /* Make sure the index is left pointing to a valid item. */
  183. if( pxList->pxIndex == pxItemToRemove )
  184. {
  185. pxList->pxIndex = pxItemToRemove->pxPrevious;
  186. }
  187. else
  188. {
  189. mtCOVERAGE_TEST_MARKER();
  190. }
  191. pxItemToRemove->pxContainer = NULL;
  192. ( pxList->uxNumberOfItems )--;
  193. return pxList->uxNumberOfItems;
  194. }
  195. /*-----------------------------------------------------------*/