list.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
  3. FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
  4. http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. ***************************************************************************
  6. * *
  7. * FreeRTOS tutorial books are available in pdf and paperback. *
  8. * Complete, revised, and edited pdf reference manuals are also *
  9. * available. *
  10. * *
  11. * Purchasing FreeRTOS documentation will not only help you, by *
  12. * ensuring you get running as quickly as possible and with an *
  13. * in-depth knowledge of how to use FreeRTOS, it will also help *
  14. * the FreeRTOS project to continue with its mission of providing *
  15. * professional grade, cross platform, de facto standard solutions *
  16. * for microcontrollers - completely free of charge! *
  17. * *
  18. * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
  19. * *
  20. * Thank you for using FreeRTOS, and thank you for your support! *
  21. * *
  22. ***************************************************************************
  23. This file is part of the FreeRTOS distribution.
  24. FreeRTOS is free software; you can redistribute it and/or modify it under
  25. the terms of the GNU General Public License (version 2) as published by the
  26. Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
  27. >>>NOTE<<< The modification to the GPL is included to allow you to
  28. distribute a combined work that includes FreeRTOS without being obliged to
  29. provide the source code for proprietary components outside of the FreeRTOS
  30. kernel. FreeRTOS is distributed in the hope that it will be useful, but
  31. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  33. more details. You should have received a copy of the GNU General Public
  34. License and the FreeRTOS license exception along with FreeRTOS; if not it
  35. can be viewed here: http://www.freertos.org/a00114.html and also obtained
  36. by writing to Richard Barry, contact details for whom are available on the
  37. FreeRTOS WEB site.
  38. 1 tab == 4 spaces!
  39. ***************************************************************************
  40. * *
  41. * Having a problem? Start by reading the FAQ "My application does *
  42. * not run, what could be wrong?" *
  43. * *
  44. * http://www.FreeRTOS.org/FAQHelp.html *
  45. * *
  46. ***************************************************************************
  47. http://www.FreeRTOS.org - Documentation, training, latest versions, license
  48. and contact details.
  49. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  50. including FreeRTOS+Trace - an indispensable productivity tool.
  51. Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
  52. the code with commercial support, indemnification, and middleware, under
  53. the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
  54. provide a safety engineered and independently SIL3 certified version under
  55. the SafeRTOS brand: http://www.SafeRTOS.com.
  56. */
  57. /*
  58. * This is the list implementation used by the scheduler. While it is tailored
  59. * heavily for the schedulers needs, it is also available for use by
  60. * application code.
  61. *
  62. * xLists can only store pointers to xListItems. Each xListItem contains a
  63. * numeric value (xItemValue). Most of the time the lists are sorted in
  64. * descending item value order.
  65. *
  66. * Lists are created already containing one list item. The value of this
  67. * item is the maximum possible that can be stored, it is therefore always at
  68. * the end of the list and acts as a marker. The list member pxHead always
  69. * points to this marker - even though it is at the tail of the list. This
  70. * is because the tail contains a wrap back pointer to the true head of
  71. * the list.
  72. *
  73. * In addition to it's value, each list item contains a pointer to the next
  74. * item in the list (pxNext), a pointer to the list it is in (pxContainer)
  75. * and a pointer to back to the object that contains it. These later two
  76. * pointers are included for efficiency of list manipulation. There is
  77. * effectively a two way link between the object containing the list item and
  78. * the list item itself.
  79. *
  80. *
  81. * \page ListIntroduction List Implementation
  82. * \ingroup FreeRTOSIntro
  83. */
  84. #ifndef LIST_H
  85. #define LIST_H
  86. #ifdef __cplusplus
  87. extern "C" {
  88. #endif
  89. #include "portmacro.h"
  90. /*
  91. * Definition of the only type of object that a list can contain.
  92. */
  93. struct xLIST_ITEM
  94. {
  95. portTickType xItemValue; /*< The value being listed. In most cases this is used to sort the list in descending order. */
  96. volatile struct xLIST_ITEM * pxNext; /*< Pointer to the next xListItem in the list. */
  97. volatile struct xLIST_ITEM * pxPrevious;/*< Pointer to the previous xListItem in the list. */
  98. 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. */
  99. void * pvContainer; /*< Pointer to the list in which this list item is placed (if any). */
  100. };
  101. typedef struct xLIST_ITEM xListItem; /* For some reason lint wants this as two separate definitions. */
  102. struct xMINI_LIST_ITEM
  103. {
  104. portTickType xItemValue;
  105. volatile struct xLIST_ITEM *pxNext;
  106. volatile struct xLIST_ITEM *pxPrevious;
  107. };
  108. typedef struct xMINI_LIST_ITEM xMiniListItem;
  109. /*
  110. * Definition of the type of queue used by the scheduler.
  111. */
  112. typedef struct xLIST
  113. {
  114. volatile unsigned portBASE_TYPE uxNumberOfItems;
  115. volatile xListItem * pxIndex; /*< Used to walk through the list. Points to the last item returned by a call to pvListGetOwnerOfNextEntry (). */
  116. volatile xMiniListItem 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. */
  117. } xList;
  118. /*
  119. * Access macro to set the owner of a list item. The owner of a list item
  120. * is the object (usually a TCB) that contains the list item.
  121. *
  122. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  123. * \ingroup LinkedList
  124. */
  125. #define listSET_LIST_ITEM_OWNER( pxListItem, pxOwner ) ( pxListItem )->pvOwner = ( void * ) ( pxOwner )
  126. /*
  127. * Access macro to get the owner of a list item. The owner of a list item
  128. * is the object (usually a TCB) that contains the list item.
  129. *
  130. * \page listSET_LIST_ITEM_OWNER listSET_LIST_ITEM_OWNER
  131. * \ingroup LinkedList
  132. */
  133. #define listGET_LIST_ITEM_OWNER( pxListItem ) ( pxListItem )->pvOwner
  134. /*
  135. * Access macro to set the value of the list item. In most cases the value is
  136. * used to sort the list in descending order.
  137. *
  138. * \page listSET_LIST_ITEM_VALUE listSET_LIST_ITEM_VALUE
  139. * \ingroup LinkedList
  140. */
  141. #define listSET_LIST_ITEM_VALUE( pxListItem, xValue ) ( pxListItem )->xItemValue = ( xValue )
  142. /*
  143. * Access macro to retrieve the value of the list item. The value can
  144. * represent anything - for example a the priority of a task, or the time at
  145. * which a task should be unblocked.
  146. *
  147. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  148. * \ingroup LinkedList
  149. */
  150. #define listGET_LIST_ITEM_VALUE( pxListItem ) ( ( pxListItem )->xItemValue )
  151. /*
  152. * Access macro the retrieve the value of the list item at the head of a given
  153. * list.
  154. *
  155. * \page listGET_LIST_ITEM_VALUE listGET_LIST_ITEM_VALUE
  156. * \ingroup LinkedList
  157. */
  158. #define listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxList ) ( (&( ( pxList )->xListEnd ))->pxNext->xItemValue )
  159. /*
  160. * Access macro to determine if a list contains any items. The macro will
  161. * only have the value true if the list is empty.
  162. *
  163. * \page listLIST_IS_EMPTY listLIST_IS_EMPTY
  164. * \ingroup LinkedList
  165. */
  166. #define listLIST_IS_EMPTY( pxList ) ( ( pxList )->uxNumberOfItems == ( unsigned portBASE_TYPE ) 0 )
  167. /*
  168. * Access macro to return the number of items in the list.
  169. */
  170. #define listCURRENT_LIST_LENGTH( pxList ) ( ( pxList )->uxNumberOfItems )
  171. /*
  172. * Access function to obtain the owner of the next entry in a list.
  173. *
  174. * The list member pxIndex is used to walk through a list. Calling
  175. * listGET_OWNER_OF_NEXT_ENTRY increments pxIndex to the next item in the list
  176. * and returns that entries pxOwner parameter. Using multiple calls to this
  177. * function it is therefore possible to move through every item contained in
  178. * a list.
  179. *
  180. * The pxOwner parameter of a list item is a pointer to the object that owns
  181. * the list item. In the scheduler this is normally a task control block.
  182. * The pxOwner parameter effectively creates a two way link between the list
  183. * item and its owner.
  184. *
  185. * @param pxList The list from which the next item owner is to be returned.
  186. *
  187. * \page listGET_OWNER_OF_NEXT_ENTRY listGET_OWNER_OF_NEXT_ENTRY
  188. * \ingroup LinkedList
  189. */
  190. #define listGET_OWNER_OF_NEXT_ENTRY( pxTCB, pxList ) \
  191. { \
  192. xList * const pxConstList = ( pxList ); \
  193. /* Increment the index to the next item and return the item, ensuring */ \
  194. /* we don't return the marker used at the end of the list. */ \
  195. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  196. if( ( pxConstList )->pxIndex == ( xListItem * ) &( ( pxConstList )->xListEnd ) ) \
  197. { \
  198. ( pxConstList )->pxIndex = ( pxConstList )->pxIndex->pxNext; \
  199. } \
  200. ( pxTCB ) = ( pxConstList )->pxIndex->pvOwner; \
  201. }
  202. /*
  203. * Access function to obtain the owner of the first entry in a list. Lists
  204. * are normally sorted in ascending item value order.
  205. *
  206. * This function returns the pxOwner member of the first item in the list.
  207. * The pxOwner parameter of a list item is a pointer to the object that owns
  208. * the list item. In the scheduler this is normally a task control block.
  209. * The pxOwner parameter effectively creates a two way link between the list
  210. * item and its owner.
  211. *
  212. * @param pxList The list from which the owner of the head item is to be
  213. * returned.
  214. *
  215. * \page listGET_OWNER_OF_HEAD_ENTRY listGET_OWNER_OF_HEAD_ENTRY
  216. * \ingroup LinkedList
  217. */
  218. #define listGET_OWNER_OF_HEAD_ENTRY( pxList ) ( (&( ( pxList )->xListEnd ))->pxNext->pvOwner )
  219. /*
  220. * Check to see if a list item is within a list. The list item maintains a
  221. * "container" pointer that points to the list it is in. All this macro does
  222. * is check to see if the container and the list match.
  223. *
  224. * @param pxList The list we want to know if the list item is within.
  225. * @param pxListItem The list item we want to know if is in the list.
  226. * @return pdTRUE is the list item is in the list, otherwise pdFALSE.
  227. * pointer against
  228. */
  229. #define listIS_CONTAINED_WITHIN( pxList, pxListItem ) ( ( pxListItem )->pvContainer == ( void * ) ( pxList ) )
  230. /*
  231. * Return the list a list item is contained within (referenced from).
  232. *
  233. * @param pxListItem The list item being queried.
  234. * @return A pointer to the xList object that references the pxListItem
  235. */
  236. #define listLIST_ITEM_CONTAINER( pxListItem ) ( ( pxListItem )->pvContainer )
  237. /*
  238. * This provides a crude means of knowing if a list has been initialised, as
  239. * pxList->xListEnd.xItemValue is set to portMAX_DELAY by the vListInitialise()
  240. * function.
  241. */
  242. #define listLIST_IS_INITIALISED( pxList ) ( ( pxList )->xListEnd.xItemValue == portMAX_DELAY )
  243. /*
  244. * Must be called before a list is used! This initialises all the members
  245. * of the list structure and inserts the xListEnd item into the list as a
  246. * marker to the back of the list.
  247. *
  248. * @param pxList Pointer to the list being initialised.
  249. *
  250. * \page vListInitialise vListInitialise
  251. * \ingroup LinkedList
  252. */
  253. void vListInitialise( xList *pxList );
  254. /*
  255. * Must be called before a list item is used. This sets the list container to
  256. * null so the item does not think that it is already contained in a list.
  257. *
  258. * @param pxItem Pointer to the list item being initialised.
  259. *
  260. * \page vListInitialiseItem vListInitialiseItem
  261. * \ingroup LinkedList
  262. */
  263. void vListInitialiseItem( xListItem *pxItem );
  264. /*
  265. * Insert a list item into a list. The item will be inserted into the list in
  266. * a position determined by its item value (descending item value order).
  267. *
  268. * @param pxList The list into which the item is to be inserted.
  269. *
  270. * @param pxNewListItem The item to that is to be placed in the list.
  271. *
  272. * \page vListInsert vListInsert
  273. * \ingroup LinkedList
  274. */
  275. void vListInsert( xList *pxList, xListItem *pxNewListItem );
  276. /*
  277. * Insert a list item into a list. The item will be inserted in a position
  278. * such that it will be the last item within the list returned by multiple
  279. * calls to listGET_OWNER_OF_NEXT_ENTRY.
  280. *
  281. * The list member pvIndex is used to walk through a list. Calling
  282. * listGET_OWNER_OF_NEXT_ENTRY increments pvIndex to the next item in the list.
  283. * Placing an item in a list using vListInsertEnd effectively places the item
  284. * in the list position pointed to by pvIndex. This means that every other
  285. * item within the list will be returned by listGET_OWNER_OF_NEXT_ENTRY before
  286. * the pvIndex parameter again points to the item being inserted.
  287. *
  288. * @param pxList The list into which the item is to be inserted.
  289. *
  290. * @param pxNewListItem The list item to be inserted into the list.
  291. *
  292. * \page vListInsertEnd vListInsertEnd
  293. * \ingroup LinkedList
  294. */
  295. void vListInsertEnd( xList *pxList, xListItem *pxNewListItem );
  296. /*
  297. * Remove an item from a list. The list item has a pointer to the list that
  298. * it is in, so only the list item need be passed into the function.
  299. *
  300. * @param uxListRemove The item to be removed. The item will remove itself from
  301. * the list pointed to by it's pxContainer parameter.
  302. *
  303. * @return The number of items that remain in the list after the list item has
  304. * been removed.
  305. *
  306. * \page uxListRemove uxListRemove
  307. * \ingroup LinkedList
  308. */
  309. unsigned portBASE_TYPE uxListRemove( xListItem *pxItemToRemove );
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif