code format
This commit is contained in:
@@ -5,50 +5,53 @@
|
||||
* \~russian Блокирующая очередь
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
|
||||
Stephan Fomenko
|
||||
PIP - Platform Independent Primitives
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
Stephan Fomenko
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIBLOCKINGQUEUE_H
|
||||
#define PIBLOCKINGQUEUE_H
|
||||
|
||||
#include "pideque.h"
|
||||
#include "piconditionvar.h"
|
||||
#include "pideque.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>
|
||||
class PIBlockingQueue: private PIQueue<T> {
|
||||
public:
|
||||
|
||||
/**
|
||||
* \brief Constructor
|
||||
*/
|
||||
explicit inline PIBlockingQueue(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 inline PIBlockingQueue(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) {}
|
||||
|
||||
/**
|
||||
* \brief Copy constructor. Initialize queue with copy of other queue elements. Not thread-safe for other queue.
|
||||
*/
|
||||
explicit inline PIBlockingQueue(const PIDeque<T>& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
|
||||
explicit inline PIBlockingQueue(const PIDeque<T> & other)
|
||||
: cond_var_add(new PIConditionVariable())
|
||||
, cond_var_rem(new PIConditionVariable()) {
|
||||
mutex.lock();
|
||||
max_size = SIZE_MAX;
|
||||
PIDeque<T>::append(other);
|
||||
@@ -58,11 +61,11 @@ public:
|
||||
/**
|
||||
* \brief Thread-safe copy constructor. Initialize queue with copy of other queue elements.
|
||||
*/
|
||||
inline PIBlockingQueue(PIBlockingQueue<T> & other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
|
||||
inline PIBlockingQueue(PIBlockingQueue<T> & other): cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
|
||||
other.mutex.lock();
|
||||
mutex.lock();
|
||||
max_size = other.max_size;
|
||||
PIDeque<T>::append(static_cast<PIDeque<T>&>(other));
|
||||
PIDeque<T>::append(static_cast<PIDeque<T> &>(other));
|
||||
mutex.unlock();
|
||||
other.mutex.unlock();
|
||||
}
|
||||
@@ -86,7 +89,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIBlockingQueue<T> & enqueue(const T & v) {return put(v);}
|
||||
PIBlockingQueue<T> & enqueue(const T & v) { return put(v); }
|
||||
|
||||
/**
|
||||
* \brief Inserts the specified element at the end of this queue if it is possible to do so immediately without
|
||||
@@ -102,7 +105,7 @@ public:
|
||||
if (timeoutMs == 0)
|
||||
isOk = PIDeque<T>::size() < max_size;
|
||||
else
|
||||
isOk = cond_var_rem->waitFor(mutex, timeoutMs, [&]() { return PIDeque<T>::size() < max_size; } );
|
||||
isOk = cond_var_rem->waitFor(mutex, timeoutMs, [&]() { return PIDeque<T>::size() < max_size; });
|
||||
if (isOk) PIDeque<T>::push_back(v);
|
||||
mutex.unlock();
|
||||
if (isOk) cond_var_add->notifyOne();
|
||||
@@ -124,7 +127,7 @@ public:
|
||||
return t;
|
||||
}
|
||||
|
||||
T dequeue() {return take();}
|
||||
T dequeue() { return take(); }
|
||||
|
||||
/**
|
||||
* \brief Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an
|
||||
@@ -191,10 +194,11 @@ public:
|
||||
/**
|
||||
* \brief Removes all available elements from this queue and adds them to other given queue.
|
||||
*/
|
||||
size_t drainTo(PIDeque<T>& other, size_t maxCount = SIZE_MAX) {
|
||||
size_t drainTo(PIDeque<T> & other, size_t maxCount = SIZE_MAX) {
|
||||
mutex.lock();
|
||||
size_t count = ((maxCount > PIDeque<T>::size()) ? PIDeque<T>::size() : maxCount);
|
||||
for (size_t i = 0; i < count; ++i) other.push_back(PIDeque<T>::take_front());
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
other.push_back(PIDeque<T>::take_front());
|
||||
mutex.unlock();
|
||||
return count;
|
||||
}
|
||||
@@ -202,13 +206,14 @@ public:
|
||||
/**
|
||||
* \brief Removes all available elements from this queue and adds them to other given queue.
|
||||
*/
|
||||
size_t drainTo(PIBlockingQueue<T>& other, size_t maxCount = SIZE_MAX) {
|
||||
size_t drainTo(PIBlockingQueue<T> & other, size_t maxCount = SIZE_MAX) {
|
||||
mutex.lock();
|
||||
other.mutex.lock();
|
||||
size_t count = maxCount > PIDeque<T>::size() ? PIDeque<T>::size() : maxCount;
|
||||
size_t otherRemainingCapacity = other.max_size - static_cast<PIDeque<T> >(other).size();
|
||||
size_t count = maxCount > PIDeque<T>::size() ? PIDeque<T>::size() : maxCount;
|
||||
size_t otherRemainingCapacity = other.max_size - static_cast<PIDeque<T>>(other).size();
|
||||
if (count > otherRemainingCapacity) count = otherRemainingCapacity;
|
||||
for (size_t i = 0; i < count; ++i) other.push_back(PIDeque<T>::take_front());
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
other.push_back(PIDeque<T>::take_front());
|
||||
other.mutex.unlock();
|
||||
mutex.unlock();
|
||||
return count;
|
||||
@@ -216,9 +221,8 @@ public:
|
||||
|
||||
private:
|
||||
PIMutex mutex;
|
||||
PIConditionVariable * cond_var_add, * cond_var_rem;
|
||||
PIConditionVariable *cond_var_add, *cond_var_rem;
|
||||
size_t max_size;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user