list.c 9.4 KB

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