list.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * FreeRTOS Kernel V10.2.1
  3. * Copyright (C) 2019 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. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. #include <stdlib.h>
  28. #include "FreeRTOS.h"
  29. #include "list.h"
  30. /*-----------------------------------------------------------
  31. * PUBLIC LIST API documented in list.h
  32. *----------------------------------------------------------*/
  33. void vListInitialise( List_t * const pxList )
  34. {
  35. /* The list structure contains a list item which is used to mark the
  36. end of the list. To initialise the list the list end is inserted
  37. as the only list entry. */
  38. 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. */
  39. /* The list end value is the highest possible value in the list to
  40. ensure it remains at the end of the list. */
  41. pxList->xListEnd.xItemValue = portMAX_DELAY;
  42. /* The list end next and previous pointers point to itself so we know
  43. when the list is empty. */
  44. 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. */
  45. 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. */
  46. pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
  47. /* Write known values into the list if
  48. configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  49. listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
  50. listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
  51. }
  52. /*-----------------------------------------------------------*/
  53. void vListInitialiseItem( ListItem_t * const pxItem )
  54. {
  55. /* Make sure the list item is not recorded as being on a list. */
  56. pxItem->pxContainer = NULL;
  57. /* Write known values into the list item if
  58. configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  59. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  60. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
  61. }
  62. /*-----------------------------------------------------------*/
  63. void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
  64. {
  65. ListItem_t * const pxIndex = pxList->pxIndex;
  66. /* Only effective when configASSERT() is also defined, these tests may catch
  67. the list data structures being overwritten in memory. They will not catch
  68. data errors caused by incorrect configuration or use of FreeRTOS. */
  69. listTEST_LIST_INTEGRITY( pxList );
  70. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  71. /* Insert a new list item into pxList, but rather than sort the list,
  72. makes the new list item the last item to be removed by a call to
  73. listGET_OWNER_OF_NEXT_ENTRY(). */
  74. pxNewListItem->pxNext = pxIndex;
  75. pxNewListItem->pxPrevious = pxIndex->pxPrevious;
  76. /* Only used during decision coverage testing. */
  77. mtCOVERAGE_TEST_DELAY();
  78. pxIndex->pxPrevious->pxNext = pxNewListItem;
  79. pxIndex->pxPrevious = pxNewListItem;
  80. /* Remember which list the item is in. */
  81. pxNewListItem->pxContainer = pxList;
  82. ( pxList->uxNumberOfItems )++;
  83. }
  84. /*-----------------------------------------------------------*/
  85. void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )
  86. {
  87. ListItem_t *pxIterator;
  88. const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
  89. /* Only effective when configASSERT() is also defined, these tests may catch
  90. the list data structures being overwritten in memory. They will not catch
  91. data errors caused by incorrect configuration or use of FreeRTOS. */
  92. listTEST_LIST_INTEGRITY( pxList );
  93. listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
  94. /* Insert the new list item into the list, sorted in xItemValue order.
  95. If the list already contains a list item with the same item value then the
  96. new list item should be placed after it. This ensures that TCBs which are
  97. stored in ready lists (all of which have the same xItemValue value) get a
  98. share of the CPU. However, if the xItemValue is the same as the back marker
  99. the iteration loop below will not end. Therefore the value is checked
  100. first, and the algorithm slightly modified if necessary. */
  101. if( xValueOfInsertion == portMAX_DELAY )
  102. {
  103. pxIterator = pxList->xListEnd.pxPrevious;
  104. }
  105. else
  106. {
  107. /* *** NOTE ***********************************************************
  108. If you find your application is crashing here then likely causes are
  109. listed below. In addition see https://www.freertos.org/FAQHelp.html for
  110. more tips, and ensure configASSERT() is defined!
  111. https://www.freertos.org/a00110.html#configASSERT
  112. 1) Stack overflow -
  113. see https://www.freertos.org/Stacks-and-stack-overflow-checking.html
  114. 2) Incorrect interrupt priority assignment, especially on Cortex-M
  115. parts where numerically high priority values denote low actual
  116. interrupt priorities, which can seem counter intuitive. See
  117. https://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition
  118. of configMAX_SYSCALL_INTERRUPT_PRIORITY on
  119. https://www.freertos.org/a00110.html
  120. 3) Calling an API function from within a critical section or when
  121. the scheduler is suspended, or calling an API function that does
  122. not end in "FromISR" from an interrupt.
  123. 4) Using a queue or semaphore before it has been initialised or
  124. before the scheduler has been started (are interrupts firing
  125. before vTaskStartScheduler() has been called?).
  126. **********************************************************************/
  127. 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. */
  128. {
  129. /* There is nothing to do here, just iterating to the wanted
  130. insertion position. */
  131. }
  132. }
  133. pxNewListItem->pxNext = pxIterator->pxNext;
  134. pxNewListItem->pxNext->pxPrevious = pxNewListItem;
  135. pxNewListItem->pxPrevious = pxIterator;
  136. pxIterator->pxNext = pxNewListItem;
  137. /* Remember which list the item is in. This allows fast removal of the
  138. item later. */
  139. pxNewListItem->pxContainer = pxList;
  140. ( pxList->uxNumberOfItems )++;
  141. }
  142. /*-----------------------------------------------------------*/
  143. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
  144. {
  145. /* The list item knows which list it is in. Obtain the list from the list
  146. item. */
  147. List_t * const pxList = pxItemToRemove->pxContainer;
  148. pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
  149. pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
  150. /* Only used during decision coverage testing. */
  151. mtCOVERAGE_TEST_DELAY();
  152. /* Make sure the index is left pointing to a valid item. */
  153. if( pxList->pxIndex == pxItemToRemove )
  154. {
  155. pxList->pxIndex = pxItemToRemove->pxPrevious;
  156. }
  157. else
  158. {
  159. mtCOVERAGE_TEST_MARKER();
  160. }
  161. pxItemToRemove->pxContainer = NULL;
  162. ( pxList->uxNumberOfItems )--;
  163. return pxList->uxNumberOfItems;
  164. }
  165. /*-----------------------------------------------------------*/