list.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /*
  56. * This is the list implementation used by the scheduler. While it is tailored
  57. * heavily for the schedulers needs, it is also available for use by
  58. * application code.
  59. *
  60. * list_ts can only store pointers to list_item_ts. Each ListItem_t contains a
  61. * numeric value (xItemValue). Most of the time the lists are sorted in
  62. * descending item value order.
  63. *
  64. * Lists are created already containing one list item. The value of this
  65. * item is the maximum possible that can be stored, it is therefore always at
  66. * the end of the list and acts as a marker. The list member pxHead always
  67. * points to this marker - even though it is at the tail of the list. This
  68. * is because the tail contains a wrap back pointer to the true head of
  69. * the list.
  70. *
  71. * In addition to it's value, each list item contains a pointer to the next
  72. * item in the list (pxNext), a pointer to the list it is in (pxContainer)
  73. * and a pointer to back to the object that contains it. These later two
  74. * pointers are included for efficiency of list manipulation. There is
  75. * effectively a two way link between the object containing the list item and
  76. * the list item itself.
  77. *
  78. *
  79. * \page ListIntroduction List Implementation
  80. * \ingroup FreeRTOSIntro
  81. */
  82. #ifndef INC_FREERTOS_H
  83. #error FreeRTOS.h must be included before list.h
  84. #endif
  85. #ifndef LIST_H
  86. #define LIST_H
  87. /*
  88. * The list structure members are modified from within interrupts, and therefore
  89. * by rights should be declared volatile. However, they are only modified in a
  90. * functionally atomic way (within critical sections of with the scheduler
  91. * suspended) and are either passed by reference into a function or indexed via
  92. * a volatile variable. Therefore, in all use cases tested so far, the volatile
  93. * qualifier can be omitted in order to provide a moderate performance
  94. * improvement without adversely affecting functional behaviour. The assembly
  95. * instructions generated by the IAR, ARM and GCC compilers when the respective
  96. * compiler's options were set for maximum optimisation has been inspected and
  97. * deemed to be as intended. That said, as compiler technology advances, and
  98. * especially if aggressive cross module optimisation is used (a use case that
  99. * has not been exercised to any great extend) then it is feasible that the
  100. * volatile qualifier will be needed for correct optimisation. It is expected
  101. * that a compiler removing essential code because, without the volatile
  102. * qualifier on the list structure members and with aggressive cross module
  103. * optimisation, the compiler deemed the code unnecessary will result in
  104. * complete and obvious failure of the scheduler. If this is ever experienced
  105. * then the volatile qualifier can be inserted in the relevant places within the
  106. * list structures by simply defining configLIST_VOLATILE to volatile in
  107. * FreeRTOSConfig.h (as per the example at the bottom of this comment block).
  108. * If configLIST_VOLATILE is not defined then the preprocessor directives below
  109. * will simply #define configLIST_VOLATILE away completely.
  110. *
  111. * To use volatile list structure members then add the following line to
  112. * FreeRTOSConfig.h (without the quotes):
  113. * "#define configLIST_VOLATILE volatile"
  114. */
  115. #ifndef configLIST_VOLATILE
  116. #define configLIST_VOLATILE
  117. #endif /* configSUPPORT_CROSS_MODULE_OPTIMISATION */
  118. #ifdef __cplusplus
  119. extern "C" {
  120. #endif
  121. /* Macros that can be used to place known values within the list structures,
  122. then check that the known values do not get corrupted during the execution of
  123. the application. These may catch the list data structures being overwritten in
  124. memory. They will not catch data errors caused by incorrect configuration or
  125. use of FreeRTOS.*/
  126. #if( configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES == 0 )
  127. /* Define the macros to do nothing. */
  128. #define listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE
  129. #define listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE
  130. #define listFIRST_LIST_INTEGRITY_CHECK_VALUE
  131. #define listSECOND_LIST_INTEGRITY_CHECK_VALUE
  132. #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
  133. #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
  134. #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList )
  135. #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )
  136. #define listTEST_LIST_ITEM_INTEGRITY( pxItem )
  137. #define listTEST_LIST_INTEGRITY( pxList )
  138. #else
  139. /* Define macros that add new members into the list structures. */
  140. #define listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE TickType_t xListItemIntegrityValue1;
  141. #define listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE TickType_t xListItemIntegrityValue2;
  142. #define listFIRST_LIST_INTEGRITY_CHECK_VALUE TickType_t xListIntegrityValue1;
  143. #define listSECOND_LIST_INTEGRITY_CHECK_VALUE TickType_t xListIntegrityValue2;
  144. /* Define macros that set the new structure members to known values. */
  145. #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ) ( pxItem )->xListItemIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
  146. #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ) ( pxItem )->xListItemIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
  147. #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList ) ( pxList )->xListIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
  148. #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList ) ( pxList )->xListIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
  149. /* Define macros that will assert if one of the structure members does not
  150. contain its expected value. */
  151. #define listTEST_LIST_ITEM_INTEGRITY( pxItem ) configASSERT( ( ( pxItem )->xListItemIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxItem )->xListItemIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
  152. #define listTEST_LIST_INTEGRITY( pxList ) configASSERT( ( ( pxList )->xListIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxList )->xListIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
  153. #endif /* configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES */
  154. /*
  155. * Definition of the only type of object that a list can contain.
  156. */
  157. struct xLIST_ITEM
  158. {
  159. listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  160. configLIST_VOLATILE TickType_t xItemValue; /*< The value being listed. In most cases this is used to sort the list in descending order. */
  161. struct xLIST_ITEM * configLIST_VOLATILE pxNext; /*< Pointer to the next ListItem_t in the list. */
  162. struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /*< Pointer to the previous ListItem_t in the list. */
  163. 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. */
  164. void * configLIST_VOLATILE pvContainer; /*< Pointer to the list in which this list item is placed (if any). */
  165. listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  166. };
  167. typedef struct xLIST_ITEM ListItem_t; /* For some reason lint wants this as two separate definitions. */
  168. struct xMINI_LIST_ITEM
  169. {
  170. listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  171. configLIST_VOLATILE TickType_t xItemValue;
  172. struct xLIST_ITEM * configLIST_VOLATILE pxNext;
  173. struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
  174. };
  175. typedef struct xMINI_LIST_ITEM MiniListItem_t;
  176. /*
  177. * Definition of the type of queue used by the scheduler.
  178. */
  179. typedef struct xLIST
  180. {
  181. listFIRST_LIST_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  182. configLIST_VOLATILE UBaseType_t uxNumberOfItems;
  183. 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 (). */
  184. 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. */
  185. listSECOND_LIST_INTEGRITY_CHECK_VALUE /*< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
  186. } List_t;
  187. /*
  188. * Access macro to set the owner of a list item. The owner of a list item
  189. * is the object (usually a TCB) that contains the list item.
  190. *
  191. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  192. * \ingroup LinkedList
  193. */
  194. #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( ( pxListItem )->pvOwner = ( void * ) ( pxOwner ) )
  195. /*
  196. * Access macro to get the owner of a list item. The owner of a list item
  197. * is the object (usually a TCB) that contains the list item.
  198. *
  199. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  200. * \ingroup LinkedList
  201. */
  202. #define listGET_LIST_ITEM_OWNER( pxListItem ) ( ( pxListItem )->pvOwner )
  203. /*
  204. * Access macro to set the value of the list item. In most cases the value is
  205. * used to sort the list in descending order.
  206. *
  207. * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
  208. * \ingroup LinkedList
  209. */
  210. #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( ( pxListItem )->xItemValue = ( xValue ) )
  211. /*
  212. * Access macro to retrieve the value of the list item. The value can
  213. * represent anything - for example the priority of a task, or the time at
  214. * which a task should be unblocked.
  215. *
  216. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  217. * \ingroup LinkedList
  218. */
  219. #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )
  220. /*
  221. * Access macro to retrieve the value of the list item at the head of a given
  222. * list.
  223. *
  224. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  225. * \ingroup LinkedList
  226. */
  227. #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext->xItemValue )
  228. /*
  229. * Return the list item at the head of the list.
  230. *
  231. * \page listGET_HEAD_ENTRY listGET_HEAD_ENTRY
  232. * \ingroup LinkedList
  233. */
  234. #define listGET_HEAD_ENTRY( pxList ) ( ( ( pxList )->xListEnd ).pxNext )
  235. /*
  236. * Return the list item at the head of the list.
  237. *
  238. * \page listGET_NEXT listGET_NEXT
  239. * \ingroup LinkedList
  240. */
  241. #define listGET_NEXT( pxListItem ) ( ( pxListItem )->pxNext )
  242. /*
  243. * Return the list item that marks the end of the list
  244. *
  245. * \page listGET_END_MARKER listGET_END_MARKER
  246. * \ingroup LinkedList
  247. */
  248. #define listGET_END_MARKER( pxList ) ( ( ListItem_t const * ) ( &( ( pxList )->xListEnd ) ) )
  249. /*
  250. * Access macro to determine if a list contains any items. The macro will
  251. * only have the value true if the list is empty.
  252. *
  253. * \page listLIST_IS_EMPTY listLIST_IS_EMPTY
  254. * \ingroup LinkedList
  255. */
  256. #define listLIST_IS_EMPTY( pxList ) ( ( BaseType_t ) ( ( pxList )->uxNumberOfItems == ( UBaseType_t ) 0 ) )
  257. /*
  258. * Access macro to return the number of items in the list.
  259. */
  260. #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )
  261. /*
  262. * Access function to obtain the owner of the next entry in a list.
  263. *
  264. * The list member pxIndex is used to walk through a list. Calling
  265. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
  266. * and returns that entry's pxOwner parameter. Using multiple calls to this
  267. * function it is therefore possible to move through every item contained in
  268. * a list.
  269. *
  270. * The pxOwner parameter of a list item is a pointer to the object that owns
  271. * the list item. In the scheduler this is normally a task control block.
  272. * The pxOwner parameter effectively creates a two way link between the list
  273. * item and its owner.
  274. *
  275. * @param pxTCB pxTCB is set to the address of the owner of the next list item.
  276. * @param pxList The list from which the next item owner is to be returned.
  277. *
  278. * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY
  279. * \ingroup LinkedList
  280. */
  281. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
  282. { \
  283. List_t * const pxConstList = ( pxList ); \
  284. /* Increment the index to the next item and return the item, ensuring */ \
  285. /* we don't return the marker used at the end of the list. */ \
  286. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  287. if( ( void * ) ( pxConstList )->pxIndex == ( void * ) &( ( pxConstList )->xListEnd ) ) \
  288. { \
  289. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  290. } \
  291. ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
  292. }
  293. /*
  294. * Access function to obtain the owner of the first entry in a list. Lists
  295. * are normally sorted in ascending item value order.
  296. *
  297. * This function returns the pxOwner member of the first item in the list.
  298. * The pxOwner parameter of a list item is a pointer to the object that owns
  299. * the list item. In the scheduler this is normally a task control block.
  300. * The pxOwner parameter effectively creates a two way link between the list
  301. * item and its owner.
  302. *
  303. * @param pxList The list from which the owner of the head item is to be
  304. * returned.
  305. *
  306. * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY
  307. * \ingroup LinkedList
  308. */
  309. #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( (&( ( pxList )->xListEnd ))->pxNext->pvOwner )
  310. /*
  311. * Check to see if a list item is within a list. The list item maintains a
  312. * "container" pointer that points to the list it is in. All this macro does
  313. * is check to see if the container and the list match.
  314. *
  315. * @param pxList The list we want to know if the list item is within.
  316. * @param pxListItem The list item we want to know if is in the list.
  317. * @return pdTRUE if the list item is in the list, otherwise pdFALSE.
  318. */
  319. #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( BaseType_t ) ( ( pxListItem )->pvContainer == ( void * ) ( pxList ) ) )
  320. /*
  321. * Return the list a list item is contained within (referenced from).
  322. *
  323. * @param pxListItem The list item being queried.
  324. * @return A pointer to the List_t object that references the pxListItem
  325. */
  326. #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pvContainer )
  327. /*
  328. * This provides a crude means of knowing if a list has been initialised, as
  329. * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
  330. * function.
  331. */
  332. #define listLIST_IS_INITIALISED( pxList ) ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )
  333. /*
  334. * Must be called before a list is used! This initialises all the members
  335. * of the list structure and inserts the xListEnd item into the list as a
  336. * marker to the back of the list.
  337. *
  338. * @param pxList Pointer to the list being initialised.
  339. *
  340. * \page vListInitialise vListInitialise
  341. * \ingroup LinkedList
  342. */
  343. void vListInitialise( List_t * const pxList ) PRIVILEGED_FUNCTION;
  344. /*
  345. * Must be called before a list item is used. This sets the list container to
  346. * null so the item does not think that it is already contained in a list.
  347. *
  348. * @param pxItem Pointer to the list item being initialised.
  349. *
  350. * \page vListInitialiseItem vListInitialiseItem
  351. * \ingroup LinkedList
  352. */
  353. void vListInitialiseItem( ListItem_t * const pxItem ) PRIVILEGED_FUNCTION;
  354. /*
  355. * Insert a list item into a list. The item will be inserted into the list in
  356. * a position determined by its item value (descending item value order).
  357. *
  358. * @param pxList The list into which the item is to be inserted.
  359. *
  360. * @param pxNewListItem The item that is to be placed in the list.
  361. *
  362. * \page vListInsert vListInsert
  363. * \ingroup LinkedList
  364. */
  365. void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem ) PRIVILEGED_FUNCTION;
  366. /*
  367. * Insert a list item into a list. The item will be inserted in a position
  368. * such that it will be the last item within the list returned by multiple
  369. * calls to listGET_OWNER_OF_NEXT_ENTRY.
  370. *
  371. * The list member pvIndex is used to walk through a list. Calling
  372. * listGET_OWNER_OF_NEXT_ENTRY increments pvIndex to the next item in the list.
  373. * Placing an item in a list using vListInsertEnd effectively places the item
  374. * in the list position pointed to by pvIndex. This means that every other
  375. * item within the list will be returned by listGET_OWNER_OF_NEXT_ENTRY before
  376. * the pvIndex parameter again points to the item being inserted.
  377. *
  378. * @param pxList The list into which the item is to be inserted.
  379. *
  380. * @param pxNewListItem The list item to be inserted into the list.
  381. *
  382. * \page vListInsertEnd vListInsertEnd
  383. * \ingroup LinkedList
  384. */
  385. void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem ) PRIVILEGED_FUNCTION;
  386. /*
  387. * Remove an item from a list. The list item has a pointer to the list that
  388. * it is in, so only the list item need be passed into the function.
  389. *
  390. * @param uxListRemove The item to be removed. The item will remove itself from
  391. * the list pointed to by it's pxContainer parameter.
  392. *
  393. * @return The number of items that remain in the list after the list item has
  394. * been removed.
  395. *
  396. * \page uxListRemove uxListRemove
  397. * \ingroup LinkedList
  398. */
  399. UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove ) PRIVILEGED_FUNCTION;
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. #endif