list.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /*
  27. * This is the list implementation used by the scheduler. While it is tailored
  28. * heavily for the schedulers needs, it is also available for use by
  29. * application code.
  30. *
  31. * list_ts can only store pointers to list_item_ts. Each ListItem_t contains a
  32. * numeric value (xItemValue). Most of the time the lists are sorted in
  33. * descending item value order.
  34. *
  35. * Lists are created already containing one list item. The value of this
  36. * item is the maximum possible that can be stored, it is therefore always at
  37. * the end of the list and acts as a marker. The list member pxHead always
  38. * points to this marker - even though it is at the tail of the list. This
  39. * is because the tail contains a wrap back pointer to the true head of
  40. * the list.
  41. *
  42. * In addition to it's value, each list item contains a pointer to the next
  43. * item in the list (pxNext), a pointer to the list it is in (pxContainer)
  44. * and a pointer to back to the object that contains it. These later two
  45. * pointers are included for efficiency of list manipulation. There is
  46. * effectively a two way link between the object containing the list item and
  47. * the list item itself.
  48. *
  49. *
  50. * \page ListIntroduction List Implementation
  51. * \ingroup FreeRTOSIntro
  52. */
  53. #ifndef LIST_H
  54. #define LIST_H
  55. #ifndef INC_FREERTOS_H
  56. #error "FreeRTOS.h must be included before list.h"
  57. #endif
  58. /*
  59. * The list structure members are modified from within interrupts, and therefore
  60. * by rights should be declared volatile. However, they are only modified in a
  61. * functionally atomic way (within critical sections of with the scheduler
  62. * suspended) and are either passed by reference into a function or indexed via
  63. * a volatile variable. Therefore, in all use cases tested so far, the volatile
  64. * qualifier can be omitted in order to provide a moderate performance
  65. * improvement without adversely affecting functional behaviour. The assembly
  66. * instructions generated by the IAR, ARM and GCC compilers when the respective
  67. * compiler's options were set for maximum optimisation has been inspected and
  68. * deemed to be as intended. That said, as compiler technology advances, and
  69. * especially if aggressive cross module optimisation is used (a use case that
  70. * has not been exercised to any great extend) then it is feasible that the
  71. * volatile qualifier will be needed for correct optimisation. It is expected
  72. * that a compiler removing essential code because, without the volatile
  73. * qualifier on the list structure members and with aggressive cross module
  74. * optimisation, the compiler deemed the code unnecessary will result in
  75. * complete and obvious failure of the scheduler. If this is ever experienced
  76. * then the volatile qualifier can be inserted in the relevant places within the
  77. * list structures by simply defining configLIST_VOLATILE to volatile in
  78. * FreeRTOSConfig.h (as per the example at the bottom of this comment block).
  79. * If configLIST_VOLATILE is not defined then the preprocessor directives below
  80. * will simply #define configLIST_VOLATILE away completely.
  81. *
  82. * To use volatile list structure members then add the following line to
  83. * FreeRTOSConfig.h (without the quotes):
  84. * "#define configLIST_VOLATILE volatile"
  85. */
  86. #ifndef configLIST_VOLATILE
  87. #define configLIST_VOLATILE
  88. #endif /* configSUPPORT_CROSS_MODULE_OPTIMISATION */
  89. /* *INDENT-OFF* */
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93. /* *INDENT-ON* */
  94. /* Macros that can be used to place known values within the list structures,
  95. * then check that the known values do not get corrupted during the execution of
  96. * the application. These may catch the list data structures being overwritten in
  97. * memory. They will not catch data errors caused by incorrect configuration or
  98. * use of FreeRTOS.*/
  99. #if ( configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES == 0 )
  100. /* Define the macros to do nothing. */
  101. #define listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE
  102. #define listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE
  103. #define listFIRST_LIST_INTEGRITY_CHECK_VALUE
  104. #define listSECOND_LIST_INTEGRITY_CHECK_VALUE
  105. #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
  106. #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
  107. #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList )
  108. #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )
  109. #define listTEST_LIST_ITEM_INTEGRITY( pxItem )
  110. #define listTEST_LIST_INTEGRITY( pxList )
  111. #else /* if ( configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES == 0 ) */
  112. /* Define macros that add new members into the list structures. */
  113. #define listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE TickType_t xListItemIntegrityValue1;
  114. #define listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE TickType_t xListItemIntegrityValue2;
  115. #define listFIRST_LIST_INTEGRITY_CHECK_VALUE TickType_t xListIntegrityValue1;
  116. #define listSECOND_LIST_INTEGRITY_CHECK_VALUE TickType_t xListIntegrityValue2;
  117. /* Define macros that set the new structure members to known values. */
  118. #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ) ( pxItem )->xListItemIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
  119. #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ) ( pxItem )->xListItemIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
  120. #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList ) ( pxList )->xListIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
  121. #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList ) ( pxList )->xListIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
  122. /* Define macros that will assert if one of the structure members does not
  123. * contain its expected value. */
  124. #define listTEST_LIST_ITEM_INTEGRITY( pxItem ) configASSERT( ( ( pxItem )->xListItemIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxItem )->xListItemIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
  125. #define listTEST_LIST_INTEGRITY( pxList ) configASSERT( ( ( pxList )->xListIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxList )->xListIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
  126. #endif /* configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES */
  127. /*
  128. * Definition of the only type of object that a list can contain.
  129. */
  130. struct xLIST;
  131. struct xLIST_ITEM
  132. {
  133. listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  134. configLIST_VOLATILE TickType_t xItemValue; /*< The value being listed. In most cases this is used to sort the list in descending order. */
  135. struct xLIST_ITEM * configLIST_VOLATILE pxNext; /*< Pointer to the next ListItem_t in the list. */
  136. struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /*< Pointer to the previous ListItem_t in the list. */
  137. void * pvOwner; /*< Pointer to the object (normally a TCB) that contains the list item. There is therefore a two way link between the object containing the list item and the list item itself. */
  138. struct xLIST * configLIST_VOLATILE pxContainer; /*< Pointer to the list in which this list item is placed (if any). */
  139. listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  140. };
  141. typedef struct xLIST_ITEM ListItem_t; /* For some reason lint wants this as two separate definitions. */
  142. struct xMINI_LIST_ITEM
  143. {
  144. listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  145. configLIST_VOLATILE TickType_t xItemValue;
  146. struct xLIST_ITEM * configLIST_VOLATILE pxNext;
  147. struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
  148. };
  149. typedef struct xMINI_LIST_ITEM MiniListItem_t;
  150. /*
  151. * Definition of the type of queue used by the scheduler.
  152. */
  153. typedef struct xLIST
  154. {
  155. listFIRST_LIST_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  156. volatile UBaseType_t uxNumberOfItems;
  157. ListItem_t * configLIST_VOLATILE pxIndex; /*< Used to walk through the list. Points to the last item returned by a call to listGET_OWNER_OF_NEXT_ENTRY (). */
  158. MiniListItem_t xListEnd; /*< List item that contains the maximum possible item value meaning it is always at the end of the list and is therefore used as a marker. */
  159. listSECOND_LIST_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  160. } List_t;
  161. /*
  162. * Access macro to set the owner of a list item. The owner of a list item
  163. * is the object (usually a TCB) that contains the list item.
  164. *
  165. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  166. * \ingroup LinkedList
  167. */
  168. #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )
  169. /*
  170. * Access macro to get the owner of a list item. The owner of a list item
  171. * is the object (usually a TCB) that contains the list item.
  172. *
  173. * \page listGET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  174. * \ingroup LinkedList
  175. */
  176. #define listGET_LIST_ITEM_OWNER( pxListItem ) ( ( pxListItem )->pvOwner )
  177. /*
  178. * Access macro to set the value of the list item. In most cases the value is
  179. * used to sort the list in descending order.
  180. *
  181. * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
  182. * \ingroup LinkedList
  183. */
  184. #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( ( pxListItem )->xItemValue = ( xValue ) )
  185. /*
  186. * Access macro to retrieve the value of the list item. The value can
  187. * represent anything - for example the priority of a task, or the time at
  188. * which a task should be unblocked.
  189. *
  190. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  191. * \ingroup LinkedList
  192. */
  193. #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )
  194. /*
  195. * Access macro to retrieve the value of the list item at the head of a given
  196. * list.
  197. *
  198. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  199. * \ingroup LinkedList
  200. */
  201. #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext->xItemValue )
  202. /*
  203. * Return the list item at the head of the list.
  204. *
  205. * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
  206. * \ingroup LinkedList
  207. */
  208. #define listGET_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext )
  209. /*
  210. * Return the next list item.
  211. *
  212. * \page listGET_NEXT listGET_NEXT
  213. * \ingroup LinkedList
  214. */
  215. #define listGET_NEXT( pxListItem ) ( ( pxListItem )->pxNext )
  216. /*
  217. * Return the list item that marks the end of the list
  218. *
  219. * \page listGET_END_MARKER listGET_END_MARKER
  220. * \ingroup LinkedList
  221. */
  222. #define listGET_END_MARKER( pxList ) ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )
  223. /*
  224. * Access macro to determine if a list contains any items. The macro will
  225. * only have the value true if the list is empty.
  226. *
  227. * \page listLIST_IS_EMPTY listLIST_IS_EMPTY
  228. * \ingroup LinkedList
  229. */
  230. #define listLIST_IS_EMPTY( pxList ) ( ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) ? pdTRUE : pdFALSE )
  231. /*
  232. * Access macro to return the number of items in the list.
  233. */
  234. #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )
  235. /*
  236. * Access function to obtain the owner of the next entry in a list.
  237. *
  238. * The list member pxIndex is used to walk through a list. Calling
  239. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
  240. * and returns that entry's pxOwner parameter. Using multiple calls to this
  241. * function it is therefore possible to move through every item contained in
  242. * a list.
  243. *
  244. * The pxOwner parameter of a list item is a pointer to the object that owns
  245. * the list item. In the scheduler this is normally a task control block.
  246. * The pxOwner parameter effectively creates a two way link between the list
  247. * item and its owner.
  248. *
  249. * @param pxTCB pxTCB is set to the address of the owner of the next list item.
  250. * @param pxList The list from which the next item owner is to be returned.
  251. *
  252. * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY
  253. * \ingroup LinkedList
  254. */
  255. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
  256. { \
  257. List_t * const pxConstList = ( pxList ); \
  258. /* Increment the index to the next item and return the item, ensuring */ \
  259. /* we don't return the marker used at the end of the list. */ \
  260. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  261. if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
  262. { \
  263. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  264. } \
  265. ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
  266. }
  267. /*
  268. * Access function to obtain the owner of the first entry in a list. Lists
  269. * are normally sorted in ascending item value order.
  270. *
  271. * This function returns the pxOwner member of the first item in the list.
  272. * The pxOwner parameter of a list item is a pointer to the object that owns
  273. * the list item. In the scheduler this is normally a task control block.
  274. * The pxOwner parameter effectively creates a two way link between the list
  275. * item and its owner.
  276. *
  277. * @param pxList The list from which the owner of the head item is to be
  278. * returned.
  279. *
  280. * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY
  281. * \ingroup LinkedList
  282. */
  283. #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( ( &( ( pxList )->xListEnd ) )->pxNext->pvOwner )
  284. /*
  285. * Check to see if a list item is within a list. The list item maintains a
  286. * "container" pointer that points to the list it is in. All this macro does
  287. * is check to see if the container and the list match.
  288. *
  289. * @param pxList The list we want to know if the list item is within.
  290. * @param pxListItem The list item we want to know if is in the list.
  291. * @return pdTRUE if the list item is in the list, otherwise pdFALSE.
  292. */
  293. #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( ( pxListItem )->pxContainer == ( pxList ) ) ? ( pdTRUE ) : ( pdFALSE ) )
  294. /*
  295. * Return the list a list item is contained within (referenced from).
  296. *
  297. * @param pxListItem The list item being queried.
  298. * @return A pointer to the List_t object that references the pxListItem
  299. */
  300. #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pxContainer )
  301. /*
  302. * This provides a crude means of knowing if a list has been initialised, as
  303. * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
  304. * function.
  305. */
  306. #define listLIST_IS_INITIALISED( pxList ) ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )
  307. /*
  308. * Must be called before a list is used! This initialises all the members
  309. * of the list structure and inserts the xListEnd item into the list as a
  310. * marker to the back of the list.
  311. *
  312. * @param pxList Pointer to the list being initialised.
  313. *
  314. * \page vListInitialise vListInitialise
  315. * \ingroup LinkedList
  316. */
  317. void vListInitialise( List_t * const pxList ) PRIVILEGED_FUNCTION;
  318. /*
  319. * Must be called before a list item is used. This sets the list container to
  320. * null so the item does not think that it is already contained in a list.
  321. *
  322. * @param pxItem Pointer to the list item being initialised.
  323. *
  324. * \page vListInitialiseItem vListInitialiseItem
  325. * \ingroup LinkedList
  326. */
  327. void vListInitialiseItem( ListItem_t * const pxItem ) PRIVILEGED_FUNCTION;
  328. /*
  329. * Insert a list item into a list. The item will be inserted into the list in
  330. * a position determined by its item value (descending item value order).
  331. *
  332. * @param pxList The list into which the item is to be inserted.
  333. *
  334. * @param pxNewListItem The item that is to be placed in the list.
  335. *
  336. * \page vListInsert vListInsert
  337. * \ingroup LinkedList
  338. */
  339. void vListInsert( List_t * const pxList,
  340. ListItem_t * const pxNewListItem ) PRIVILEGED_FUNCTION;
  341. /*
  342. * Insert a list item into a list. The item will be inserted in a position
  343. * such that it will be the last item within the list returned by multiple
  344. * calls to listGET_OWNER_OF_NEXT_ENTRY.
  345. *
  346. * The list member pxIndex is used to walk through a list. Calling
  347. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list.
  348. * Placing an item in a list using vListInsertEnd effectively places the item
  349. * in the list position pointed to by pxIndex. This means that every other
  350. * item within the list will be returned by listGET_OWNER_OF_NEXT_ENTRY before
  351. * the pxIndex parameter again points to the item being inserted.
  352. *
  353. * @param pxList The list into which the item is to be inserted.
  354. *
  355. * @param pxNewListItem The list item to be inserted into the list.
  356. *
  357. * \page vListInsertEnd vListInsertEnd
  358. * \ingroup LinkedList
  359. */
  360. void vListInsertEnd( List_t * const pxList,
  361. ListItem_t * const pxNewListItem ) PRIVILEGED_FUNCTION;
  362. /*
  363. * Remove an item from a list. The list item has a pointer to the list that
  364. * it is in, so only the list item need be passed into the function.
  365. *
  366. * @param uxListRemove The item to be removed. The item will remove itself from
  367. * the list pointed to by it's pxContainer parameter.
  368. *
  369. * @return The number of items that remain in the list after the list item has
  370. * been removed.
  371. *
  372. * \page uxListRemove uxListRemove
  373. * \ingroup LinkedList
  374. */
  375. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove ) PRIVILEGED_FUNCTION;
  376. /* *INDENT-OFF* */
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. /* *INDENT-ON* */
  381. #endif /* ifndef LIST_H */