Refactor PIBlockingDequeue

This commit is contained in:
3 changed files with 130 additions and 95 deletions

View File

@@ -21,14 +21,13 @@
#define PIBLOCKINGDEQUEUE_H
#include <queue>
#include "pideque.h"
#include "piconditionvar.h"
/**
* @brief A Queue that supports operations that wait for the queue to become non-empty when retrieving an element, and
* wait for space to become available in the queue when storing an element.
*/
template <typename T, template<typename = T, typename...> class Queue_ = std::deque >
template <typename T, template<typename = T, typename...> class Queue_ = std::deque, typename ConditionVariable_ = PIConditionVariable>
class PIBlockingDequeue {
public:
typedef Queue_<T> QueueType;
@@ -36,29 +35,27 @@ public:
/**
* @brief Constructor
*/
explicit inline PIBlockingDequeue(size_t capacity = SIZE_MAX,
PIConditionVariable* cond_var_add = new PIConditionVariable(),
PIConditionVariable* cond_var_rem = new PIConditionVariable())
: cond_var_add(cond_var_add), cond_var_rem(cond_var_rem), max_size(capacity) { }
explicit PIBlockingDequeue(size_t capacity = SIZE_MAX)
: cond_var_add(new ConditionVariable_()), cond_var_rem(new ConditionVariable_()), max_size(capacity) { }
/**
* @brief Copy constructor. Initialize queue with copy of other queue elements. Not thread-safe for other queue.
* @brief Copy constructor. Initialize queue with copy of other container elements. Not thread-safe for other queue.
*/
explicit inline PIBlockingDequeue(const QueueType& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
template<typename Iterable>
explicit PIBlockingDequeue(const Iterable& other): PIBlockingDequeue() {
mutex.lock();
max_size = SIZE_MAX;
data_queue = QueueType(other);
for (const T& t : other) data_queue.push_back(t);
mutex.unlock();
}
/**
* @brief Thread-safe copy constructor. Initialize queue with copy of other queue elements.
*/
inline PIBlockingDequeue(PIBlockingDequeue<T>& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
inline PIBlockingDequeue(PIBlockingDequeue<T>& other): PIBlockingDequeue() {
other.mutex.lock();
mutex.lock();
max_size = other.max_size;
data_queue = QueueType(other.data_queue);
data_queue = other.data_queue;
mutex.unlock();
other.mutex.unlock();
}
@@ -88,13 +85,14 @@ public:
* @param v the element to add
* @return true if the element was added to this queue, else false
*/
bool offer(T && v) {
template<typename Type>
bool offer(Type && v) {
mutex.lock();
if (data_queue.size() >= max_size) {
mutex.unlock();
return false;
}
data_queue.push_back(std::forward<T>(v));
data_queue.push_back(std::forward<Type>(v));
mutex.unlock();
cond_var_add->notifyOne();
return true;
@@ -124,7 +122,7 @@ public:
*/
T take() {
mutex.lock();
cond_var_add->wait(mutex, [&]() { return !data_queue.empty(); });
cond_var_add->wait(mutex, [&]() { return data_queue.size() != 0; });
T t = std::move(data_queue.front());
data_queue.pop_front();
mutex.unlock();
@@ -144,7 +142,7 @@ public:
*/
T poll(int timeoutMs, T && defaultVal = T(), bool * isOk = nullptr) {
mutex.lock();
bool isNotEmpty = cond_var_add->waitFor(mutex, timeoutMs, [&]() { return !data_queue.empty(); });
bool isNotEmpty = cond_var_add->waitFor(mutex, timeoutMs, [&]() { return data_queue.size() != 0; });
T t;
if (isNotEmpty) {
t = std::move(data_queue.front());
@@ -170,7 +168,7 @@ public:
T poll(T && defaultVal = T(), bool * isOk = nullptr) {
T t;
mutex.lock();
bool isNotEmpty = !data_queue.empty();
bool isNotEmpty = data_queue.size() != 0;
if (isNotEmpty) {
t = std::move(data_queue.front());
data_queue.pop_front();
@@ -223,9 +221,10 @@ public:
/**
* @brief Removes all available elements from this queue and adds them to other given queue.
*/
size_t drainTo(QueueType& other, size_t maxCount = SIZE_MAX) {
template<typename Appendable>
size_t drainTo(Appendable& other, size_t maxCount = SIZE_MAX) {
mutex.lock();
size_t count = ((maxCount > data_queue.size()) ? data_queue.size() : maxCount);
size_t count = maxCount > data_queue.size() ? data_queue.size() : maxCount;
for (size_t i = 0; i < count; ++i) {
other.push_back(std::move(data_queue.front()));
data_queue.pop_front();
@@ -252,9 +251,10 @@ public:
return count;
}
private:
protected:
PIMutex mutex;
PIConditionVariable * cond_var_add, * cond_var_rem;
// TODO change to type without point
ConditionVariable_ *cond_var_add, *cond_var_rem;
QueueType data_queue;
size_t max_size;