Skip to content

Commit

Permalink
rename lock queue
Browse files Browse the repository at this point in the history
  • Loading branch information
KjellKod committed Dec 15, 2023
1 parent 8715b26 commit 19e3dad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
14 changes: 7 additions & 7 deletions benchmark/test_performance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@

// // TEST(Performance, MPMC_1_to_1) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kAmount);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kAmount);
// // RunSPSC(queue, kAmount);
// // }

// // TEST(Performance, MPMC_1_to_1_Smaller) {
// // using namespace std;

// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // RunSPSC(queue, kAmount);
// // }

Expand Down Expand Up @@ -99,7 +99,7 @@

// // TEST(Performance, DISABLED_MPMC_1_to_4_20secRun_LargeData) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // const size_t large = 65000;
// // std::string payload(large, 'x');
// // EXPECT_EQ(large, payload.size());
Expand All @@ -111,7 +111,7 @@

// // TEST(Performance, DISABLED_MPMC_1_to_4_20secRun_SmallData) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // const size_t small = 10;
// // std::string payload(small, 'x');
// // EXPECT_EQ(small, payload.size());
Expand Down Expand Up @@ -157,7 +157,7 @@

// // TEST(Performance, DISABLED_MPMC_4_to_1_20secRun_LargeData) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // const size_t large = 65000;
// // std::string payload(large, 'x');
// // EXPECT_EQ(large, payload.size());
Expand All @@ -169,7 +169,7 @@

// // TEST(Performance, DISABLED_MPMC_4_to_1_20secRun_SmallData) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // const size_t small = 10;
// // std::string payload(small, 'x');
// // EXPECT_EQ(small, payload.size());
Expand Down Expand Up @@ -215,7 +215,7 @@

// // TEST(Performance, DISABLED_MPMC_4_to_4_20secRun_LargeData) {
// // using namespace std;
// // auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<string>>(kSmallQueueSize);
// // auto queue = queue_api::CreateQueue<mpmc::lock_queue<string>>(kSmallQueueSize);
// // const size_t large = 65000;
// // std::string payload(large, 'x');
// // EXPECT_EQ(large, payload.size());
Expand Down
2 changes: 1 addition & 1 deletion src/q/mpmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

#pragma once

#include "q/mpmc_flexible_lock_queue.hpp"
#include "q/mpmc_lock_queue.hpp"
34 changes: 17 additions & 17 deletions src/q/mpmc_flexible_lock_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@
* protected by mutex. Since 'return by reference' is used this queue won't throw */
namespace mpmc {
template <typename T>
class flexible_lock_queue {
class lock_queue {
static const int kUnlimited = -1;
static const int kSmallDefault = 100;
const int kMaxSize;
std::queue<T> queue_;
mutable std::mutex m_;
std::condition_variable data_cond_;

flexible_lock_queue& operator=(const flexible_lock_queue&) = delete;
flexible_lock_queue(const flexible_lock_queue& other) = delete;
lock_queue& operator=(const lock_queue&) = delete;
lock_queue(const lock_queue& other) = delete;

bool internal_full() const;
size_t internal_capacity() const;

public:
// -1 : unbounded
// 0 ... N : bounded (0 is silly)
flexible_lock_queue(const int maxSize = kSmallDefault);
lock_queue(const int maxSize = kSmallDefault);

bool lock_free() const;
bool push(T& item);
Expand All @@ -68,16 +68,16 @@ namespace mpmc {

// maxSize of -1 equals unlimited size
template <typename T>
flexible_lock_queue<T>::flexible_lock_queue(int maxSize) :
lock_queue<T>::lock_queue(int maxSize) :
kMaxSize(maxSize) {}

template <typename T>
bool flexible_lock_queue<T>::lock_free() const {
bool lock_queue<T>::lock_free() const {
return false;
}

template <typename T>
bool flexible_lock_queue<T>::push(T& item) {
bool lock_queue<T>::push(T& item) {
{
std::lock_guard<std::mutex> lock(m_);
if (internal_full()) {
Expand All @@ -90,7 +90,7 @@ namespace mpmc {
}

template <typename T>
bool flexible_lock_queue<T>::pop(T& popped_item) {
bool lock_queue<T>::pop(T& popped_item) {
std::lock_guard<std::mutex> lock(m_);
if (queue_.empty()) {
return false;
Expand All @@ -101,7 +101,7 @@ namespace mpmc {
}

template <typename T>
bool flexible_lock_queue<T>::wait_and_pop(T& popped_item, std::chrono::milliseconds max_wait) {
bool lock_queue<T>::wait_and_pop(T& popped_item, std::chrono::milliseconds max_wait) {
std::unique_lock<std::mutex> lock(m_);
auto const timeout = std::chrono::steady_clock::now() + max_wait;
while (queue_.empty()) {
Expand All @@ -121,52 +121,52 @@ namespace mpmc {
}

template <typename T>
bool flexible_lock_queue<T>::full() {
bool lock_queue<T>::full() {
std::lock_guard<std::mutex> lock(m_);
return internal_full();
}

template <typename T>
bool flexible_lock_queue<T>::empty() const {
bool lock_queue<T>::empty() const {
std::lock_guard<std::mutex> lock(m_);
return queue_.empty();
}

template <typename T>
size_t flexible_lock_queue<T>::size() const {
size_t lock_queue<T>::size() const {
std::lock_guard<std::mutex> lock(m_);
return queue_.size();
}

template <typename T>
size_t flexible_lock_queue<T>::capacity() const {
size_t lock_queue<T>::capacity() const {
std::lock_guard<std::mutex> lock(m_);
return internal_capacity();
}

template <typename T>
size_t flexible_lock_queue<T>::capacity_free() const {
size_t lock_queue<T>::capacity_free() const {
std::lock_guard<std::mutex> lock(m_);
return internal_capacity() - queue_.size();
}

template <typename T>
size_t flexible_lock_queue<T>::usage() const {
size_t lock_queue<T>::usage() const {
std::lock_guard<std::mutex> lock(m_);
return (100 * queue_.size() / internal_capacity());
}

// private
template <typename T>
size_t flexible_lock_queue<T>::internal_capacity() const {
size_t lock_queue<T>::internal_capacity() const {
if (kMaxSize == kUnlimited) {
return std::numeric_limits<unsigned int>::max();
}
return kMaxSize;
}

template <typename T>
bool flexible_lock_queue<T>::internal_full() const {
bool lock_queue<T>::internal_full() const {
if (kMaxSize == kUnlimited) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions test/test_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Type = string;
using FlexibleQ = spsc::flexible::circular_fifo<Type>;
using FixedQ = spsc::fixed::circular_fifo<Type, 100>;
using FixedSmallQ = spsc::fixed::circular_fifo<Type, 2>;
using LockedQ = mpmc::flexible_lock_queue<Type>;
using LockedQ = mpmc::lock_queue<Type>;

template <typename Prod, typename Cons>
void ProdConsInitialization(Prod& prod, Cons& cons) {
Expand Down Expand Up @@ -358,7 +358,7 @@ TEST(Queue, FixedQ_MoveUnique) {
}

TEST(Queue, LockedQ_MoveUnique) {
auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<Unique>>(2);
auto queue = queue_api::CreateQueue<mpmc::lock_queue<Unique>>(2);
auto producer = std::get<queue_api::index::sender>(queue);
auto consumer = std::get<queue_api::index::receiver>(queue);
MoveUniquePtrArgument(producer, consumer);
Expand Down Expand Up @@ -409,7 +409,7 @@ TEST(Queue, FixedQ_NoMoveOfPtr) {
}

TEST(Queue, LockedQ_NoMoveUnique) {
auto queue = queue_api::CreateQueue<mpmc::flexible_lock_queue<Ptr>>(2);
auto queue = queue_api::CreateQueue<mpmc::lock_queue<Ptr>>(2);
auto producer = std::get<queue_api::index::sender>(queue);
auto consumer = std::get<queue_api::index::receiver>(queue);
NoMovePtrArgument(producer, consumer);
Expand Down

0 comments on commit 19e3dad

Please sign in to comment.