list.c 9.3 KB

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