queue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #ifndef QUEUE_H
  29. #define QUEUE_H
  30. /* *INDENT-OFF* */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* *INDENT-ON* */
  35. /**
  36. * Type by which queues are referenced. For example, a call to xQueueCreate()
  37. * returns an QueueHandle_t variable that can then be used as a parameter to
  38. * xQueueSend(), xQueueReceive(), etc.
  39. */
  40. struct QueueDefinition; /* Using old naming convention so as not to break kernel aware debuggers. */
  41. typedef struct QueueDefinition * QueueHandle_t;
  42. /* For internal use only. */
  43. #define queueSEND_TO_BACK ( ( BaseType_t ) 0 )
  44. #define queueSEND_TO_FRONT ( ( BaseType_t ) 1 )
  45. #define queueOVERWRITE ( ( BaseType_t ) 2 )
  46. /* For internal use only. These definitions *must* match those in queue.c. */
  47. #define queueQUEUE_TYPE_BASE ( ( uint8_t ) 0U )
  48. #define queueQUEUE_TYPE_SET ( ( uint8_t ) 0U )
  49. #define queueQUEUE_TYPE_MUTEX ( ( uint8_t ) 1U )
  50. #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( ( uint8_t ) 2U )
  51. #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( ( uint8_t ) 3U )
  52. #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( ( uint8_t ) 4U )
  53. /**
  54. * queue. h
  55. * @code{c}
  56. * BaseType_t xQueueGenericSend(
  57. * QueueHandle_t xQueue,
  58. * const void * pvItemToQueue,
  59. * TickType_t xTicksToWait
  60. * BaseType_t xCopyPosition
  61. * );
  62. * @endcode
  63. *
  64. * It is preferred that the macros xQueueSend(), xQueueSendToFront() and
  65. * xQueueSendToBack() are used in place of calling this function directly.
  66. *
  67. * Post an item on a queue. The item is queued by copy, not by reference.
  68. * This function must not be called from an interrupt service routine.
  69. * See xQueueSendFromISR () for an alternative which may be used in an ISR.
  70. *
  71. * @param xQueue The handle to the queue on which the item is to be posted.
  72. *
  73. * @param pvItemToQueue A pointer to the item that is to be placed on the
  74. * queue. The size of the items the queue will hold was defined when the
  75. * queue was created, so this many bytes will be copied from pvItemToQueue
  76. * into the queue storage area.
  77. *
  78. * @param xTicksToWait The maximum amount of time the task should block
  79. * waiting for space to become available on the queue, should it already
  80. * be full. The call will return immediately if this is set to 0 and the
  81. * queue is full. The time is defined in tick periods so the constant
  82. * portTICK_PERIOD_MS should be used to convert to real time if this is required.
  83. *
  84. * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the
  85. * item at the back of the queue, or queueSEND_TO_FRONT to place the item
  86. * at the front of the queue (for high priority messages).
  87. *
  88. * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
  89. *
  90. * Example usage:
  91. * @code{c}
  92. * struct AMessage
  93. * {
  94. * char ucMessageID;
  95. * char ucData[ 20 ];
  96. * } xMessage;
  97. *
  98. * uint32_t ulVar = 10UL;
  99. *
  100. * void vATask( void *pvParameters )
  101. * {
  102. * QueueHandle_t xQueue1, xQueue2;
  103. * struct AMessage *pxMessage;
  104. *
  105. * // Create a queue capable of containing 10 uint32_t values.
  106. * xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) );
  107. *
  108. * // Create a queue capable of containing 10 pointers to AMessage structures.
  109. * // These should be passed by pointer as they contain a lot of data.
  110. * xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
  111. *
  112. * // ...
  113. *
  114. * if( xQueue1 != 0 )
  115. * {
  116. * // Send an uint32_t. Wait for 10 ticks for space to become
  117. * // available if necessary.
  118. * if( xQueueGenericSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10, queueSEND_TO_BACK ) != pdPASS )
  119. * {
  120. * // Failed to post the message, even after 10 ticks.
  121. * }
  122. * }
  123. *
  124. * if( xQueue2 != 0 )
  125. * {
  126. * // Send a pointer to a struct AMessage object. Don't block if the
  127. * // queue is already full.
  128. * pxMessage = & xMessage;
  129. * xQueueGenericSend( xQueue2, ( void * ) &pxMessage, ( TickType_t ) 0, queueSEND_TO_BACK );
  130. * }
  131. *
  132. * // ... Rest of task code.
  133. * }
  134. * @endcode
  135. * \defgroup xQueueSend xQueueSend
  136. * \ingroup QueueManagement
  137. */
  138. BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
  139. const void * const pvItemToQueue,
  140. TickType_t xTicksToWait,
  141. const BaseType_t xCopyPosition );
  142. /**
  143. * queue. h
  144. * @code{c}
  145. * void vQueueDelete( QueueHandle_t xQueue );
  146. * @endcode
  147. *
  148. * Delete a queue - freeing all the memory allocated for storing of items
  149. * placed on the queue.
  150. *
  151. * @param xQueue A handle to the queue to be deleted.
  152. *
  153. * \defgroup vQueueDelete vQueueDelete
  154. * \ingroup QueueManagement
  155. */
  156. void vQueueDelete( QueueHandle_t xQueue );
  157. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
  158. BaseType_t * const pxHigherPriorityTaskWoken );
  159. /**
  160. * queue. h
  161. * @code{c}
  162. * BaseType_t xQueueReceiveFromISR(
  163. * QueueHandle_t xQueue,
  164. * void *pvBuffer,
  165. * BaseType_t *pxTaskWoken
  166. * );
  167. * @endcode
  168. *
  169. * Receive an item from a queue. It is safe to use this function from within an
  170. * interrupt service routine.
  171. *
  172. * @param xQueue The handle to the queue from which the item is to be
  173. * received.
  174. *
  175. * @param pvBuffer Pointer to the buffer into which the received item will
  176. * be copied.
  177. *
  178. * @param pxTaskWoken A task may be blocked waiting for space to become
  179. * available on the queue. If xQueueReceiveFromISR causes such a task to
  180. * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will
  181. * remain unchanged.
  182. *
  183. * @return pdTRUE if an item was successfully received from the queue,
  184. * otherwise pdFALSE.
  185. *
  186. * Example usage:
  187. * @code{c}
  188. *
  189. * QueueHandle_t xQueue;
  190. *
  191. * // Function to create a queue and post some values.
  192. * void vAFunction( void *pvParameters )
  193. * {
  194. * char cValueToPost;
  195. * const TickType_t xTicksToWait = ( TickType_t )0xff;
  196. *
  197. * // Create a queue capable of containing 10 characters.
  198. * xQueue = xQueueCreate( 10, sizeof( char ) );
  199. * if( xQueue == 0 )
  200. * {
  201. * // Failed to create the queue.
  202. * }
  203. *
  204. * // ...
  205. *
  206. * // Post some characters that will be used within an ISR. If the queue
  207. * // is full then this task will block for xTicksToWait ticks.
  208. * cValueToPost = 'a';
  209. * xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait );
  210. * cValueToPost = 'b';
  211. * xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait );
  212. *
  213. * // ... keep posting characters ... this task may block when the queue
  214. * // becomes full.
  215. *
  216. * cValueToPost = 'c';
  217. * xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait );
  218. * }
  219. *
  220. * // ISR that outputs all the characters received on the queue.
  221. * void vISR_Routine( void )
  222. * {
  223. * BaseType_t xTaskWokenByReceive = pdFALSE;
  224. * char cRxedChar;
  225. *
  226. * while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )
  227. * {
  228. * // A character was received. Output the character now.
  229. * vOutputCharacter( cRxedChar );
  230. *
  231. * // If removing the character from the queue woke the task that was
  232. * // posting onto the queue cTaskWokenByReceive will have been set to
  233. * // pdTRUE. No matter how many times this loop iterates only one
  234. * // task will be woken.
  235. * }
  236. *
  237. * if( cTaskWokenByPost != ( char ) pdFALSE;
  238. * {
  239. * taskYIELD ();
  240. * }
  241. * }
  242. * @endcode
  243. * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR
  244. * \ingroup QueueManagement
  245. */
  246. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
  247. void * const pvBuffer,
  248. BaseType_t * const pxHigherPriorityTaskWoken );
  249. /*
  250. * For internal use only. Use xSemaphoreCreateMutex(),
  251. * xSemaphoreCreateCounting() or xSemaphoreGetMutexHolder() instead of calling
  252. * these functions directly.
  253. */
  254. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType );
  255. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
  256. StaticQueue_t * pxStaticQueue );
  257. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
  258. const UBaseType_t uxInitialCount );
  259. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
  260. const UBaseType_t uxInitialCount,
  261. StaticQueue_t * pxStaticQueue );
  262. BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
  263. TickType_t xTicksToWait );
  264. /*
  265. * For internal use only. Use xSemaphoreTakeMutexRecursive() or
  266. * xSemaphoreGiveMutexRecursive() instead of calling these functions directly.
  267. */
  268. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
  269. TickType_t xTicksToWait );
  270. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex );
  271. /*
  272. * Generic version of the function used to create a queue using dynamic memory
  273. * allocation. This is called by other functions and macros that create other
  274. * RTOS objects that use the queue structure as their base.
  275. */
  276. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  277. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
  278. const UBaseType_t uxItemSize,
  279. const uint8_t ucQueueType );
  280. #endif
  281. /*
  282. * Generic version of the function used to create a queue using dynamic memory
  283. * allocation. This is called by other functions and macros that create other
  284. * RTOS objects that use the queue structure as their base.
  285. */
  286. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  287. QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
  288. const UBaseType_t uxItemSize,
  289. uint8_t * pucQueueStorage,
  290. StaticQueue_t * pxStaticQueue,
  291. const uint8_t ucQueueType );
  292. #endif
  293. /* *INDENT-OFF* */
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. /* *INDENT-ON* */
  298. #endif /* QUEUE_H */