Skip to content

Commit f5cf022

Browse files
committed
rewrite MPSC queue support based on origin/cjappl#19
1 parent 7628b14 commit f5cf022

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

include/rtlog/rtlog.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,34 @@ template <typename T>
8080
inline constexpr bool has_try_dequeue_v = has_try_dequeue<T>::value;
8181
} // namespace detail
8282

83+
template<typename T, size_t MaxNumItem>
84+
class FarbotMPSCQueueWrapper {
85+
farbot::fifo<T, farbot::fifo_options::concurrency::single,
86+
farbot::fifo_options::concurrency::multiple,
87+
farbot::fifo_options::full_empty_failure_mode::
88+
return_false_on_full_or_empty,
89+
farbot::fifo_options::full_empty_failure_mode::
90+
overwrite_or_return_default>
91+
mQueue{MaxNumItem};
92+
public:
93+
bool try_enqueue(T const & item) {
94+
return mQueue.push(std::move(item));
95+
}
96+
bool try_enqueue(T && item) {
97+
return mQueue.push(std::move(item));
98+
}
99+
bool try_dequeue(T &item) {
100+
return mQueue.pop(item);
101+
}
102+
};
103+
83104
// On earlier versions of compilers (especially clang) you cannot
84105
// rely on defaulted template template parameters working as intended
85106
// This overload explicitly has 1 template paramter which is what
86107
// `Logger` expects, it uses the default 512 from ReaderWriterQueue as
87108
// the hardcoded MaxBlockSize
88109
template <typename T> using rtlog_SPSC = moodycamel::ReaderWriterQueue<T, 512>;
110+
template <typename T> using rtlog_MPSC = rtlog::FarbotMPSCQueueWrapper<T, 512>;
89111

90112
/**
91113
* @brief A logger class for logging messages.

test/test_rtlog.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ TEST(RtlogTest, BasicConstruction) {
8282
EXPECT_EQ(logger.PrintAndClearLogQueue(PrintMessage), 4);
8383
}
8484

85+
TEST(RtlogTest, BasicConstructionMPSC) {
86+
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
87+
gSequenceNumber, rtlog::rtlog_MPSC>
88+
logger;
89+
logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
90+
"Hello, world!");
91+
logger.Log({ExampleLogLevel::Info, ExampleLogRegion::Game}, "Hello, world!");
92+
logger.Log({ExampleLogLevel::Warning, ExampleLogRegion::Network},
93+
"Hello, world!");
94+
logger.Log({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
95+
"Hello, world!");
96+
97+
EXPECT_EQ(logger.PrintAndClearLogQueue(PrintMessage), 4);
98+
}
99+
85100
TEST(RtlogTest, VaArgsWorksAsIntended) {
86101
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
87102
gSequenceNumber>

0 commit comments

Comments
 (0)