CubicSDR/src/util/ThreadQueue.h

298 lines
8.6 KiB
C
Raw Normal View History

2014-11-20 20:10:28 -05:00
#pragma once
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/* Credit to Alfredo Pons / https://plus.google.com/109903449837592676231
* Code from http://gnodebian.blogspot.com.es/2013/07/a-thread-safe-asynchronous-queue-in-c11.html
*
* Changes:
* Charles J. Nov-19-2014
* - Renamed SafeQueue -> ThreadQueue
*/
#include <queue>
#include <list>
#include <mutex>
#include <thread>
#include <cstdint>
#include <condition_variable>
2015-08-25 21:35:38 -04:00
#include <atomic>
2014-11-22 22:33:32 -05:00
2015-07-29 20:57:02 -04:00
class ThreadQueueBase {
};
2014-11-20 20:10:28 -05:00
/** A thread-safe asynchronous queue */
2014-11-22 22:33:32 -05:00
template<class T, class Container = std::list<T>>
2015-07-29 20:57:02 -04:00
class ThreadQueue : public ThreadQueueBase {
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef Container container_type;
2014-11-22 22:33:32 -05:00
public:
2014-11-20 20:10:28 -05:00
/*! Create safe queue. */
ThreadQueue() {
m_max_num_items.store(0);
};
2014-11-22 22:33:32 -05:00
ThreadQueue(ThreadQueue&& sq) {
m_queue = std::move(sq.m_queue);
m_max_num_items.store(0);
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
ThreadQueue(const ThreadQueue& sq) {
std::lock_guard < std::mutex > lock(sq.m_mutex);
m_queue = sq.m_queue;
m_max_num_items.store(0);
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/*! Destroy safe queue. */
2014-11-22 22:33:32 -05:00
~ThreadQueue() {
std::lock_guard < std::mutex > lock(m_mutex);
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Sets the maximum number of items in the queue. Defaults is 0: No limit
* \param[in] item An item.
*/
2014-11-22 22:33:32 -05:00
void set_max_num_items(unsigned int max_num_items) {
std::lock_guard < std::mutex > lock(m_mutex);
if (m_max_num_items.load() != max_num_items) {
m_max_num_items.store(max_num_items);
}
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pushes the item into the queue.
* \param[in] item An item.
* \return true if an item was pushed into the queue
*/
2014-11-22 22:33:32 -05:00
bool push(const value_type& item) {
std::lock_guard < std::mutex > lock(m_mutex);
if (m_max_num_items.load() > 0 && m_queue.size() > m_max_num_items.load())
2014-11-22 22:33:32 -05:00
return false;
m_queue.push(item);
m_condition.notify_one();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pushes the item into the queue.
* \param[in] item An item.
* \return true if an item was pushed into the queue
*/
2014-11-22 22:33:32 -05:00
bool push(const value_type&& item) {
std::lock_guard < std::mutex > lock(m_mutex);
if (m_max_num_items.load() > 0 && m_queue.size() > m_max_num_items.load())
2014-11-22 22:33:32 -05:00
return false;
m_queue.push(item);
m_condition.notify_one();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pops item from the queue. If queue is empty, this function blocks until item becomes available.
* \param[out] item The item.
*/
2014-11-22 22:33:32 -05:00
void pop(value_type& item) {
std::unique_lock < std::mutex > lock(m_mutex);
m_condition.wait(lock, [this]() // Lambda funct
{
return !m_queue.empty();
});
item = m_queue.front();
m_queue.pop();
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pops item from the queue using the contained type's move assignment operator, if it has one..
* This method is identical to the pop() method if that type has no move assignment operator.
* If queue is empty, this function blocks until item becomes available.
* \param[out] item The item.
*/
2014-11-22 22:33:32 -05:00
void move_pop(value_type& item) {
std::unique_lock < std::mutex > lock(m_mutex);
m_condition.wait(lock, [this]() // Lambda funct
{
return !m_queue.empty();
});
item = std::move(m_queue.front());
m_queue.pop();
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Tries to pop item from the queue.
* \param[out] item The item.
* \return False is returned if no item is available.
*/
2014-11-22 22:33:32 -05:00
bool try_pop(value_type& item) {
std::unique_lock < std::mutex > lock(m_mutex);
if (m_queue.empty())
return false;
item = m_queue.front();
m_queue.pop();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Tries to pop item from the queue using the contained type's move assignment operator, if it has one..
* This method is identical to the try_pop() method if that type has no move assignment operator.
* \param[out] item The item.
* \return False is returned if no item is available.
*/
2014-11-22 22:33:32 -05:00
bool try_move_pop(value_type& item) {
std::unique_lock < std::mutex > lock(m_mutex);
if (m_queue.empty())
return false;
item = std::move(m_queue.front());
m_queue.pop();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pops item from the queue. If the queue is empty, blocks for timeout microseconds, or until item becomes available.
* \param[out] t An item.
* \param[in] timeout The number of microseconds to wait.
* \return true if get an item from the queue, false if no item is received before the timeout.
*/
2014-11-22 22:33:32 -05:00
bool timeout_pop(value_type& item, std::uint64_t timeout) {
std::unique_lock < std::mutex > lock(m_mutex);
if (m_queue.empty()) {
if (timeout == 0)
return false;
if (m_condition.wait_for(lock, std::chrono::microseconds(timeout)) == std::cv_status::timeout)
return false;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
item = m_queue.front();
m_queue.pop();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Pops item from the queue using the contained type's move assignment operator, if it has one..
* If the queue is empty, blocks for timeout microseconds, or until item becomes available.
* This method is identical to the try_pop() method if that type has no move assignment operator.
* \param[out] t An item.
* \param[in] timeout The number of microseconds to wait.
* \return true if get an item from the queue, false if no item is received before the timeout.
*/
2014-11-22 22:33:32 -05:00
bool timeout_move_pop(value_type& item, std::uint64_t timeout) {
std::unique_lock < std::mutex > lock(m_mutex);
if (m_queue.empty()) {
if (timeout == 0)
return false;
if (m_condition.wait_for(lock, std::chrono::microseconds(timeout)) == std::cv_status::timeout)
return false;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
item = std::move(m_queue.front());
m_queue.pop();
return true;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Gets the number of items in the queue.
* \return Number of items in the queue.
*/
2014-11-22 22:33:32 -05:00
size_type size() const {
std::lock_guard < std::mutex > lock(m_mutex);
return m_queue.size();
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/**
* Check if the queue is empty.
* \return true if queue is empty.
*/
2014-11-22 22:33:32 -05:00
bool empty() const {
std::lock_guard < std::mutex > lock(m_mutex);
return m_queue.empty();
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
/**
* Check if the queue is full.
* \return true if queue is full.
*/
bool full() const {
std::lock_guard < std::mutex > lock(m_mutex);
return (m_max_num_items.load() != 0) && (m_queue.size() >= m_max_num_items.load());
}
/**
* Remove any items in the queue.
*/
void flush() {
std::lock_guard < std::mutex > lock(m_mutex);
m_queue = std::queue<T, Container>();
std::queue<T, Container> emptyQueue;
std::swap(m_queue, emptyQueue);
}
2014-11-20 20:10:28 -05:00
/**
* Swaps the contents.
* \param[out] sq The ThreadQueue to swap with 'this'.
*/
2014-11-22 22:33:32 -05:00
void swap(ThreadQueue& sq) {
if (this != &sq) {
std::lock_guard < std::mutex > lock1(m_mutex);
std::lock_guard < std::mutex > lock2(sq.m_mutex);
m_queue.swap(sq.m_queue);
if (!m_queue.empty())
m_condition.notify_all();
if (!sq.m_queue.empty())
sq.m_condition.notify_all();
2014-11-20 20:10:28 -05:00
}
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/*! The copy assignment operator */
2014-11-22 22:33:32 -05:00
ThreadQueue& operator=(const ThreadQueue& sq) {
if (this != &sq) {
std::lock_guard < std::mutex > lock1(m_mutex);
std::lock_guard < std::mutex > lock2(sq.m_mutex);
std::queue<T, Container> temp { sq.m_queue };
m_queue.swap(temp);
if (!m_queue.empty())
m_condition.notify_all();
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
return *this;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/*! The move assignment operator */
2014-11-22 22:33:32 -05:00
ThreadQueue& operator=(ThreadQueue && sq) {
std::lock_guard < std::mutex > lock(m_mutex);
m_queue = std::move(sq.m_queue);
if (!m_queue.empty())
m_condition.notify_all();
return *this;
2014-11-20 20:10:28 -05:00
}
2014-11-22 22:33:32 -05:00
private:
2014-11-20 20:10:28 -05:00
std::queue<T, Container> m_queue;
mutable std::mutex m_mutex;
std::condition_variable m_condition;
std::atomic_uint m_max_num_items;
2014-11-20 20:10:28 -05:00
};
2014-11-22 22:33:32 -05:00
2014-11-20 20:10:28 -05:00
/*! Swaps the contents of two ThreadQueue objects. */
2014-11-22 22:33:32 -05:00
template<class T, class Container>
void swap(ThreadQueue<T, Container>& q1, ThreadQueue<T, Container>& q2) {
q1.swap(q2);
2014-11-20 20:10:28 -05:00
}