thread_queue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file thread_queue.h
  3. /// Implementation of the template class 'thread_queue', a thread-safe,
  4. /// blocking queue for passing data between threads, safe for use with smart
  5. /// pointers.
  6. /// @date 09-Jan-2017
  7. /////////////////////////////////////////////////////////////////////////////
  8. /*******************************************************************************
  9. * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
  10. *
  11. * All rights reserved. This program and the accompanying materials
  12. * are made available under the terms of the Eclipse Public License v1.0
  13. * and Eclipse Distribution License v1.0 which accompany this distribution.
  14. *
  15. * The Eclipse Public License is available at
  16. * http://www.eclipse.org/legal/epl-v10.html
  17. * and the Eclipse Distribution License is available at
  18. * http://www.eclipse.org/org/documents/edl-v10.php.
  19. *
  20. * Contributors:
  21. * Frank Pagliughi - initial implementation and documentation
  22. *******************************************************************************/
  23. #ifndef __mqtt_thread_queue_h
  24. #define __mqtt_thread_queue_h
  25. #include <thread>
  26. #include <mutex>
  27. #include <condition_variable>
  28. #include <limits>
  29. #include <deque>
  30. #include <queue>
  31. namespace mqtt {
  32. /////////////////////////////////////////////////////////////////////////////
  33. /**
  34. * A thread-safe queue for inter-thread communication.
  35. * This is a lockinq queue with blocking operations. The get() operations
  36. * can always block on an empty queue, but have variations for non-blocking
  37. * (try_get) and bounded-time blocking (try_get_for, try_get_until).
  38. * @par
  39. * The default queue has a capacity that is unbounded in the practical
  40. * sense, limited by available memory. In this mode the object will not
  41. * block when placing values into the queue. A capacity can bet set with the
  42. * construtor or, at any time later by calling the @ref capacity(size_type)
  43. * method. Using this latter method, the capacity can be set to an amount
  44. * smaller than the current size of the queue. In that case all put's to the
  45. * queue will block until the number of items are removed from the queue to
  46. * bring the size below the new capacity.
  47. * @par
  48. * Note that the queue uses move semantics to place items into the queue and
  49. * remove items from the queue. This means that the type, T, of the data
  50. * held by the queue only needs to follow move semantics; not copy
  51. * semantics. In addition, this means that copies of the value will @em not
  52. * be left in the queue. This is especially useful when creating queues of
  53. * shared pointers, as the "dead" part of the queue will not hold onto a
  54. * reference count after the item has been removed from the queue.
  55. *
  56. * @param T The type of the items to be held in the queue.
  57. * @param Container The type of the underlying container to use. It must
  58. * support back(), front(), push_back(), pop_front().
  59. */
  60. template <typename T, class Container=std::deque<T>>
  61. class thread_queue
  62. {
  63. public:
  64. /** The underlying container type to use for the queue. */
  65. using container_type = Container;
  66. /** The type of items to be held in the queue. */
  67. using value_type = T;
  68. /** The type used to specify number of items in the container. */
  69. using size_type = typename Container::size_type;
  70. /** The maximum capacity of the queue. */
  71. static constexpr size_type MAX_CAPACITY = std::numeric_limits<size_type>::max();
  72. private:
  73. /** Object lock */
  74. mutable std::mutex lock_;
  75. /** Condition get signaled when item added to empty queue */
  76. std::condition_variable notEmptyCond_;
  77. /** Condition gets signaled then item removed from full queue */
  78. std::condition_variable notFullCond_;
  79. /** The capacity of the queue */
  80. size_type cap_;
  81. /** The actual STL container to hold data */
  82. std::queue<T,Container> que_;
  83. /** Simple, scope-based lock guard */
  84. using guard = std::lock_guard<std::mutex>;
  85. /** General purpose guard */
  86. using unique_guard = std::unique_lock<std::mutex>;
  87. public:
  88. /**
  89. * Constructs a queue with the maximum capacity.
  90. */
  91. thread_queue() : cap_(MAX_CAPACITY) {}
  92. /**
  93. * Constructs a queue with the specified capacity.
  94. * @param cap The maximum number of items that can be placed in the
  95. * queue.
  96. */
  97. explicit thread_queue(size_t cap) : cap_(cap) {}
  98. /**
  99. * Determine if the queue is empty.
  100. * @return @em true if there are no elements in the queue, @em false if
  101. * there are any items in the queue.
  102. */
  103. bool empty() const {
  104. guard g(lock_);
  105. return que_.empty();
  106. }
  107. /**
  108. * Gets the capacity of the queue.
  109. * @return The maximum number of elements before the queue is full.
  110. */
  111. size_type capacity() const {
  112. guard g(lock_);
  113. return cap_;
  114. }
  115. /**
  116. * Sets the capacity of the queue.
  117. * Note that the capacity can be set to a value smaller than the current
  118. * size of the queue. In that event, all calls to put() will block until
  119. * a suffucuent number
  120. */
  121. void capacity(size_type cap) {
  122. guard g(lock_);
  123. cap_ = cap;
  124. }
  125. /**
  126. * Gets the number of items in the queue.
  127. * @return The number of items in the queue.
  128. */
  129. size_type size() const {
  130. guard g(lock_);
  131. return que_.size();
  132. }
  133. /**
  134. * Put an item into the queue.
  135. * If the queue is full, this will block the caller until items are
  136. * removed bringing the size less than the capacity.
  137. * @param val The value to add to the queue.
  138. */
  139. void put(value_type val) {
  140. unique_guard g(lock_);
  141. size_type n = que_.size();
  142. if (n >= cap_)
  143. notFullCond_.wait(g, [=]{return que_.size() < cap_;});
  144. que_.emplace(std::move(val));
  145. if (n == 0) {
  146. g.unlock();
  147. notEmptyCond_.notify_one();
  148. }
  149. }
  150. /**
  151. * Non-blocking attempt to place an item into the queue.
  152. * @param val The value to add to the queue.
  153. * @return @em true if the item was added to the queue, @em false if the
  154. * item was not added because the queue is currently full.
  155. */
  156. bool try_put(value_type val) {
  157. unique_guard g(lock_);
  158. size_type n = que_.size();
  159. if (n >= cap_)
  160. return false;
  161. que_.emplace(std::move(val));
  162. if (n == 0) {
  163. g.unlock();
  164. notEmptyCond_.notify_one();
  165. }
  166. return true;
  167. }
  168. /**
  169. * Attempt to place an item in the queue with a bounded wait.
  170. * This will attempt to place the value in the queue, but if it is full,
  171. * it will wait up to the specified time duration before timing out.
  172. * @param val The value to add to the queue.
  173. * @param relTime The amount of time to wait until timing out.
  174. * @return @em true if the value was added to the queue, @em false if a
  175. * timeout occurred.
  176. */
  177. template <typename Rep, class Period>
  178. bool try_put_for(value_type* val, const std::chrono::duration<Rep, Period>& relTime) {
  179. unique_guard g(lock_);
  180. size_type n = que_.size();
  181. if (n >= cap_ && !notFullCond_.wait_for(g, relTime, [=]{return que_.size() < cap_;}))
  182. return false;
  183. que_.emplace(std::move(val));
  184. if (n == 0) {
  185. g.unlock();
  186. notEmptyCond_.notify_one();
  187. }
  188. return true;
  189. }
  190. /**
  191. * Attempt to place an item in the queue with a bounded wait to an
  192. * absolute time point.
  193. * This will attempt to place the value in the queue, but if it is full,
  194. * it will wait up until the specified time before timing out.
  195. * @param val The value to add to the queue.
  196. * @param absTime The absolute time to wait to before timing out.
  197. * @return @em true if the value was added to the queue, @em false if a
  198. * timeout occurred.
  199. */
  200. template <class Clock, class Duration>
  201. bool try_put_until(value_type* val, const std::chrono::time_point<Clock,Duration>& absTime) {
  202. unique_guard g(lock_);
  203. size_type n = que_.size();
  204. if (n >= cap_ && !notFullCond_.wait_until(g, absTime, [=]{return que_.size() < cap_;}))
  205. return false;
  206. que_.emplace(std::move(val));
  207. if (n == 0) {
  208. g.unlock();
  209. notEmptyCond_.notify_one();
  210. }
  211. return true;
  212. }
  213. /**
  214. * Retrieve a value from the queue.
  215. * If the queue is empty, this will block indefinitely until a value is
  216. * added to the queue by another thread,
  217. * @param val Pointer to a variable to receive the value.
  218. */
  219. void get(value_type* val) {
  220. unique_guard g(lock_);
  221. auto n = que_.size();
  222. if (n == 0)
  223. notEmptyCond_.wait(g, [=]{return !que_.empty();});
  224. *val = std::move(que_.front());
  225. que_.pop();
  226. if (n == cap_) {
  227. g.unlock();
  228. notFullCond_.notify_one();
  229. }
  230. }
  231. /**
  232. * Retrieve a value from the queue.
  233. * If the queue is empty, this will block indefinitely until a value is
  234. * added to the queue by another thread,
  235. * @return The value removed from the queue
  236. */
  237. value_type get() {
  238. unique_guard g(lock_);
  239. auto n = que_.size();
  240. if (n == 0)
  241. notEmptyCond_.wait(g, [=]{return !que_.empty();});
  242. value_type val = std::move(que_.front());
  243. que_.pop();
  244. if (n == cap_) {
  245. g.unlock();
  246. notFullCond_.notify_one();
  247. }
  248. return val;
  249. }
  250. /**
  251. * Attempts to remove a value from the queue without blocking.
  252. * If the queue is currently empty, this will return immediately with a
  253. * failure, otherwise it will get the next value and return it.
  254. * @param val Pointer to a variable to receive the value.
  255. * @return @em true if a value was removed from the queue, @em false if
  256. * the queue is empty.
  257. */
  258. bool try_get(value_type* val) {
  259. unique_guard g(lock_);
  260. auto n = que_.size();
  261. if (n == 0)
  262. return false;
  263. *val = std::move(que_.front());
  264. que_.pop();
  265. if (n == cap_) {
  266. g.unlock();
  267. notFullCond_.notify_one();
  268. }
  269. return true;
  270. }
  271. /**
  272. * Attempt to remove an item from the queue for a bounded amout of time.
  273. * This will retrieve the next item from the queue. If the queue is
  274. * empty, it will wait the specified amout of time for an item to arive
  275. * before timing out.
  276. * @param val Pointer to a variable to receive the value.
  277. * @param relTime The amount of time to wait until timing out.
  278. * @return @em true if the value was removed the queue, @em false if a
  279. * timeout occurred.
  280. */
  281. template <typename Rep, class Period>
  282. bool try_get_for(value_type* val, const std::chrono::duration<Rep, Period>& relTime) {
  283. unique_guard g(lock_);
  284. auto n = que_.size();
  285. if (n == 0 && !notEmptyCond_.wait_for(g, relTime, [=]{return !que_.empty();}))
  286. return false;
  287. *val = std::move(que_.front());
  288. que_.pop();
  289. if (n == cap_) {
  290. g.unlock();
  291. notFullCond_.notify_one();
  292. }
  293. return true;
  294. }
  295. /**
  296. * Attempt to remove an item from the queue for a bounded amout of time.
  297. * This will retrieve the next item from the queue. If the queue is
  298. * empty, it will wait until the specified time for an item to arive
  299. * before timing out.
  300. * @param val Pointer to a variable to receive the value.
  301. * @param absTime The absolute time to wait to before timing out.
  302. * @return @em true if the value was removed from the queue, @em false
  303. * if a timeout occurred.
  304. */
  305. template <class Clock, class Duration>
  306. bool try_get_until(value_type* val, const std::chrono::time_point<Clock,Duration>& absTime) {
  307. unique_guard g(lock_);
  308. auto n = que_.size();
  309. if (n == 0 && !notEmptyCond_.wait_until(g, absTime, [=]{return !que_.empty();}))
  310. return false;
  311. *val = std::move(que_.front());
  312. que_.pop();
  313. if (n == cap_) {
  314. g.unlock();
  315. notFullCond_.notify_one();
  316. }
  317. return true;
  318. }
  319. };
  320. /////////////////////////////////////////////////////////////////////////////
  321. // end namespace mqtt
  322. }
  323. #endif // __mqtt_thread_queue_h