Improved PIBlockingDeque behaviour and unit tests for put, offer, take methods
- Methods put, offer, take begins working with move and copy semantics - Mocking queue condition variables with GMock in Unit tests - Rewrite part of unit tests
This commit is contained in:
@@ -70,10 +70,11 @@ public:
|
||||
*
|
||||
* @param v the element to add
|
||||
*/
|
||||
void put(T && v) {
|
||||
template<typename Type>
|
||||
void put(Type && v) {
|
||||
mutex.lock();
|
||||
cond_var_rem->wait(mutex, [&]() { return data_queue.size() < max_size; });
|
||||
data_queue.push_back(std::forward<T>(v));
|
||||
data_queue.push_back(std::forward<Type>(v));
|
||||
mutex.unlock();
|
||||
cond_var_add->notifyOne();
|
||||
}
|
||||
@@ -106,10 +107,11 @@ public:
|
||||
* @param timeoutMs how long to wait before giving up, in milliseconds
|
||||
* @return true if successful, or false if the specified waiting time elapses before space is available
|
||||
*/
|
||||
bool offer(T && v, int timeoutMs) {
|
||||
template<typename Type>
|
||||
bool offer(Type && v, int timeoutMs) {
|
||||
mutex.lock();
|
||||
bool isOk = cond_var_rem->waitFor(mutex, timeoutMs, [&]() { return data_queue.size() < max_size; } );
|
||||
if (isOk) data_queue.push_back(std::forward<T>(v));
|
||||
if (isOk) data_queue.push_back(std::forward<Type>(v));
|
||||
mutex.unlock();
|
||||
if (isOk) cond_var_add->notifyOne();
|
||||
return isOk;
|
||||
@@ -140,7 +142,8 @@ public:
|
||||
* return value is retrieved value
|
||||
* @return the head of this queue, or defaultVal if the specified waiting time elapses before an element is available
|
||||
*/
|
||||
T poll(int timeoutMs, T && defaultVal = T(), bool * isOk = nullptr) {
|
||||
template<typename Type = T>
|
||||
T poll(int timeoutMs, Type && defaultVal = Type(), bool * isOk = nullptr) {
|
||||
mutex.lock();
|
||||
bool isNotEmpty = cond_var_add->waitFor(mutex, timeoutMs, [&]() { return data_queue.size() != 0; });
|
||||
T t;
|
||||
@@ -148,7 +151,7 @@ public:
|
||||
t = std::move(data_queue.front());
|
||||
data_queue.pop_front();
|
||||
} else {
|
||||
t = std::move(defaultVal);
|
||||
t = std::forward<Type>(defaultVal);
|
||||
}
|
||||
mutex.unlock();
|
||||
if (isNotEmpty) cond_var_rem->notifyOne();
|
||||
@@ -165,7 +168,8 @@ public:
|
||||
* return value is retrieved value
|
||||
* @return the head of this queue, or defaultVal if the specified waiting time elapses before an element is available
|
||||
*/
|
||||
T poll(T && defaultVal = T(), bool * isOk = nullptr) {
|
||||
template<typename Type = T>
|
||||
T poll(Type && defaultVal = Type(), bool * isOk = nullptr) {
|
||||
T t;
|
||||
mutex.lock();
|
||||
bool isNotEmpty = data_queue.size() != 0;
|
||||
@@ -173,7 +177,7 @@ public:
|
||||
t = std::move(data_queue.front());
|
||||
data_queue.pop_front();
|
||||
} else {
|
||||
t = std::move(defaultVal);
|
||||
t = std::forward<Type>(defaultVal);
|
||||
}
|
||||
mutex.unlock();
|
||||
if (isNotEmpty) cond_var_rem->notifyOne();
|
||||
|
||||
Reference in New Issue
Block a user