list.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. */
  8. #include <stdlib.h>
  9. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  10. * all the API functions to use the MPU wrappers. That should only be done when
  11. * task.h is included from an application file. */
  12. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  13. #include "FreeRTOS.h"
  14. #include "list.h"
  15. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  16. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be
  17. * defined for the header files above, but not in this file, in order to
  18. * generate the correct privileged Vs unprivileged linkage and placement. */
  19. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  20. void vListInitialise(List_t * const pxList)
  21. {
  22. /* The list structure contains a list item which is used to mark the
  23. * end of the list. To initialise the list the list end is inserted
  24. * as the only list entry. */
  25. pxList->pxIndex = (ListItem_t *) &(pxList->xListEnd);
  26. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(&(pxList->xListEnd));
  27. /* The list end value is the highest possible value in the list to
  28. * ensure it remains at the end of the list. */
  29. pxList->xListEnd.xItemValue = portMAX_DELAY;
  30. /* The list end next and previous pointers point to itself so we know
  31. * when the list is empty. */
  32. pxList->xListEnd.pxNext = (ListItem_t *) &(pxList->xListEnd);
  33. pxList->xListEnd.pxPrevious = (ListItem_t *) &(pxList->xListEnd);
  34. /* Initialize the remaining fields of xListEnd when it is a proper ListItem_t */
  35. #if (configUSE_MINI_LIST_ITEM == 0)
  36. {
  37. pxList->xListEnd.pvOwner = NULL;
  38. pxList->xListEnd.pxContainer = NULL;
  39. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(&(pxList->xListEnd));
  40. }
  41. #endif
  42. pxList->uxNumberOfItems = (UBaseType_t)0U;
  43. /* Write known values into the list if
  44. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  45. listSET_LIST_INTEGRITY_CHECK_1_VALUE(pxList);
  46. listSET_LIST_INTEGRITY_CHECK_2_VALUE(pxList);
  47. }
  48. void vListInitialiseItem(ListItem_t * const pxItem)
  49. {
  50. /* Make sure the list item is not recorded as being on a list. */
  51. pxItem->pxContainer = NULL;
  52. /* Write known values into the list item if
  53. * configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  54. listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem);
  55. listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE(pxItem);
  56. }
  57. /**
  58. * pxNewListItem插入到链表的尾部
  59. */
  60. void vListInsertEnd(List_t * const pxList, ListItem_t * const pxNewListItem)
  61. {
  62. ListItem_t * const pxIndex = pxList->pxIndex;
  63. /* Only effective when configASSERT() is also defined, these tests may catch
  64. * the list data structures being overwritten in memory. They will not catch
  65. * data errors caused by incorrect configuration or use of FreeRTOS. */
  66. listTEST_LIST_INTEGRITY(pxList);
  67. listTEST_LIST_ITEM_INTEGRITY(pxNewListItem);
  68. /* Insert a new list item into pxList, but rather than sort the list,
  69. * makes the new list item the last item to be removed by a call to
  70. * listGET_OWNER_OF_NEXT_ENTRY(). */
  71. pxNewListItem->pxNext = pxIndex;
  72. pxNewListItem->pxPrevious = pxIndex->pxPrevious;
  73. /* Only used during decision coverage testing. */
  74. mtCOVERAGE_TEST_DELAY();
  75. pxIndex->pxPrevious->pxNext = pxNewListItem;
  76. pxIndex->pxPrevious = pxNewListItem;
  77. /* Remember which list the item is in. */
  78. pxNewListItem->pxContainer = pxList;
  79. (pxList->uxNumberOfItems)++;
  80. }
  81. /**
  82. * 将pxNewListItem插入到pxList升序链表
  83. * xItemValue为键值,元素从小到大排列
  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. *
  96. * If the list already contains a list item with the same item value then the
  97. * new list item should be placed after it. This ensures that TCBs which are
  98. * stored in ready lists (all of which have the same xItemValue value) get a
  99. * share of the CPU. However, if the xItemValue is the same as the back marker
  100. * the iteration loop below will not end. Therefore the value is checked
  101. * first, and the algorithm slightly modified if necessary.
  102. */
  103. if(xValueOfInsertion == portMAX_DELAY)
  104. {
  105. pxIterator = pxList->xListEnd.pxPrevious;
  106. }
  107. else
  108. {
  109. /* 遍历找到链表插入的位置 */
  110. pxIterator = (ListItem_t *) &(pxList->xListEnd);
  111. while (pxIterator->pxNext->xItemValue <= xValueOfInsertion)
  112. pxIterator = pxIterator->pxNext;
  113. }
  114. pxNewListItem->pxNext = pxIterator->pxNext;
  115. pxNewListItem->pxNext->pxPrevious = pxNewListItem;
  116. pxNewListItem->pxPrevious = pxIterator;
  117. pxIterator->pxNext = pxNewListItem;
  118. /* Remember which list the item is in. This allows fast removal of the
  119. * item later.
  120. */
  121. pxNewListItem->pxContainer = pxList;
  122. (pxList->uxNumberOfItems)++;
  123. }
  124. /**
  125. * 删除pxItemToRemove节点
  126. */
  127. UBaseType_t uxListRemove(ListItem_t * const pxItemToRemove)
  128. {
  129. /* The list item knows which list it is in. Obtain the list from the list
  130. * item.
  131. * 找到节点所在的链表
  132. */
  133. List_t * const pxList = pxItemToRemove->pxContainer;
  134. pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
  135. pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
  136. /* Only used during decision coverage testing. */
  137. mtCOVERAGE_TEST_DELAY();
  138. /* Make sure the index is left pointing to a valid item. */
  139. if(pxList->pxIndex == pxItemToRemove)
  140. {
  141. pxList->pxIndex = pxItemToRemove->pxPrevious;
  142. }
  143. pxItemToRemove->pxContainer = NULL;
  144. (pxList->uxNumberOfItems)--;
  145. /* 返回剩余节点数 */
  146. return pxList->uxNumberOfItems;
  147. }