queue.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #ifndef QUEUE_HPP_
  39. #define QUEUE_HPP_
  40. /**
  41. * C++ exceptions are used by default when constructors fail.
  42. * If you do not want this behavior, define the following in your makefile
  43. * or project. Note that in most / all cases when a constructor fails,
  44. * it's a fatal error. In the cases when you've defined this, the new
  45. * default behavior will be to issue a configASSERT() instead.
  46. */
  47. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  48. #include <exception>
  49. #include <cstdio>
  50. #include <string>
  51. #ifdef CPP_FREERTOS_NO_CPP_STRINGS
  52. #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
  53. #endif
  54. #endif
  55. #include "FreeRTOS.h"
  56. #include "queue.h"
  57. namespace cpp_freertos {
  58. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  59. /**
  60. * This is the exception that is thrown if a Queue constructor fails.
  61. */
  62. class QueueCreateException : public std::exception {
  63. public:
  64. /**
  65. * Create the exception.
  66. */
  67. QueueCreateException()
  68. {
  69. sprintf(errorString, "Queue Constructor Failed");
  70. }
  71. /**
  72. * Create the exception.
  73. */
  74. explicit QueueCreateException(const char *info)
  75. {
  76. snprintf(errorString, sizeof(errorString),
  77. "Queue Constructor Failed %s", info);
  78. }
  79. /**
  80. * Get what happened as a string.
  81. * We are overriding the base implementation here.
  82. */
  83. virtual const char *what() const throw()
  84. {
  85. return errorString;
  86. }
  87. private:
  88. /**
  89. * A text string representing what failed.
  90. */
  91. char errorString[80];
  92. };
  93. #endif
  94. /**
  95. * Queue class wrapper for FreeRTOS queues. This class provides enqueue
  96. * and dequeue operations.
  97. *
  98. * @note It is expected that an application will instantiate this class or
  99. * one of the derived classes and use that. It is not expected that
  100. * a user or application will derive from these classes.
  101. */
  102. class Queue {
  103. /////////////////////////////////////////////////////////////////////////
  104. //
  105. // Public API
  106. //
  107. /////////////////////////////////////////////////////////////////////////
  108. public:
  109. /**
  110. * Our constructor.
  111. *
  112. * @throws QueueCreateException
  113. * @param maxItems Maximum number of items this queue can hold.
  114. * @param itemSize Size of an item in a queue.
  115. * @note FreeRTOS queues use a memcpy / fixed size scheme for queues.
  116. */
  117. Queue(UBaseType_t maxItems, UBaseType_t itemSize);
  118. /**
  119. * Our destructor.
  120. */
  121. virtual ~Queue();
  122. /**
  123. * Add an item to the back of the queue.
  124. *
  125. * @param item The item you are adding.
  126. * @return true if the item was added, false if it was not.
  127. */
  128. virtual bool Enqueue(void *item);
  129. /**
  130. * Add an item to the back of the queue.
  131. *
  132. * @param item The item you are adding.
  133. * @param Timeout How long to wait to add the item to the queue if
  134. * the queue is currently full.
  135. * @return true if the item was added, false if it was not.
  136. */
  137. virtual bool Enqueue(void *item, TickType_t Timeout);
  138. /**
  139. * Remove an item from the front of the queue.
  140. *
  141. * @param item Where the item you are removing will be returned to.
  142. * @param Timeout How long to wait to remove an item if the queue
  143. * is currently empty.
  144. * @return true if an item was removed, false if no item was removed.
  145. */
  146. bool Dequeue(void *item, TickType_t Timeout = portMAX_DELAY);
  147. /**
  148. * Make a copy of an item from the front of the queue. This will
  149. * not remove it from the head of the queue.
  150. *
  151. * @param item Where the item you are removing will be returned to.
  152. * @param Timeout How long to wait to remove an item if the queue
  153. * is currently empty.
  154. * @return true if an item was copied, false if no item was copied.
  155. */
  156. bool Peek(void *item, TickType_t Timeout = portMAX_DELAY);
  157. /**
  158. * Add an item to the back of the queue in ISR context.
  159. *
  160. * @param item The item you are adding.
  161. * @param pxHigherPriorityTaskWoken Did this operation result in a
  162. * rescheduling event.
  163. * @return true if the item was added, false if it was not.
  164. */
  165. virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
  166. /**
  167. * Remove an item from the front of the queue in ISR context.
  168. *
  169. * @param item Where the item you are removing will be returned to.
  170. * @param pxHigherPriorityTaskWoken Did this operation result in a
  171. * rescheduling event.
  172. * @return true if an item was removed, false if no item was removed.
  173. */
  174. bool DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
  175. /**
  176. * Make a copy of an item from the front of the queue. This will
  177. * not remove it from the head of the queue.
  178. *
  179. * @param item Where the item you are removing will be returned to.
  180. * @return true if an item was copied, false if no item was copied.
  181. */
  182. bool PeekFromISR(void *item);
  183. /**
  184. * Is the queue empty?
  185. * @return true if the queue was empty when this was called, false if
  186. * the queue was not empty.
  187. */
  188. bool IsEmpty();
  189. /**
  190. * Is the queue full?
  191. * @return true if the queue was full when this was called, false if
  192. * the queue was not full.
  193. */
  194. bool IsFull();
  195. /**
  196. * Remove all objects from the queue.
  197. */
  198. void Flush();
  199. /**
  200. * How many items are currently in the queue.
  201. * @return the number of items in the queue.
  202. */
  203. UBaseType_t NumItems();
  204. /**
  205. * How many empty spaves are currently left in the queue.
  206. * @return the number of remaining spaces.
  207. */
  208. UBaseType_t NumSpacesLeft();
  209. /////////////////////////////////////////////////////////////////////////
  210. //
  211. // Protected API
  212. // Not intended for use by application code.
  213. //
  214. /////////////////////////////////////////////////////////////////////////
  215. protected:
  216. /**
  217. * FreeRTOS queue handle.
  218. */
  219. QueueHandle_t handle;
  220. };
  221. /**
  222. * Enhanced queue class that implements a double ended queue (a "deque"),
  223. * almost. Unlike the traditional CommSci version, there is no way to
  224. * dequeue from the back. Practically, this most likely isn't a big deal.
  225. *
  226. * @note It is expected that an application will instantiate this class or
  227. * one of the derived classes and use that. It is not expected that
  228. * a user or application will derive from these classes.
  229. */
  230. class Deque : public Queue {
  231. /////////////////////////////////////////////////////////////////////////
  232. //
  233. // Public API
  234. //
  235. /////////////////////////////////////////////////////////////////////////
  236. public:
  237. /**
  238. * Our constructor.
  239. *
  240. * @throws QueueCreateException
  241. * @param maxItems Maximum number of items thsi queue can hold.
  242. * @param itemSize Size of an item in a queue.
  243. * @note FreeRTOS queues use a memcpy / fixed size scheme for queues.
  244. */
  245. Deque(UBaseType_t maxItems, UBaseType_t itemSize);
  246. /**
  247. * Add an item to the front of the queue. This will result in
  248. * the item being removed first, ahead of all of the items
  249. * added by the base calss Dequeue() function.
  250. *
  251. * @param item The item you are adding.
  252. * @param Timeout How long to wait to add the item to the queue if
  253. * the queue is currently full.
  254. * @return true if the item was added, false if it was not.
  255. */
  256. bool EnqueueToFront(void *item, TickType_t Timeout = portMAX_DELAY);
  257. /**
  258. * Add an item to the front of the queue. This will result in
  259. * the item being removed first, ahead of all of the items
  260. * added by the base calss Dequeue() function.
  261. *
  262. * @param item The item you are adding.
  263. * @param pxHigherPriorityTaskWoken Did this operation result in a
  264. * rescheduling event.
  265. * @return true if the item was added, false if it was not.
  266. */
  267. bool EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
  268. };
  269. /**
  270. * Binary queue with overwrite. This queue can only hold one item.
  271. * If sucessive Enqueue operations are called, that item is overwritten
  272. * with whatever the last item was.
  273. *
  274. * @note It is expected that an application will instantiate this class or
  275. * one of the derived classes and use that. It is not expected that
  276. * a user or application will derive from these classes.
  277. */
  278. class BinaryQueue : public Queue {
  279. /////////////////////////////////////////////////////////////////////////
  280. //
  281. // Public API
  282. //
  283. /////////////////////////////////////////////////////////////////////////
  284. public:
  285. /**
  286. * Our constructor.
  287. *
  288. * @throws QueueCreateException
  289. * @param itemSize Size of an item in a queue.
  290. * @note FreeRTOS queues use a memcpy / fixed size scheme for queues.
  291. */
  292. explicit BinaryQueue(UBaseType_t itemSize);
  293. /**
  294. * Add an item to the queue.
  295. *
  296. * @param item The item you are adding.
  297. * @return true always, because of overwrite.
  298. */
  299. virtual bool Enqueue(void *item);
  300. /**
  301. * Add an item to the queue in ISR context.
  302. *
  303. * @param item The item you are adding.
  304. * @param pxHigherPriorityTaskWoken Did this operation result in a
  305. * rescheduling event.
  306. * @return true always, because of overwrite.
  307. */
  308. virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken);
  309. };
  310. }
  311. #endif