Skip to content

Commit b8becad

Browse files
committed
Simplify push method and also add fixed_size arg to the MPMC queue
1 parent 689f63c commit b8becad

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

include/realtime_tools/lock_free_queue.hpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,14 @@ class LockFreeQueueBase
173173
* @return false If the queue is full or the data could not be pushed
174174
*/
175175

176-
[[nodiscard]] bool push(const T & data) { return data_queue_.push(data); }
176+
[[nodiscard]] bool push(const T & data)
177+
{
178+
if constexpr (is_spsc_queue<LockFreeContainer>::value) {
179+
return data_queue_.push(data);
180+
} else {
181+
return data_queue_.bounded_push(data);
182+
}
183+
}
177184

178185
/**
179186
* @brief Push the data into the queue
@@ -185,7 +192,11 @@ class LockFreeQueueBase
185192
template <typename U>
186193
[[nodiscard]] std::enable_if_t<std::is_convertible_v<T, U>, bool> push(const U & data)
187194
{
188-
return data_queue_.push(data);
195+
if constexpr (is_spsc_queue<LockFreeContainer>::value) {
196+
return data_queue_.push(data);
197+
} else {
198+
return data_queue_.bounded_push(data);
199+
}
189200
}
190201

191202
/**
@@ -203,17 +214,10 @@ class LockFreeQueueBase
203214
template <typename U>
204215
[[nodiscard]] std::enable_if_t<std::is_convertible_v<T, U>, bool> bounded_push(const U & data)
205216
{
206-
const auto bounded_push = [this](const U & info) -> bool {
207-
if constexpr (is_spsc_queue<LockFreeContainer>::value) {
208-
return data_queue_.push(info);
209-
} else {
210-
return data_queue_.bounded_push(info);
211-
}
212-
};
213-
if (!bounded_push(data)) {
217+
if (!push(data)) {
214218
T dummy;
215219
data_queue_.pop(dummy);
216-
return bounded_push(data);
220+
return push(data);
217221
}
218222
return true;
219223
}
@@ -317,7 +321,9 @@ using LockFreeSPSCQueue = std::conditional_t<
317321
*/
318322
template <class DataType, std::size_t Capacity = 0, bool FixedSize = true>
319323
using LockFreeMPMCQueue = std::conditional_t<
320-
Capacity == 0, LockFreeQueueBase<DataType, boost::lockfree::queue<DataType>>,
324+
Capacity == 0,
325+
LockFreeQueueBase<
326+
DataType, boost::lockfree::queue<DataType, boost::lockfree::fixed_sized<FixedSize>>>,
321327
LockFreeQueueBase<
322328
DataType,
323329
boost::lockfree::queue<

test/lock_free_queue_tests.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,27 @@ TEST(LockFreeMPMCQueue, initialize_value)
271271

272272
TEST(LockFreeMPMCQueue, test_push)
273273
{
274-
LockFreeMPMCQueue<double, 10> buffer;
275-
ASSERT_TRUE(buffer.empty()) << "Buffer should be empty";
276-
for (auto i = 1; i <= 10; i++) {
277-
ASSERT_TRUE(buffer.push(i)) << "Buffer should have space for element as size is 10";
278-
ASSERT_EQ(10, buffer.capacity());
274+
{
275+
LockFreeMPMCQueue<double, 10> buffer;
276+
ASSERT_TRUE(buffer.empty()) << "Buffer should be empty";
277+
for (auto i = 1; i <= 10; i++) {
278+
ASSERT_TRUE(buffer.push(i)) << "Buffer should have space for element as size is 10";
279+
ASSERT_EQ(10, buffer.capacity());
280+
}
281+
ASSERT_FALSE(buffer.push(11)) << "Buffer should not have space for element as size is 10";
282+
ASSERT_FALSE(buffer.empty());
283+
}
284+
{
285+
LockFreeMPMCQueue<double> buffer(10);
286+
ASSERT_TRUE(buffer.empty()) << "Buffer should be empty";
287+
for (auto i = 1; i <= 10; i++) {
288+
ASSERT_TRUE(buffer.push(i)) << "Buffer should have space for element as size is 10";
289+
ASSERT_EQ(10, buffer.capacity());
290+
}
291+
ASSERT_FALSE(buffer.push(11)) << "Buffer should not have space for element as size is 10";
292+
ASSERT_FALSE(buffer.push(12)) << "Buffer should not have space for element as size is 10";
293+
ASSERT_FALSE(buffer.empty());
279294
}
280-
ASSERT_FALSE(buffer.push(11)) << "Buffer should not have space for element as size is 10";
281-
ASSERT_FALSE(buffer.empty());
282295
}
283296

284297
TEST(LockFreeMPMCQueue, test_pop)

0 commit comments

Comments
 (0)