Skip to content

Commit

Permalink
rewrite MPSC queue support based on origin/cjappl#19
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushieno committed Dec 23, 2024
1 parent 7628b14 commit f5cf022
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/rtlog/rtlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,34 @@ template <typename T>
inline constexpr bool has_try_dequeue_v = has_try_dequeue<T>::value;
} // namespace detail

template<typename T, size_t MaxNumItem>
class FarbotMPSCQueueWrapper {
farbot::fifo<T, farbot::fifo_options::concurrency::single,
farbot::fifo_options::concurrency::multiple,
farbot::fifo_options::full_empty_failure_mode::
return_false_on_full_or_empty,
farbot::fifo_options::full_empty_failure_mode::
overwrite_or_return_default>
mQueue{MaxNumItem};
public:
bool try_enqueue(T const & item) {
return mQueue.push(std::move(item));
}
bool try_enqueue(T && item) {
return mQueue.push(std::move(item));
}
bool try_dequeue(T &item) {
return mQueue.pop(item);
}
};

// On earlier versions of compilers (especially clang) you cannot
// rely on defaulted template template parameters working as intended
// This overload explicitly has 1 template paramter which is what
// `Logger` expects, it uses the default 512 from ReaderWriterQueue as
// the hardcoded MaxBlockSize
template <typename T> using rtlog_SPSC = moodycamel::ReaderWriterQueue<T, 512>;
template <typename T> using rtlog_MPSC = rtlog::FarbotMPSCQueueWrapper<T, 512>;

/**
* @brief A logger class for logging messages.
Expand Down
15 changes: 15 additions & 0 deletions test/test_rtlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ TEST(RtlogTest, BasicConstruction) {
EXPECT_EQ(logger.PrintAndClearLogQueue(PrintMessage), 4);
}

TEST(RtlogTest, BasicConstructionMPSC) {
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
gSequenceNumber, rtlog::rtlog_MPSC>
logger;
logger.Log({ExampleLogLevel::Debug, ExampleLogRegion::Engine},
"Hello, world!");
logger.Log({ExampleLogLevel::Info, ExampleLogRegion::Game}, "Hello, world!");
logger.Log({ExampleLogLevel::Warning, ExampleLogRegion::Network},
"Hello, world!");
logger.Log({ExampleLogLevel::Critical, ExampleLogRegion::Audio},
"Hello, world!");

EXPECT_EQ(logger.PrintAndClearLogQueue(PrintMessage), 4);
}

TEST(RtlogTest, VaArgsWorksAsIntended) {
rtlog::Logger<ExampleLogData, MAX_NUM_LOG_MESSAGES, MAX_LOG_MESSAGE_LENGTH,
gSequenceNumber>
Expand Down

0 comments on commit f5cf022

Please sign in to comment.